google-cloud-functions - Cloud Functions and Storage:删除文件是异步的吗?
问题描述
在我的 Google Cloud Functions 脚本中,我想使用以下代码删除 Google Cloud Storage 文件:
const gcs = require('@google-cloud/storage')()
exports.deletePost = functions.https.onRequest((request, response) => {
if(!context.auth) {
throw new functions.https.HttpsError('failed-precondition', 'The function must be called while authenticated.');
}
const the_post = request.query.the_post;
const filePath = context.auth.uid + '/posts/' + the_post;
const bucket = gcs.bucket('android-com')
const file = bucket.file(filePath)
const pr = file.delete()
});
问题是我还需要在删除存储文件后删除 Google Firebase Firestore 数据库条目。所以我想知道我是否可以在例如返回的承诺中做到这一点delete
?
PS:我没有找到文档
解决方案
该代码file.delete()
是异步的并返回一个 Promise,如Google Cloud Storage:删除对象文档中所定义。
要从您的某个 Cloud Storage 存储分区中删除对象,请执行以下操作:
// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');
// Creates a client
const storage = new Storage();
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const bucketName = 'Name of a bucket, e.g. my-bucket';
// const filename = 'File to delete, e.g. file.txt';
// Deletes the file from the bucket
await storage
.bucket(bucketName)
.file(filename)
.delete();
console.log(`gs://${bucketName}/${filename} deleted.`);
不是很清楚,但是由于await
使用了语法,这意味着它正确的表达式的结果是一个 Promise。
注意:大部分有用的 Google Cloud Storage 文档都可以在此处找到。
推荐阅读
- python - 关闭与 Neomodel 的数据库连接
- c - 为什么'emacs &'会导致我的程序崩溃-C?
- python - 如何添加“if”语句,以便将某些相关的语句组合在一起
- python - Django Allauth 忽略适配器
- ionic4 - 当我们通过 fcm 收到来自应用服务器的通知时,即使应用被杀死,如何触发回调函数?
- python - cv2.VideoCapture() 用于 IP 网络摄像头安卓
- javascript - 在 React 中使用 Onclick 更改 div 中的图像
- torch - 火炬。如何扩展张量中的维度(从 [[1, 2, 3]] 到 [[1, 2, 3, 4]])?
- powerbi - 如何统计ALL返回的成员
- python - R中的循环交互