Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.4k views
in Technique[技术] by (71.8m points)

typescript - firebase function get download url after successfully save image to firebase cloud storage

I'm having problem getting download url in firebase function after saving image to cloud storage. Below is my firebase http function in typescript to save base64 string to jpeg in firebase cloud storage. when i log result, it always empty. I follow this link to get download url "Get Public URL from file uploaded with firebase-admin". But it give me following error:"SigningError: Identity and Access Management (IAM) API has not been used in project 81673989436 before or it is disabled." And couldn't find simple example to follow. My plan is to update my firestore table, once uploaded to cloud storage.

export const publishImage = functions.https.onRequest((req, res) => {
// Convert the base64 string back to an image to upload into the Google 
// Cloud Storage bucket
const base64EncodedImageString=req.body.image.replace(/^data:image/jpeg;base64,/, "");
const mimeType = 'image/jpeg';
const randomFileName = crypto.randomBytes(16).toString('hex') + '.jpeg';    
const imageBuffer = new Buffer(base64EncodedImageString, 'base64');

const bucket = admin.storage().bucket();

// Upload the image to the bucket
const file = bucket.file('images/' + randomFileName);
file.save(imageBuffer, {
    metadata: { contentType: mimeType },
}).then(result => {
    console.log(result);
    file.makePublic();
    file.getSignedUrl({
        action: 'read',
        expires: '03-09-2491'
    }).then(urls => {
        console.log(urls[0]);
    })
});
return res.status(200).send("NOT WORKING");    
})

The error is as follow in firebase console. Even with error, I can save image in storage.

SigningError: Permission iam.serviceAccounts.signBlob is required to perform this operation on service account projects/vmsystem-4aa54/serviceAccounts/[email protected]. at /user_code/node_modules/@google-cloud/storage/src/file.js:1715:16 at Request._callback (/user_code/node_modules/@google-cloud/storage/node_modules/google-auto-auth/index.js:356:9) at Request.self.callback (/user_code/node_modules/@google-cloud/storage/node_modules/request/request.js:186:22) at emitTwo (events.js:106:13) at Request.emit (events.js:191:7) at Request. (/user_code/node_modules/@google-cloud/storage/node_modules/request/request.js:1163:10) at emitOne (events.js:96:13) at Request.emit (events.js:188:7) at IncomingMessage. (/user_code/node_modules/@google-cloud/storage/node_modules/request/request.js:1085:12) at IncomingMessage.g (events.js:292:16)

How can i get download url?

I am getting confused. Now, I am stuck at updating function after changes. Below is my initialization for admin.

 import * as admin from 'firebase-admin';
 const serviceAccount = require('./../service_account.json');

 try {   
 // admin.initializeApp(functions.config().firebase); // initial
 admin.initializeApp({
     credential: admin.credential.cert(serviceAccount),
     databaseURL: "https://vmsystem-4aa54.firebaseio.com"
 });
 } catch(e) {}
 import * as simpletest from './simpletest';
 export const testsomething = simpletest.helloWorld;

first

second

third

fourth

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...