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

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

const res = fs.writeFileSync("submission.html", fileData, "utf8");
GCS.upload(res, filePath);

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


冉冉说
浏览 279回答 1
1回答

凤凰求蛊

我相信的解决方案是使用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() {});
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript