简体   繁体   English

如何使用Java API在一个调用中将多个文件上传到Google Cloud Storage?

[英]How to upload multiple files to Google Cloud Storage in a single call using Java API?

We want to upload multiple files to Google Cloud Storage. 我们想将多个文件上传到Google Cloud Storage。 Currently, we are uploading one by one using the Google Java API. 目前,我们正在使用Google Java API逐一上传。 The code is below: 代码如下:

public void uploadFile(File srcFile,String bucketName, String destPath) throws IOException {
      BlobId blobId = BlobId.of(bucketName, srcFile.getName());
      BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();

  long startTime = System.currentTimeMillis();

 // Blob blob = storage.create(blobInfo,new FileInputStream(srcFile));

  try (WriteChannel writer = storage.writer(blobInfo)) {
    try (FileInputStream in = new FileInputStream(srcFile)){


        byte[] buffer = new byte[1024 * 1024 * 100] ;

        writer.setChunkSize(buffer.length);

        int readSize = 0;

        while((readSize = in.read(buffer)) > 0) {
             writer.write(ByteBuffer.wrap(buffer, 0, readSize));
        }

        long endTime = System.currentTimeMillis();

      double writeTime = (double)(endTime - startTime) / 1000;

      System.out.println("File write time : " + writeTime);

    }
  }


}

Our application wants to upload multiple files at a time. 我们的应用程序希望一次上传多个文件。 I tried to find a method to upload multiple files in the Java API, but could not find the any method to upload multiple files using a single call. 我试图找到一种在Java API中上载多个文件的方法,但找不到使用单个调用上载多个文件的任何方法。

If I loop and upload using multiple calls, it is adding a huge network overhead and performance is very slow, which the application cannot afford. 如果我使用多个调用循环并上载,则会增加巨大的网络开销,并且性能非常慢,这是应用程序无法承受的。

My questions are: 我的问题是:

  1. Is it possible to upload multiple files via a single call, either using the Java API or REST API? 是否可以通过一次调用使用Java API或REST API上传多个文件?
  2. Could you please provide an example? 你能举个例子吗?

Neither the google-cloud-java library nor the underlying JSON API has an API call to upload multiple files, but you could accomplish what you're trying to do by spawning multiple threads and having each thread upload a file. google-cloud-java库和底层JSON API都没有API调用来上传多个文件,但是您可以通过产生多个线程并让每个线程上传一个文件来完成您要执行的操作。 The gsutil -m option does it this way. gsutil -m选项采用这种方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM