首页 文章

是否可以将数据流(上传)存储到Google Cloud 存储桶中并允许同时下载?

提问于
浏览
2

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

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

1 回答

  • 0

    不幸的是,这是不可能的,如_2898704中的状态:

    对象是不可变的,这意味着上载的对象在其整个存储生命周期内无法更改 . 对象的存储生命周期是成功创建对象(上载)和成功删除对象之间的时间 .

    这意味着 Cloud 存储中的对象在上传完成后开始存在,因此在上传之前您无法访问该对象 .

相关问题