在本文中,我们将为您详细介绍如何使用CloudFunctions删除文件?的相关知识,并且为您解答关于cloudfiles的疑问,此外,我们还会提供一些关于CloudFunctions工作正常,但没有
在本文中,我们将为您详细介绍如何使用Cloud Functions删除文件?的相关知识,并且为您解答关于cloud files的疑问,此外,我们还会提供一些关于Cloud Functions 工作正常,但没有出现日志、Cloud Functions 解析请求路径参数、Cloud Functions、Firestore 触发器、递归删除不起作用、Firebase Cloud Functions 中的条件语句的有用信息。
本文目录一览:- 如何使用Cloud Functions删除文件?(cloud files)
- Cloud Functions 工作正常,但没有出现日志
- Cloud Functions 解析请求路径参数
- Cloud Functions、Firestore 触发器、递归删除不起作用
- Firebase Cloud Functions 中的条件语句
如何使用Cloud Functions删除文件?(cloud files)
当我删除Firebase数据库中的帖子时,我希望云功能可以相应地删除Firebase存储中帖子的缩略图。我的问题是,当我尝试删除缩略图时,我认为找不到正确的图像文件。
这是我尝试过的:
const functions = require(''firebase-functions'')const admin = require(''firebase-admin'')const gcs = require(''@google-cloud/storage'')()exports.deletePost = functions.database.ref(''Posts/{pushId}'').onWrite(event => { const original = event.data.val() const previous = event.data.previous.val() const pushId = event.params.pushId if (original === null) return const filePath = ''Posts/'' + pushId + ''thumbnail.jpg'' const bucket = gcs.bucket(''postsapp-12312'') const file = bucket.file(filePath) const pr = file.delete() return pr});
这就是我在日志中得到的
ApiError:找不到Object.parseHttpRespBody(/user_code/node_modules/@google-
cloud/storage/node_modules/@google-
cloud/common/src/util.js:192:30),位于Object.handleResp(/ user_code /
node_modules / @ google-cloud / storage / node_modules / @ google-cloud /
common / src / util.js:132:18)在/ user_code / node_modules / @ google-cloud /
storage / node_modules / @ google-cloud / common / src /
Request.onResponse上的util.js:465:12
[作为_callback](/user_code/node_modules/@google-
cloud/storage/node_modules/retry-
request/index.js:120:7)在Request.self.callback(/
user_code/node_modules/@google-
cloud/storage/node_modules/request/request.js:188:22)(在Request.emit(events.js:191:7),在sendTwo(events.js:106:13))。(/user_code/node_modules/@google-
cloud/storage/node_modules/request/request.js:1171:10)在emitOne(events.js:96:
答案1
小编典典我已经设法解决了。这里的问题是我写的存储桶地址错误。它应该像postsapp-12312.appspot.com
而不是postsapp-12312
更新 有关放置存储桶地址的更好方法
Cloud Functions 工作正常,但没有出现日志
要在终端运行中更新到最新版本:$ sudo npm install -g firebase-tools
我联系了 Firebase 团队,这是他们的回应:
我已经详细阅读了您的描述,问题似乎是 由 Cloud Logging 中某些区域的某些摄取延迟引起的 由 GCP 处理(有关详细信息,请查看 GCP Dashboard)。
然而,之前在 Github issue 中报告了类似的问题,显然它只发生在旧版本的 Firebase CLI 工具,所以我的建议是更新这个命令 行接口到 latest version (v9.6.1) 并重新部署 再次函数,这将允许验证函数是否为 正确存储日志。您也可以尝试更新 npm 用于将所有内容保持在最新版本中的库。
您可以在终端中检查您的 firebase 版本:$ firebase --version
Cloud Functions 界面在过去几年发生了变化。
不确定这是否影响了日志聚合,但有可能。
重新部署您的函数,无需代码更新即可更新它
Cloud Functions 解析请求路径参数
如何解决Cloud Functions 解析请求路径参数?
我想弄清楚如何解析 GCP 云函数中的 URL 参数。我正在尝试做这个 answer 所做的事情。由于 HTTP GCP cloud function 的入口点接受请求 AFAIK。我可以想出一种方法让 Flask 像我提到的示例中那样解析 URL 参数。
解决方法
网址参数不是很清楚。如果您谈论查询参数(网址中 ? 之后的参数),您可以简单地使用 getting started example
def hello_world(request):
request_json = request.get_json()
# Here the query parameters
if request.args and ''message'' in request.args:
return request.args.get(''message'')
# Here it''s in the post body in JSON
elif request_json and ''message'' in request_json:
return request_json[''message'']
else:
return f''Hello World!''
如果你说路径参数,你可以依靠 view_args 值
# Function code
def entrypoint(request):
return request.view_args,200
# Test like that
> curl https://us-central1-<PROJECT ID>.cloudfunctions.net/<FUNCTION NAME>/test/to/path
> {"path":"test/to/path"}
但是,path 参数并没有为您拆分,您需要手动处理。
编辑 1
我找到了如何破解 Flask 并使用它的 URL 处理器。这是我的工作代码示例
# Function code
from flask import Flask
def entrypoint(request):
path = request.view_args[''path'']
f = Flask("internal")
f.add_url_rule("/test/<string:id>","entrypoint_internal")
r = f.test_request_context(path=path)
r.push()
path_value = r.request.view_args
r.pop()
return path_value,200
# Test like that
> curl https://us-central1-<PROJECT ID>.cloudfunctions.net/<FUNCTION NAME>/test/path
> {"id":"path"}
Cloud Functions、Firestore 触发器、递归删除不起作用
如何解决Cloud Functions、Firestore 触发器、递归删除不起作用?
目前,当使用 Firestore 触发器删除某些文档时,我正在尝试删除所有嵌套的 hirachy 数据
我的代码和错误代码如下
const admin = require(''firebase-admin'');
const firebase_tools = require(''firebase-tools'');
const functions = require(''firebase-functions'');
admin.initializeApp({
}
);
exports.firestore_delete_trigger_test = functions.firestore
.document(''collection1/{docs1}/collection2/{docs2}'')
.onDelete(async (change: any,context: any) => {
const path = change.path;
await firebase_tools.firestore
.delete(path,{
project: process.env.GCLOUD_PROJECT,recursive: true,yes: true,token: functions.config().fb.token
});
return {
path: path
};
});
错误代码如下
FirebaseError: Must specify a path.
at Object.reject (/workspace/node_modules/firebase-tools/lib/utils.js:122:27)
为什么会显示错误代码?出于安全考虑,我不想要“可调用函数”
解决方法
在使用 TypeScript 编写时永远不要使用 any
,因为您实际上“关闭”了 TypeScript 并丢失了有价值的类型信息,例如您的代码为何无法运行。
如果使用以下几行,change
(应该是snapshot
)和context
的类型分别自动设置为QueryDocumentSnapshot
和EventContext
由 onDelete
方法定义。
exports.firestore_delete_trigger_test = functions.firestore
.document(''collection1/{docs1}/collection2/{docs2}'')
.onDelete(async (snapshot,context) => {
});
注意:QueryDocumentSnapshot
就像一个普通的 DocumentSnapshot
,但它保证存在(snap.exists
总是返回 true
和 {{ 1}} 永远不会返回 snap.data()
)。
既然类型已恢复为 undefined
,您将看到您无法从中读取名为 snapshot
的属性,因为它未设置。在您当前的代码中,path
只会设置为 path
,这会导致错误提示您需要指定路径。
undefined
这是因为 const path = snapshot.path; // ✖ Property ''path'' does not exist on type ''QueryDocumentSnapshot''. ts(2339)
是 DocumentReference
的属性。要获取快照的基础 path
,您可以访问它的 DocumentReference
属性。
ref
接下来,在 Node 10+ Cloud Functions 中,更改了 available environment variables 的列表并删除了 const path = snapshot.ref.path; // ✔
。要获取当前项目 ID,您可以使用
process.env.GCLOUD_PROJECT
或
const PROJECT_ID = JSON.parse(process.env.FIREBASE_CONFIG).projectId;
这将生成您的(现代化)代码:
const defaultApp = admin.intializeApp();
const PROJECT_ID = defaultApp.options.projectId;
Firebase Cloud Functions 中的条件语句
既然要检查两个条件然后更新数据库,首先需要获取引用的内容。 即使数据库中的底层位置没有值,条件 user1 的引用变量也将始终具有值。相反,您需要检查基础值是否存在。
这必须通过异步编码(承诺)来完成,因为您需要检查的不是一个而是两个位置。
请通读 firebase 文档以了解创建参考变量和获取基础数据之间的区别。检查此文档 https://firebase.google.com/docs/reference/node/firebase.database.DataSnapshot#exists
, exports.onUserStatusChange = functions.database
.ref("/{uid}/presence")
.onUpdate(async (change,context) => {
// Get the data written to Realtime Database
const isOnline = change.after.val();
// Get a User1 reference to the Firestore document
const user1StatusFirestoreRef = firestore.doc(`user1_data/${context.params.uid}`);
// Get a User2 reference to the Firestore document
const user2StatusFirestoreRef = firestore.doc(`user2_data/${context.params.uid}`);
console.log(`status: ${isOnline}`);
// // Update the values on Firestore
return promise.all([
user1StatusFirestoreRef.update(
{
presence: isOnline,last_seen: Date.now(),}
),user2StatusFirestoreRef.update(
{
presence: isOnline,}
);
]);
});
我们今天的关于如何使用Cloud Functions删除文件?和cloud files的分享已经告一段落,感谢您的关注,如果您想了解更多关于Cloud Functions 工作正常,但没有出现日志、Cloud Functions 解析请求路径参数、Cloud Functions、Firestore 触发器、递归删除不起作用、Firebase Cloud Functions 中的条件语句的相关信息,请在本站查询。
本文标签: