Changed Storage path from "yourmaps" to "users" for this question Attempting to delete an image from Firebase Storage. I have read similar stack overflow questions and attempted to solve this for days without effort.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const {Storage} = require('@google-cloud/storage');
admin.initializeApp();
A user uuid is passed to the cloud function when a users account has been deleted. i am attempting to delete the user image which has the same uuid in firebase Storage.
exports.storageImageDelete = functions.firestore
.document('user/{userId}')
.onDelete(async (snapshot, context) => {
const userId = context.params.userId
const path = `users/${userId}.jpg`;
const bucketName = 'xxxxxxx.appspot.com/'
const storage = new Storage();
return storage.bucket(bucketName).file(path).delete()
.then(function () {
console.log(`File deleted successfully in path: ${path}`)
})
.catch(function (error) {
console.error(storage.bucket(bucketName).file(path).delete())
console.info(storage.bucket(bucketName).file(path).delete())
console.log(`File NOT deleted: ${path}`)
})
});
Error message in Firebase Storage Log. Path is correct however there is no object...
Error: No such object: xxxxxx.appspot.com/users/XXXXXXXXXX-XXXX-XXXX-XXXXXXXXXXX.jpg
In Firebase Storage "Monitor rules" i can see that my attempts are allowed. Any help would be appreciated. What am i missing?
Since it is your default bucket, the following should do the trick (untested):
then
(not using async/await)const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.storageImageDelete = functions.firestore
.document('user/{userId}')
.onDelete((snapshot, context) => {
const userId = context.params.userId
const path = `users/${userId}.jpg`;
const bucket = admin.storage().bucket();
return bucket.file(path).delete()
.then(() => {
console.log(`File deleted successfully in path: ${path}`)
return null;
})
.catch(error => {
console.log(`File NOT deleted: ${path}`);
return null;
})
});
async
/ await
exports.storageImageDelete = functions.firestore
.document('user/{userId}')
.onDelete(async (snapshot, context) => {
try {
const userId = context.params.userId
const path = `users/${userId}.jpg`;
const bucket = admin.storage().bucket();
await bucket.file(path).delete();
console.log(`File deleted successfully in path: ${path}`)
return null;
} catch (error) {
console.log(`File NOT deleted: ${path}`);
console.log(error);
return null;
}
});
In other words, no need to declare the bucket as you do, you can directly declare it with admin.storage().bucket()
.
I finally solved my issue. Here is the missing code that was required to correctly find my bucket.
admin.initializeApp({
databaseURL: "https://xxxxxxxx.firebaseio.com",
storageBucket: "xxxxxxxx.appspot.com"
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.