问题
I cannot seem to delete files from my Firebase Storage using Firebase Functions. I have been at it for almost a week now, and the "closest" I have gotten I believe is an error within the error itself, "Cannot parse JSON response at ApiError".
Now, what I want to be doing is that once a Firebase user is deleted, I want to clear my database and storage from the users files and data.
const admin = require('firebase-admin');
const {Storage} = require('@google-cloud/storage');
exports.deleteUserHandler = (event, context) => {
const userUID = event.uid;
const bucketname = "gs://MY_PROJECT.appspot.com/user/"+userUID;
const storage = new Storage({
projectId: "MY_PROJECT_ID",
});
return admin.database().ref('/user/' + userUID).remove().then(() => {
console.log("User " + userUID + " database removed");
return storage.bucket(bucketname).file("profilepic.jpeg").delete();
}).then(() => {
return storage.bucket(bucketname).file("smallprofilepic.jpeg").delete();
}).then(() => {
console.log("User " + userUID + " firestore removed");
});
}
The function trigger when supposed to, and removes the data from the realtime database. I can not however remove images from the storage. I feel like I am closest to what should be at the moment, but the error I get from the function logs is as follows.
Error: Cannot parse JSON response
at ApiError (/user_code/node_modules/@google-cloud/storage/node_modules/@google-cloud/common/build/src/util.js:43:9)
at Util.parseHttpRespBody (/user_code/node_modules/@google-cloud/storage/node_modules/@google-cloud/common/build/src/util.js:167:42)
at Util.handleResp (/user_code/node_modules/@google-cloud/storage/node_modules/@google-cloud/common/build/src/util.js:116:117)
at retryRequest (/user_code/node_modules/@google-cloud/storage/node_modules/@google-cloud/common/build/src/util.js:403:22)
at onResponse (/user_code/node_modules/@google-cloud/storage/node_modules/retry-request/index.js:200:7)
at /user_code/node_modules/@google-cloud/storage/node_modules/teeny-request/build/src/index.js:222:13
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
I do not know what it means.
I have set my Google APIs Service Agent and App Engine default service account to be storage admins.
My dependencies at the moment of posting are
"@google-cloud/storage": "^2.3.4",
"firebase-admin": "^6.4.0",
"firebase-functions": "^2.1.0"
回答1:
const bucketname = "gs://MY_PROJECT.appspot.com/user/"+userUID;
That string above is not the name of your bucket. It's a URL to a location in the bucket. The name of a bucket doesn't look like a URL, and it doesn't have a path to a file. The name of your default bucket is probably just "MY_PROJECT.appspot.com", and you can check that in the Cloud console in the Storage section.
It looks like you might be under the misconception that a bucket is a folder or something. Buckets are just containers. You build references to files after you have a Bucket object. Those files might have path components, such as "/user/UID/somefile.jpg".
来源:https://stackoverflow.com/questions/53901874/cannot-delete-google-cloud-storage-files-using-firebase-functions