我们使用 Google Drive v3 API 来管理 Web 应用程序中的文档。我们有一个简单的用例,其中用户单击按钮,后端需要从文件夹复制大约 5-10 个source文件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);
}
Run Code Online (Sandbox Code Playgroud)
批处理执行成功后处理响应:
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();
}
};
Run Code Online (Sandbox Code Playgroud)
我遵循了 Google 推荐的最佳实践:
API 需要 7 秒才能完成这个简单的任务。从用户体验的角度来看,这是一个非常糟糕的表现。我想知道这是预期的延迟还是我在这里做错了什么?
你的代码没问题。Google Drive API 有时确实很慢。在这种情况下,唯一的解决方案是,如果花费的时间超过阈值时间,则向 API 发出新请求。大多数情况下,新的请求会很快返回数据。
关于堆栈溢出的一些答案说,这不是驱动API,而是开发人员的代码导致代码变慢,从我个人的经验来看,这是不正确的。(我检查过 Chrome 开发工具,API 的响应时间通常在 20-30 秒范围内)。
| 归档时间: |
|
| 查看次数: |
3139 次 |
| 最近记录: |