问题
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