如何将图像从URL上传到Firebase Storage?

时间:2019-02-21 20:37:53

标签: node.js firebase google-cloud-storage firebase-storage firebase-admin

我正在使用Node.JS。 我似乎无法弄清楚。我已经更新了模块。
旧代码:

var mimeType = 'image/jpeg';
var randomString = uuidv4();
var options = {
    destination: 'test',
    metadata: { 
        contentType: mimeType,
        metadata: {
            firebaseStorageDownloadTokens: randomString
        }
    }
};
return admin.storage().bucket().upload(userPhotoDownloadLink, options).then(()=>{
    //
}).catch((err) => {
    print(`Error Uploading Image:${err}`);
    //
});  

此代码不再起作用。如何通过http链接上传。 Google Cloud 2.4.x仅显示有关本地文件上传的指南。

编辑:

var uploadPhoto = async function(public_courier_id,userPhotoDownloadLink){
var mimeType = 'image/jpeg';
var randomString = "testkey123";
var options = {
    //destination: 'courierPhotos/' + public_courier_id,
    metadata: { 
        contentType: mimeType,
        metadata: {
            firebaseStorageDownloadTokens: randomString
        }
    }
};
const file = admin.storage().bucket().file(`courierPhotos/${public_courier_id}`)
console.log(`Fetching File...`);
return fetch(userPhotoDownloadLink).then(res=>{
    console.log(`Successfully Fetched, Piping File...`)
    return new Promise((resolve,reject)=>{
        res.body.pipe(file.createWriteStream(options))
        .on('error', function(err) {
            reject(`err: ${err}`)
        })
        .on('finish', function() {
            resolve(`complete`);
        });
    })
});
}

return uploadPhoto("TESTUID","https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png")
.then(res => console.log(`Printing after function ${res}`))
.catch(e => console.log(e));

0 个答案:

没有答案