【发布时间】:2020-04-09 09:59:29
【问题描述】:
我们正在使用 Google Drive v3 API 来管理我们网络应用程序中的文档。我们有一个简单的用例,用户点击一个按钮,后端需要从source 复制大约5-10 个文件到destination 文件夹。我已经在源文件夹中测试了 6 个文件,API 花了大约 7 秒。我使用批处理来调用复制文件 API。以下是相同的代码:
将请求添加到队列:
for(Template template: templates) {
File file = new File();
file.setParents(Collections.singletonList(parentFileId));
file.setName(template.getName());
file.setWritersCanShare(false);
file.setViewersCanCopyContent(false);
Map<String, String> appProperties = new HashMap<>();
appProperties.put(TEMPLATE_CODE_PROP_NAME, template.getCode());
file.setAppProperties(appProperties);
driveService.files().copy(template.getFileId(), file)
.setFields("id, name, appProperties, webViewLink, iconLink, mimeType")
.queue(batch, callback);
}
批处理成功后处理响应:
JsonBatchCallback<File> callback = new JsonBatchCallback<File>() {
@Override
public void onSuccess(File file, HttpHeaders responseHeaders) throws IOException {
log.info("Copied file successfully - " + file.getName() + " " + file.getId());
}
@Override
public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) throws IOException {
log.severe("Failed to copy file " + e.getCode() + " " + e.getMessage());
throw new Exception();
}
};
我已遵循 Google 推荐的最佳做法:
- 设置响应中所需的字段,以便我们获得部分响应而不是完整响应
- 使用批处理调用 API
API 需要 7 秒来完成这个简单的任务。从用户体验的角度来看,这是一个非常糟糕的表现。 我想知道这是预期的延迟还是我在这里做错了什么?
【问题讨论】:
-
您可以尝试检查 Drive API 的Performance Tips,它涵盖了一些可用于提高应用程序性能的技术。它还介绍了如何使用 gzip 和部分响应。欲了解更多信息,请查看此相关SO question。
-
我已经查看了这些链接,并且已经在遵循这些最佳实践。我引用的延迟数字是在实施最佳实践之后得出的。
标签: google-drive-api