【发布时间】:2020-09-05 16:38:39
【问题描述】:
我正在尝试将文件上传到谷歌存储桶。该文件将使用 post 方法和 form-data 发送到 nodejs 后端路由。我已阅读有关上传文件的 google 存储文档。
我不知道我应该如何接收文件并将其分配给文件名变量,以便可以将其上传到谷歌存储桶。就是这样。
const {Storage} = require('@google-cloud/storage');
// Creates a client
const storage = new Storage();
test: async (req, res, next) => {
const filename = req.body.file
// const bucketName = 'Name of a bucket, e.g. my-bucket';
// const filename = 'Local file to upload, e.g./local/path/to/file.txt';
await storage.bucket(bucketName).upload(filename, {
gzip: true,
metadata: {
cacheControl: 'public, max-age=31536000',
},
});
console.log(`${filename} uploaded to ${bucketName}.`);
}
}
【问题讨论】:
-
文件上传后需要调用该函数。为此,您可以使用文件上传包。一个很好的是:github.com/react-dropzone/react-dropzone
-
实际上,我在 node.js 部分工作,我将使用 post 方法接收文件,并从请求部分将其上传到谷歌存储桶。
-
请参阅本教程then。你会得到上下文
-
我也看到了。事实上,我已经尝试在互联网上到处寻找。实际上,发送文件只是整个表单的一部分。我将收到 json +file。我会将文件保存在云存储中,该存储的 url 将与其他表单详细信息一起保存在我的 mongodb 集合中。
标签: node.js reactjs google-cloud-storage multer