【发布时间】:2017-03-23 02:37:19
【问题描述】:
是否可以将数据流(上传)存储在谷歌云存储桶中并允许同时下载? 我尝试使用 Cloud API 使用以下代码将 100MB 文件上传到存储桶,但是在上传期间,我在 Google 云控制台中刷新存储桶,直到上传完成后我才能看到新的上传文件完成的。我想上传以 H.264 编码的实时视频以存储在云存储上,因此大小未知,同时其他用户可以开始下载它正在上传的文件事件。那么有可能吗?
Test code:
File tempFile = new File("StorageSample");
RandomAccessFile raf = new RandomAccessFile(tempFile, "rw");
try
{
raf.setLength(1000 * 1000 * 100);
}
finally
{
raf.close();
}
uploadFile(TEST_FILENAME, "text/plain", tempFile, bucketName);
public static void uploadFile(
String name, String contentType, File file, String bucketName)
throws IOException, GeneralSecurityException
{
InputStreamContent contentStream = new InputStreamContent(
contentType, new FileInputStream(file));
// Setting the length improves upload performance
contentStream.setLength(file.length());
StorageObject objectMetadata = new StorageObject()
// Set the destination object name
.setName(name)
// Set the access control list to publicly read-only
.setAcl(Arrays.asList(
new ObjectAccessControl().setEntity("allAuthenticatedUsers").setRole("READER"))); //allUsers//
// Do the insert
Storage client = StorageFactory.getService();
Storage.Objects.Insert insertRequest = client.objects().insert(
bucketName, objectMetadata, contentStream);
insertRequest.getMediaHttpUploader().setDirectUploadEnabled(false);
insertRequest.execute();
}
【问题讨论】: