如何将文件上传到多个块的谷歌存储?

时间:2017-03-27 10:31:27

标签: google-cloud-storage chunks pre-signed-url

我正在尝试将文件上传到谷歌存储多个块但签名的URL。

以下是我要遵循的步骤:

  1. 使用Node.js创建可恢复的上传URL。
  2. 示例网址:https://www.googleapis.com/upload/storage/v1/b/MYBUCKET/o?name=sdk2.txt&uploadType=resumable&upload_id=AEnB2UrmFY9CKwkHFxsYdT71LcWa0zUVjG6iYfr9mHiwsIaG3c1goFiVURUPz6KFTIhtywndse00XaOkH7ftXPf82S7bNVRhPg

    1. 启动可恢复的上传会话:
    2. POST https://www.googleapis.com/upload/storage/v1/b/MYBUCKET/o?name=sdk2.txt&uploadType=resumable&upload_id=AEnB2UrmFY9CKwkHFxsYdT71LcWa

      使用这些标题:

      Content-Type:text/plain
      name:sdk2.txt
      Content-Length:0
      x-goog-resumable:start
      

      HTTPS响应:200 OK

      1. 发送第一个块:
      2. PUT https://www.googleapis.com/upload/storage/v1/b/MYBUCKET/o?name=sdk2.txt&uploadType=resumable&upload_id=AEnB2UrmFY9CKwkHFxsYdT71LcWa0zUVjG6iYfr9mHiwsIaG3c1goFiVURUPz6KFTIhtywndse00XaOkH7ftXPf82S7bNVRhPg

        使用这些标题:

        Content-Type:text/plain
        name:sdk2.txt
        Content-Length: 262144
        Content-Range: bytes 0-262143/331886 
        

        HTTPS响应:200 OK包含数据:

         {
          "kind": "storage#object",
          "id": "MYBUCKET/sdk2.txt/1490608714382301",
          "selfLink": "https://www.googleapis.com/storage/v1/b/testupload-sdk/o/sdk2.txt",
          "name": "sdk2.txt",
          "bucket": "MYBUCKET",
          "generation": "1490608714382301",
          "metageneration": "1",
          "timeCreated": "2017-03-27T09:58:34.164Z",
          "updated": "2017-03-27T09:58:34.164Z",
          "storageClass": "MULTI_REGIONAL",
          "timeStorageClassUpdated": "2017-03-27T09:58:34.164Z",
          "size": "0",
          "md5Hash": "1B2M2Y8AsgTpgAmY7PhCfg==",
          "mediaLink": "https://www.googleapis.com/download/storage/v1/b/MYBUCKET/o/sdk2.txt?generation=1490608714382301&alt=media",
          "crc32c": "AAAAAA==",
          "etag": "CN33rZe29tICEAE="
        }
        

        根据find command,上述回复应为308,而不是200 OK

        这些API调用我做错了什么?

1 个答案:

答案 0 :(得分:0)

不需要步骤2。如果将步骤2替换为步骤3,则响应将按预期返回308而不是200

x-goog-resumable仅适用于XML API,因此在使用JSON API时应将其省略。

在步骤1中,恢复了可恢复上传。 GCS在初始化上传时会生成上传ID,因此在初始化上传之前无法获得上传ID。

第2步使用0字节对象完成了可恢复的上传,因为GCS将其解释为要上传的数据(由于第1步初始化了上传)。结果,第3步从第2步返回了响应。如果可恢复上传完成,则GCS将从创建对象的请求中返回响应。