如何使用服务器上的节点从客户端 JavaScript 上传到 Google 云存储?

How do I upload to Google Cloud Storage from client-side JavaScript, with Node on the server?

我想从我的客户端 JavaScript 代码直接上传到 Google Cloud Storage 存储桶,尽管该存储桶不应公开写入。服务器端代码是 Node.js。过去,通过在服务器上为客户端生成临时授权,我能够直接上传到 Amazon S3。将文件上传到写保护的 Google Cloud Storage 存储桶的过程是什么,即需要授权?

我发现我可以使用 getSignedUrl,它应该允许对文件进行某些临时访问(例如写入):

bucket = gcs.bucket('aBucket')
bucket.file('aFile').getSignedUrl({
  action: 'write',
  expires: moment.utc().add(1, 'days').format(),
}, (error, signedUrl) => {
  if (error == null) {
    console.log(`Signed URL is ${signedUrl}`)
  }
})

为了上传文件,向获得的签名URL发出PUT请求。