使用 Google App Engine 将大文件上传到 Google 云存储

Upload large files to Google Cloud Storage using Google App Engine

我想将最大 1GB 的文件上传到 Google 云存储。我正在使用 Google App Engine Flexible。据我了解,GAE 对文件上传有 32MB 的限制,这意味着我必须直接上传到 GCS 或将文件分成块。

几年前的 answer 建议使用 Blobstore API,但是 Node.js 似乎没有选项,文档还建议使用 GCS 而不是用于存储文件的 Blobstore。

经过一些搜索后,似乎使用签名网址直接上传到 GCS 可能是最好的选择,但我无法找到有关如何执行此操作的任何示例代码。这是最好的方法吗?是否有任何示例说明如何使用带有 Node.js 的 App Engine 来做到这一点?

您最好的选择是使用 Node.js 的 Cloud Storage 客户端库来创建 resumable upload

这里是关于如何创建会话 URI 的 official 代码示例:

const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
const myBucket = storage.bucket('my-bucket');

const file = myBucket.file('my-file');
file.createResumableUpload(function(err, uri) {
  if (!err) {
    // `uri` can be used to PUT data to.
  }
});

//-
// If the callback is omitted, we'll return a Promise.
//-
file.createResumableUpload().then(function(data) {
  const uri = data[0];
});

编辑:现在看来您可以使用 createWriteStream 方法执行上传,而不必担心创建 URL.