3

We are using Google Drive v3 API to manage documents in our web application. We have a simple use case in which the user clicks on a button and the backend needs to copy about 5-10 files from source to destination folder. I have tested with 6 files in the source folder and the API took about 7 seconds. I have used batching to invoke the copy file API. Following is the code for the same:

Adding requests to the Queue:

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);
}

Handle response after the batch is executed successfully:

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();
    }           
};

I have followed the best practices recommended by Google:

  1. Set fields that are required in the response so that we get partial response instead of complete response
  2. Use batching for invoking the API

The API is taking 7 seconds to complete this simple task. This is a very bad performance from user experience perspective. I would like to know if this is the expected delay or am I doing something wrong here ?

Mithun
  • 7,747
  • 6
  • 52
  • 68
  • You can try to check the [Performance Tips](https://developers.google.com/drive/v3/web/performance) of the Drive API, it covers some techniques you can use to improve the performance of your application. It also covers how to use gzip and partial response. For more information, check this related [SO question](http://stackoverflow.com/questions/19159364/google-drive-api-too-slow). – KENdi Nov 11 '16 at 07:50
  • I have looked into those links and I am already following those best practices. The latency numbers that I have quoted are after implementing the best practices. – Mithun Nov 14 '16 at 13:17

1 Answers1

4

You code is fine. Google drive API is indeed slow sometimes. The only solution in this situation is to make a new request to the API if it takes more than a threshold time. In most cases, the new request will return the data quickly.

Some answers on stack overflow have said that it's not the drive API, but the developer's code which makes the code slow, which from my personal experience is incorrect. (I have checked on Chrome dev tools, and the response times for the API have often been in the range of 20-30 seconds).

ssy
  • 129
  • 2
  • 9