【问题标题】:Is it possible to stream data(Upload) to store on bucket of Google cloud storage and allow to download at the same time?是否可以将数据流(上传)存储在谷歌云存储桶中并允许同时下载?
【发布时间】: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();
}

【问题讨论】:

    标签: google-cloud-storage


    【解决方案1】:

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

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

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

    【讨论】:

    • 感谢您的回复。那么您对我如何完成我期望的功能有什么建议吗?谢谢
    • 一个可能的解决方案是将您的大对象划分为具有特定命名约定的较小块。这样,一旦上传了一个小对象,用户就可以开始内容的流式传输。您将需要处理不同对象的智能缓冲和迭代,以便为用户提供流畅的体验。
    猜你喜欢
    • 2015-03-01
    • 2016-12-16
    • 2021-06-15
    • 2019-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-01
    相关资源
    最近更新 更多