将base64编码的jpeg上传到Firebase存储(Admin SDK)

时间:2022-03-04 15:25:07

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.

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

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

请注意,我正在使用适用于Node.js的Firebase Admin SDK。

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:

所以我将base64编码的图像发送到Heroku(我用在线base64解码器检查编码的字符串,它没关系),由以下函数处理:

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));
    });
  })
};

I have 2 major issues:

我有两个主要问题:

  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. 上传成功,但是当我访问Firebase存储控制台时,当我尝试显示图片的预览时出现错误,我下载时无法从计算机中打开它。我想这是编码的事情......?

  3. 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?
  4. 如何检索新上传的文件下载URL?我期待在.on('finish)中返回一个对象,就像在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.

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

1 个解决方案

#1


3  

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

我的问题是我在base64对象字符串的开头添加数据:image / jpeg; base64;我只需要删除它。

For the download url, I did the following:

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

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);
      });

#1


3  

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

我的问题是我在base64对象字符串的开头添加数据:image / jpeg; base64;我只需要删除它。

For the download url, I did the following:

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

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);
      });