【问题标题】:Node.js upload images to firebase storageNode.js 将图像上传到 Firebase 存储
【发布时间】:2018-06-10 13:05:11
【问题描述】:

我正在尝试在我的 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。谁能告诉我如何用bucket上传image

【问题讨论】:

标签: node.js firebase google-cloud-platform google-cloud-functions


【解决方案1】:

远程目标文件名包含在选项参数中。 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)=>{
....
});

【讨论】:

    猜你喜欢
    • 2017-09-25
    • 2018-10-24
    • 2019-01-22
    • 2022-11-11
    • 2020-07-10
    相关资源
    最近更新 更多