简体   繁体   English

使用Firebase功能将图像上传到Cloud Storage错误

[英]Image Uploading to Cloud Storage with firebase functions Error

I'm trying to upload a base64 jpeg image to Firebase Storage I don't know what's wrong. 我正在尝试将base64 jpeg图像上传到Firebase存储,我不知道怎么了。 I don't think the error is from my image encoding. 我认为错误不是来自我的图像编码。 I'm trying to upload an image from the user's phone to the firebase storage using React Native (fetch API) to send the request. 我正在尝试使用React Native(fetch API)将用户手机中的图像上传到Firebase存储以发送请求。

index.js file: index.js文件:

const functions = require("firebase-functions");
const cors = require("cors")({ origin: true });
const fs = require("fs");
const UUID = require("uuid-v4");

const gcconfig = {
projectId: "awsome-34343434343434",
keyFileName: "awsome.json"
};

 const { Storage } = require("@google-cloud/storage");
const storage = new Storage(gcconfig);

exports.storeImage = functions.https.onRequest((request, response) => {
cors(request, response, () => {
console.log("THE BODY:" + JSON.stringify(request.body));

const body = JSON.parse(request.body);
console.log("THE IMAGE:" + body.image);
fs.writeFileSync("/tmp/uploaded-image.jpg", body.image, "base64", err => {
  console.log(err);
  return response.status(500).json({ error: err });
});
const bucket = storage.bucket("awsome-1548769019561");
const uuid = UUID();
console.log("UUID: " + uuid);
bucket.upload(
  "/tmp/uploaded-image.jpg",
  {
    uploadType: "media",
    destination: "/places/" + uuid + ".jpg",
    metadata: {
      metadata: {
        contentType: "image/jpeg",
        firebaseStorageDownloadTokens: uuid
      }
    }
  },
  (err, file) => {
    if (!err) {
      response.status(201).json({
        imageUrl:
          "https://firebasestorage.googleapis.com/v0/b/" +
          bucket.name +
          "/o/" +
          encodeURIComponent(file.name) +
          "?alt=media&token=" +
          uuid
      });
    } else {
      console.log(err);
      response.status(500).json({ error: err });
    }
    }
  );
});
});

the request body I sent : 我发送的请求正文:

{"image": "/9j/4QG1RXhpZgAATU0AKgAAAAgABwEQA..."}

I get this error: 我收到此错误:

SyntaxError: Unexpected token o in JSON at position 1
at Object.parse (native)
at cors (/user_code/index.js:21:24)
at cors (/user_code/node_modules/cors/lib/index.js:188:7)
at /user_code/node_modules/cors/lib/index.js:224:17
at originCallback (/user_code/node_modules/cors/lib/index.js:214:15)
at /user_code/node_modules/cors/lib/index.js:219:13
at optionsCallback (/user_code/node_modules/cors/lib/index.js:199:9)
at corsMiddleware (/user_code/node_modules/cors/lib/index.js:204:7)
at exports.storeImage.functions.https.onRequest (/user_code/index.js:18:3)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/providers/https.js:57:9)

Your error is being caused by the line const body = JSON.parse(request.body); 您的错误是由以下行引起的: const body = JSON.parse(request.body);

Firebase automatically parses data from the request body, there's no need for you to try to do it yourself, see the section " Read values from the request " in the function calls docs . Firebase会自动分析来自请求正文的数据,您无需自己尝试进行操作,请参阅函数调用docs中的“ 从请求中读取值 ”部分。 Since your body is already in JSON format, and your function is trying to re-JSON-ify it... then it triggers that error. 由于您的身体已经是JSON格式,并且您的函数正在尝试对其重新进行JSON修饰...因此它将触发该错误。 Take out that line and you shouldn't get that error anymore. 删除那条线,您不应该再收到该错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM