[英]Unable to delete Firebase Storage Image with Cloud Function
Changed Storage path from "yourmaps" to "users" for this question将此问题的存储路径从“yourmaps”更改为“users”
Attempting to delete an image from Firebase Storage.
尝试从 Firebase 存储中删除图像。 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.删除用户帐户后,会将用户 uuid 传递到云 function。 i am attempting to delete the user image which has the same uuid in firebase Storage.
我正在尝试删除 firebase 存储中具有相同 uuid 的用户图像。
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. Firebase 存储日志中的错误消息。 Path is correct however there is no object...
路径是正确的,但是没有 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.在 Firebase 存储“监控规则”中,我可以看到我的尝试是允许的。 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)then
的版本(不使用 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
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()
.也就是说,不需要像你这样声明bucket,你可以直接用
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"
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.