Google drive api 谷歌驱动API响应非常慢

Google drive api 谷歌驱动API响应非常慢,google-drive-api,Google Drive Api,我们正在使用GoogleDriveV3API来管理web应用程序中的文档。我们有一个简单的用例,用户点击一个按钮,后端需要将大约5-10个文件从source复制到destination文件夹。我用源文件夹中的6个文件进行了测试,API大约用了7秒钟。我已经使用批处理来调用复制文件API。以下是相同的代码: 向队列添加请求: for(Template template: templates) { File file = new File(); file.setParents(Col

我们正在使用GoogleDriveV3API来管理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);
}
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();
    }           
};
for(模板:模板){
File File=新文件();
setParents(Collections.singletonList(parentFileId));
file.setName(template.getName());
file.setWritersCanShare(false);
setViewersCanCopyContent文件(false);
Map appProperties=new HashMap();
put(TEMPLATE\u CODE\u PROP\u NAME,TEMPLATE.getCode());
setAppProperties文件(appProperties);
driveService.files().copy(template.getFileId(),文件)
.setFields(“id、名称、appProperties、webViewLink、iconLink、mimeType”)
.队列(批处理、回调);
}
成功执行批处理后处理响应:

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();
    }           
};
JsonBatchCallback callback=new JsonBatchCallback(){
@凌驾
public void onSuccess(文件、HttpHeaders和responseHeaders)引发IOException{
log.info(“成功复制文件-”+file.getName()+“”+file.getId());
}
@凌驾
public void onFailure(GoogleJsonError e、HttpHeaders responseHeaders)引发IOException{
log.severe(“未能复制文件”+e.getCode()+“”+e.getMessage());
抛出新异常();
}           
};
我遵循了谷歌推荐的最佳实践:

  • 设置响应中所需的字段,以便获得部分响应而不是完整响应
  • 使用批处理调用API

  • API需要7秒钟来完成这个简单的任务。从用户体验的角度来看,这是一个非常糟糕的性能我想知道这是预期的延迟还是我在这里做错了什么?

    您的代码很好。谷歌驱动API有时确实很慢。在这种情况下,唯一的解决方案是在需要超过阈值时间的情况下向API发出新请求。在大多数情况下,新请求将快速返回数据


    关于堆栈溢出的一些回答说,不是驱动API,而是开发人员的代码使代码变慢,根据我个人的经验,这是不正确的。(我已经检查了Chrome开发工具,API的响应时间通常在20-30秒之间)。

    您可以尝试检查驱动器API的响应时间,它涵盖了一些可用于提高应用程序性能的技术。它还介绍了如何使用gzip和部分响应。有关更多信息,请查看此相关链接。我已经查看了这些链接,并且已经在遵循这些最佳实践。我引用的延迟数字是在实施了最佳实践之后得出的。