无法使用 Cloud Function 删除 Firebase 存储映像
Unable to delete Firebase Storage Image with Cloud Function
已将此问题的存储路径从“yourmaps”更改为“users”
正在尝试从 Firebase 存储中删除图像。
我读过类似的堆栈溢出问题,并尝试了几天不费力地解决这个问题。
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const {Storage} = require('@google-cloud/storage');
admin.initializeApp();
当用户帐户被删除时,用户uuid 被传递到云函数。我正在尝试删除在 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}`)
})
});
Firebase 存储日志中的错误消息。路径正确但是没有对象...
Error: No such object: xxxxxx.appspot.com/users/XXXXXXXXXX-XXXX-XXXX-XXXXXXXXXXX.jpg
在 Firebase 存储的“监控规则”中,我可以看到我的尝试是被允许的。任何帮助,将不胜感激。我错过了什么?
因为它是您的默认存储桶,下面应该可以解决问题(未经测试):
带有 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
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;
}
});
也就是说,不需要像你那样声明bucket,直接用admin.storage().bucket()
声明就可以了。
我终于解决了我的问题。我需要在初始化应用程序时声明 storageBucket。现在我可以正确找到我的水桶了。
admin.initializeApp({
databaseURL: "https://xxxxxxxx.firebaseio.com",
storageBucket: "xxxxxxxx.appspot.com"
});
已将此问题的存储路径从“yourmaps”更改为“users”
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const {Storage} = require('@google-cloud/storage');
admin.initializeApp();
当用户帐户被删除时,用户uuid 被传递到云函数。我正在尝试删除在 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}`)
})
});
Firebase 存储日志中的错误消息。路径正确但是没有对象...
Error: No such object: xxxxxx.appspot.com/users/XXXXXXXXXX-XXXX-XXXX-XXXXXXXXXXX.jpg
在 Firebase 存储的“监控规则”中,我可以看到我的尝试是被允许的。任何帮助,将不胜感激。我错过了什么?
因为它是您的默认存储桶,下面应该可以解决问题(未经测试):
带有 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
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;
}
});
也就是说,不需要像你那样声明bucket,直接用admin.storage().bucket()
声明就可以了。
我终于解决了我的问题。我需要在初始化应用程序时声明 storageBucket。现在我可以正确找到我的水桶了。
admin.initializeApp({
databaseURL: "https://xxxxxxxx.firebaseio.com",
storageBucket: "xxxxxxxx.appspot.com"
});