如何将文件逐行上传到Java中的Google云存储 [英] how to upload file line by line to google cloud storage in java

查看:417
本文介绍了如何将文件逐行上传到Java中的Google云存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

 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中的文件仅包含最后一行.

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?

推荐答案

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

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

    }

这篇关于如何将文件逐行上传到Java中的Google云存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆