how to upload file line by line to google cloud storage in java

早过忘川 提交于 2020-02-04 01:20:25

问题


I have the following code:

 String fullFileUrl = fileUrl;
 Storage storage = StorageServiceHolder.getStorage();
 BlobId blobId = GCSHelper.uri2blobId(fullFileUrl);

 while (!queue.isEmpty()) {
     try {
         String line = queue.poll(1000, TimeUnit.SECONDS);
         InputStream inputStream = new ByteArrayInputStream(line.getBytes());

         BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
         Blob blob = storage.create(blobInfo, inputStream);

      } catch (InterruptedException e) {
          log.error("Get interrupt while writing to Goolge Cloud Storage" + e.getMessage(), e);
 }

Because I upload the data line by line, each time, the previous data is removed and it the end the while loop, the file in GCS contains only the last line.

How can I upload it line by line and append it to the existing data?


回答1:


I managed to solve it using pipe instead of the Queue:

    private PipedInputStream inStream;
    inStream = .....


    String fullFileUrl = fileUrl;
    Storage storage = StorageServiceHolder.getStorage();
    BlobId blobId = GCSHelper.uri2blobId(fullFileUrl);

    BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
    Blob blob = storage.create(blobInfo, inStream);

    }


来源:https://stackoverflow.com/questions/49510128/how-to-upload-file-line-by-line-to-google-cloud-storage-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!