Node.js将图像上传到firebase存储

问题描述 投票:1回答:1

我想在我的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

node.js firebase google-cloud-platform google-cloud-functions
1个回答
2
投票

远程目标文件名包含在选项参数中。你的例子中的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)=>{
....
});
© www.soinside.com 2019 - 2024. All rights reserved.