简体   繁体   English

无法使用Firebase功能删除Google Cloud Storage文件

[英]Cannot delete Google Cloud Storage files using Firebase Functions

I cannot seem to delete files from my Firebase Storage using Firebase Functions. 我似乎无法使用Firebase功能从Firebase存储中删除文件。 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 ". 我已经进行了将近一个星期的时间,而我所得到的“最接近”我相信是错误本身内的错误, “无法在ApiError处解析JSON响应 ”。

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. 现在,我要做的是,一旦删除Firebase用户,我想从用户文件和数据中清除数据库和存储。

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. 我已将我的Google API服务代理App Engine默认服务帐户设置为存储管理员。

My dependencies at the moment of posting are 我发布时的依赖是

"@google-cloud/storage": "^2.3.4",
"firebase-admin": "^6.4.0",
"firebase-functions": "^2.1.0"

const bucketname = "gs://MY_PROJECT.appspot.com/user/"+userUID; 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. 这是存储桶中某个位置的URL。 The name of a bucket doesn't look like a URL, and it doesn't have a path to a file. 存储桶的名称看起来不像URL,也没有文件的路径。 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. 默认存储桶的名称可能只是“ MY_PROJECT.appspot.com”,您可以在“存储”部分的“云控制台”中进行检查。

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. 拥有Bucket对象后,便可以建立对文件的引用。 Those files might have path components, such as "/user/UID/somefile.jpg". 这些文件可能具有路径组件,例如“ /user/UID/somefile.jpg”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 每日从 Google Cloud Firebase 以.png 结尾的存储桶删除文件 - Daily delete files from Google Cloud Firebase Storage bucket that end with .png firebase 函数中有没有办法删除与正则表达式匹配的存储文件? - Is there a way in firebase functions to delete storage files that match a regular expression? Firebase Admin SDK可下载/检索Google Cloud Storage上的文件 - Firebase Admin SDK to download/retrieve files on Google Cloud Storage 从云功能中删除云存储中的文件 - Delete file in cloud storage from cloud functions 使用Firebase功能将图像上传到Cloud Storage错误 - Image Uploading to Cloud Storage with firebase functions Error 重命名谷歌云存储中的文件? - renaming files in Google Cloud Storage? 云功能:从存储中删除文件 - Cloud functions: Delete file from storage 在 firebase 云函数中使用 express `app.get()` 访问文件 - Using express `app.get()` in firebase cloud functions to access files 使用JavaScript Promise从Firebase云功能读取Google表格 - Reading Google Sheets from Firebase cloud functions using javascript promises 在Firebase Cloud Functions中使用Google Vision API时发生TypeError - TypeError when using Google Vision API in Firebase Cloud Functions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM