将base64编码的jpeg上传到Firebase存储(Admin SDK) [英] Upload base64 encoded jpeg to Firebase Storage (Admin SDK)

查看:55
本文介绍了将base64编码的jpeg上传到Firebase存储(Admin SDK)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将图片从我的移动混合应用程序(Ionic 3)发送到我的Heroku后端(Node.js),并让后端将图片上传到Firebase Storage,然后将新上传的fil下载URL返回到移动应用

I am trying to send a picture from my mobile hybrid app (Ionic 3) to my Heroku backend (Node.js) and have the backend upload the picture to Firebase Storage and return the newly uploaded fil download url to the mobile app.

请记住,我正在使用Firebase Admin SDK for Node.js.

Keep in mind that I am using the Firebase Admin SDK for Node.js.

因此,我将base64编码的图像发送到Heroku(我通过在线base64解码器检查了编码的字符串,没关系),它由以下功能处理:

So I send the base64 encoded image to Heroku (I check the encoded string with an online base64 decoder and it is alright) which is handle by the following function:

const uploadPicture = function(base64, postId, uid) {
  return new Promise((resolve, reject) => {
    if (!base64 || !postId) {
      reject("news.provider#uploadPicture - Could not upload picture because at least one param is missing.");
    }

    let bufferStream = new stream.PassThrough();
    bufferStream.end(new Buffer.from(base64, 'base64'));

    // Retrieve default storage bucket
    let bucket = firebase.storage().bucket();

    // Create a reference to the new image file
    let file = bucket.file(`/news/${uid}_${postId}.jpg`);

    bufferStream.pipe(file.createWriteStream({
      metadata: {
        contentType: 'image/jpeg'
      }
    }))
    .on('error', error => {
      reject(`news.provider#uploadPicture - Error while uploading picture ${JSON.stringify(error)}`);
    })
    .on('finish', (file) => {
      // The file upload is complete.
      console.log("news.provider#uploadPicture - Image successfully uploaded: ", JSON.stringify(file));
    });
  })
};

我有2个主要问题:

  1. 上传成功,但是当我转到Firebase存储控制台时,尝试显示图片预览时出现错误,并且下载图片时无法从计算机上打开它.我想这是一种编码的事情....?
  2. 如何获取新上传的文件下载网址?我期望像upload()函数一样在.on('finish)中返回一个对象,但是没有返回任何对象(文件未定义).我该如何检索该URL以将其发送回服务器响应中?
  1. Upload succeeds but I when I go to Firebase Storage console, there is an error when I try to display the preview of the picture and I cannot open it from my computer when I download it. I guess it is an encoding thing....?
  2. How can I retrieve the newly uploaded file download url ? I was expecting an object to be returned in the .on('finish), like in the upload() function, but none is returned (file is undefined). How could I retrieve this url to send it back in the server response?

我想避免使用upload()函数,因为我不想在后端托管文件,因为它不是专用服务器.

I want to avoid using the upload() function because I don't want to host files on the backend as it is not a dedicated server.

推荐答案

我的问题是我在base64对象字符串的开头添加了data:image/jpeg;base64,;我只需要删除它.

My problem was that I add data:image/jpeg;base64,at the beginning of the base64 object string ; I just had to remove it.

对于下载网址,我执行了以下操作:

For the download url, I did the following:

const config = {
        action: 'read',
        expires: '03-01-2500'
      };
      let downloadUrl = file.getSignedUrl(config, (error, url) => {
        if (error) {
          reject(error);
        }
        console.log('download url ', url);
        resolve(url);
      });

这篇关于将base64编码的jpeg上传到Firebase存储(Admin SDK)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆