GCS:上传已签名的URL(在Node.js服务器上生成):在文件体中插入多部分头



上下文:

  • Node.js服务器部署在VPS实例中
  • Web应用程序客户端(Vue/Quasar,但并不重要(
  • 同时上传服务器大型文件(500Mb-1Gb(

步骤:

  1. 客户端向服务器请求已签名的url,节点处理请求并返回url
  2. 客户端上传到第1点提供的目的地
  3. 结果是:
------WebKitFormBoundaryu22DC1TykLzIabb9
Content-Disposition: form-data; name="pozitie corecta obiecte.jpg"; filename="pozitie corecta obiecte.jpg"
Content-Type: image/jpeg
ÿØÿáLExi.... normal jpg file

因此,GCS还包括多部分流的报头。

我该怎么解决?

已解决。首先从签名功能中删除contentType道具:

async function generateV4UploadSignedUrl(fileName) {
// These options will allow temporary uploading of the file with outgoing
// Content-Type: application/octet-stream header.
const options = {
version       : 'v4',
action        : 'write',
expires       : Date.now() + 30 * 60 * 1000 // 30 minutes
// contentType: 'application/octet-stream', // delete this line!!!
};

然后在客户端添加您正在上传的文件的内容类型:

headers: [{
name : 'Content-Type',
value: files[0].type
}]

就是这样,一切都好。