使用 Firebase 函数将发布的表单图像缓冲区上传到 Cloud Storage

cer*_*lex 3 google-cloud-storage firebase google-cloud-functions

这是我的云功能。它应该获取一个 http 发布的图像并将其上传到存储,返回 url。

exports.uploadImageToEditor = functions.https.onRequest((req, res) => {
        const img = JSON.parse(JSON.stringify(req.body));
        const bucket = admin.storage().bucket();

        return bucket.file('blog/foo.jpg').save(img.data, { 
          resumable: false, 
          metadata: { 
            contentType: 'image/jpeg' 
          } 
        })
          .then(() => {
              return cors(req, res, () => {
                  res.status(200).send({ "url": bucket.file('foo.jpg').getSignedUrl()});
                });
            });
    });
Run Code Online (Sandbox Code Playgroud)

这是图像在客户端中实际发送的方式:

uploadImage(file, endPoint) {
        if (!endPoint) {
            throw new Error('Image Endpoint isn`t provided or invalid');
        }
        const                  formData = new FormData();
        if (file) {
            formData.append('file', file);
            const                  req = new HttpRequest('POST', endPoint, formData, {
                reportProgress: true
            });
            return this._http.request(req);
        }
        else {
            throw new Error('Invalid Image');
        }
    }
Run Code Online (Sandbox Code Playgroud)

Mic*_*igh 5

我认为您可能正在Admin SDK 中寻找save()方法File

const bucket = admin.storage().bucket()
  .file('my-file.jpg').save(blob)
  .then(() => { /* ... */ });
Run Code Online (Sandbox Code Playgroud)