为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

冉冉说 2021-06-27 18:14:11
我正在尝试将 HTML 文件写入 Google Cloud Storage,而不将文件保存在我的节点应用程序中。我需要创建一个文件并立即上传到云存储。我尝试使用fs.writeFileSync()方法,但它在我的应用程序中创建了文件,我不想要那样。const res = fs.writeFileSync("submission.html", fileData, "utf8"); GCS.upload(res, filePath);我希望直接在云存储中保存一个 HTML 文件。你能向我推荐正确的方法吗?
查看完整描述

1 回答

?
凤凰求蛊

TA贡献1825条经验 获得超4个赞

我相信的解决方案是使用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() {});


查看完整回答
反对 回复 2021-07-08
  • 1 回答
  • 0 关注
  • 277 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信