如何将文件直接写入 Google Cloud Storage 而无需将其保存在我的应用程序中?

Ola*_*aac 2 javascript node.js google-cloud-storage

我正在尝试将 HTML 文件写入 Google Cloud Storage,而不将文件保存在我的节点应用程序中。我需要创建一个文件并立即上传到云存储。我尝试使用fs.writeFileSync()方法,但它在我的应用程序中创建了文件,我不想要那样。

const res = fs.writeFileSync("submission.html", fileData, "utf8");
GCS.upload(res, filePath);
Run Code Online (Sandbox Code Playgroud)

我希望直接在云存储中保存一个 HTML 文件。你能向我推荐正确的方法吗?

小智 5

我相信的解决方案是使用file.savewraps的方法createWriteStream,所以这应该有效。

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

const file = myBucket.file('my-file');
const contents = 'This is the contents of the file.';

file.save(contents, function(err) {
  if (!err) {
    // File written successfully.
  }
});

//-
// If the callback is omitted, we'll return a Promise.
//-
file.save(contents).then(function() {});
Run Code Online (Sandbox Code Playgroud)