【问题标题】:how to upload file line by line to google cloud storage in java如何在java中逐行上传文件到谷歌云存储
【发布时间】:2018-09-05 16:47:41
【问题描述】:

我有以下代码:

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

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

【问题讨论】:

    标签: java google-cloud-storage google-cloud-sdk


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-11
      • 1970-01-01
      • 1970-01-01
      • 2019-03-19
      • 2021-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多