3

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

以下是我正在遵循的步骤:

  1. 使用 Node.js 创建可恢复上传 URL。

示例网址:https://www.googleapis.com/upload/storage/v1/b/MYBUCKET/o?name=sdk2.txt&uploadType=resumable&upload_id=AEnB2UrmFY9CKwkHFxsYdT71LcWa0zUVjG6iYfr9mHiwsIaG3c1goFiVURUPz6KFTIhtywndse00XaOkH7ftXPf82S7bNVRhPg

  1. 启动可恢复的上传会话:

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. 发送第一个块:

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="
}

根据文档,上述响应应该是308而不是200 OK

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

4

1 回答 1

0

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

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

在第 1 步中,可恢复上传已初始化。GCS在初始化上传时会生成上传ID,所以在初始化上传之前不可能有上传ID。

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

于 2020-01-02T20:49:28.450 回答