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;
You're not showing all the code here (for example, how you initialized the Firebase Admin SDK to create admin
). So I'm going to assume that you used the project default credentials to initialize it like this:
import * as admin from 'firebase-admin'
admin.initializeApp()
This isn't sufficient to to be able to use getSignedUrl when reaching into the admin SDK to use Cloud Storage APIs. If you want to use getSignedUrl, you'll need to provide the credentials for a dedicated service account that you create in the console.
Assuming that you put those credentials in a file called yourServiceAccount.json in your functions folder, you should initialize the admin SDK with those credentials like this:
import * as serviceAccount from 'yourServiceAccount.json';
const adminConfig = JSON.parse(process.env.FIREBASE_CONFIG)
adminConfig.credential = admin.credential.cert(<any>serviceAccount)
admin.initializeApp(adminConfig);
Note that this is just taking the default project config from FIREBASE_CONFIG and adding the service account to them.
In order to get the import of a JSON file, you will also need to have a file (called typings.d.ts):
declare module "*.json" {
const value: any;
export default value;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With