如何逐行上传文件到 google 云存储 java

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

我有以下代码:

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

因为我是逐行上传数据,所以每次都是把之前的数据去掉,while循环结束,GCS里面的文件只有最后一行。

如何逐行上传并将其附加到现有数据?

我设法使用管道而不是队列来解决它:

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

    }