the*_*ler 5 java google-cloud-storage google-cloud-sdk
我正在尝试使用他们的Java SDK从Google Cloud Storage下载一个字节范围。
我可以像这样下载整个文件。
Storage mStorage; // initialized and working
Blob blob = mStorage.get(pBucketName, pSource);
try (ReadChannel reader = mStorage.reader(blob.getBlobId())) {
// read bytes from read channel
}
Run Code Online (Sandbox Code Playgroud)
如果我愿意,我可以ReadChannel#seek(long)直到达到所需的起始字节,然后从该点下载一个范围,但这似乎效率低下(尽管我不知道实现中到底发生了什么。)
理想情况下,我想指定Google Cloud Storage REST API 中所示的Range: bytes=start-end标头,但我不知道如何在 Java 中设置标头。
如何在 Java SDK Storage get 调用中指定字节范围,或指定标头,以便我可以有效地下载所需的字节范围?
小智 -1
这是读取对象内容的一个很好的例子。在这个链接中有更多代码解决方案:
/**
* Example of reading a blob's content through a reader.
*/
// [TARGET reader(String, String, BlobSourceOption...)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_blob_name"]
public void readerFromStrings(String bucketName, String blobName) throws IOException {
// [START readerFromStrings]
try (ReadChannel reader = storage.reader(bucketName, blobName)) {
ByteBuffer bytes = ByteBuffer.allocate(64 * 1024);
while (reader.read(bytes) > 0) {
bytes.flip();
// do something with bytes
bytes.clear();
}
}
// [END readerFromStrings]
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1255 次 |
| 最近记录: |