我想在我的firebase云功能中上传files(images)
。阅读了很多帖子之后,我发现我能做到这一点的唯一方法是使用Google Cloud Platform (GCP) API
,这是我的代码:
const storage = require('@google-cloud/storage')();
var bucket = storage.bucket('myBucketName');
var buffer = bufferRepresentMyImage;
// what next?
我从GCP's API
找到了以下代码:
bucket.upload(filename, (err, file) => {
// code goes here
});
然而,它似乎不会起作用,因为它从不要求文件的数据,相反,callback function
返回file
。谁能告诉我如何用image
上传bucket
?
远程目标文件名包含在选项参数中。你的例子中的filename
(和这个答案)是本地文件名路径。
let options = {
destination: fileNameOnDestinationBucket,
metadata: { // (optional)
metadata: yourCustomMetadata,
contentType: 'image/jpeg' // (example)
}
};
// with Promise:
bucket.upload(filename, options).then((response)=>{
...
});
// with callback:
bucket.upload(filename, options, (err, metadata, response)=>{
....
});