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

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

问题描述

我正在尝试从我的移动混合应用程序 (Ionic 3) 将图片发送到我的 Heroku 后端 (Node.js) 并让后端将图片上传到 Firebase 存储并将新上传的 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.

请记住,我正在使用适用于 Node.js 的 Firebase Admin SDK.

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

我有两个主要问题:

  1. 上传成功,但是当我转到 Firebase 存储控制台时,尝试显示图片预览时出现错误,并且下载时无法从计算机打开它.我猜这是一个编码的东西......?
  2. 如何找回新上传的文件下载地址?我期望在 .on('finish) 中返回一个对象,就像在 upload() 函数中一样,但没有返回任何对象(文件未定义).如何检索此 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天全站免登陆