2016-11-09 25 views
2

是否可以将数据流(Upload)存储在Google云存储桶中并允许同时下载? 我试图使用Cloud API上传一个100MB文件到存储桶,但使用下面的代码,但在上传期间,我刷新了Google云端控制台中的存储桶,直到上传时才能看到新的上传文件完成。我想上传用H.264编码的实时视频以存储在云存储上,因此其大小未知,与此同时,其他用户可以开始下载正在上传的文件事件。那有可能吗?是否可以将数据流(Upload)存储在Google云存储桶中并允许同时下载?

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

回答

0

不幸的是这是不可能的,因为状态documentation中:

对象是不可变的,这意味着一个上传的对象不能在其整个寿命存储变化 。对象的存储生命周期 是成功创建对象(上载)和成功删除对象 之间的时间。

这意味着云存储中的一个对象在上传完成后开始存在,因此只有上传完成后才能访问该对象。

+0

感谢您的回复。所以你有什么建议我可以做我期望的功能吗?谢谢 –

+0

一个可能的解决方案,将用特定的命名约定将大对象分成更小的块。通过这种方式,一旦小对象被上传,用户就可以开始流式传输内容。您将需要处理智能缓冲和对不同对象的迭代,以便为用户提供流畅的体验。 –

相关问题