gpt4 book ai didi

python - 通过 GCS 触发器在云功能中获取用户信息

转载 作者:行者123 更新时间:2023-12-01 00:02:46 33 4
gpt4 key购买 nike

如果有任何文件上传到 Google 云存储,我们将通过 Google 云功能自动处理文件。我们使用 python 编写了代码。

https://cloud.google.com/functions/docs/calling/storage

def hello_gcs_generic(data, context):
"""Background Cloud Function to be triggered by Cloud Storage.
This generic function logs relevant data when a file is changed.

Args:
data (dict): The Cloud Functions event payload.
context (google.cloud.functions.Context): Metadata of triggering event.
Returns:
None; the output is written to Stackdriver Logging
"""

print('Event ID: {}'.format(context.event_id))
print('Event type: {}'.format(context.event_type))
print('Bucket: {}'.format(data['bucket']))
print('File: {}'.format(data['name']))
print('Metageneration: {}'.format(data['metageneration']))
print('Created: {}'.format(data['timeCreated']))
print('Updated: {}'.format(data['updated']))

我们想知道谁上传了用于我们记录目的的文件。在上述提供用户信息的函数中,我在数据和上下文中没有看到任何变量。

有什么办法可以找出谁在 GCF 中上传文件以用于 GCS 触发器吗?

最佳答案

正如您所提到的,数据或上下文中的对象所有者没有变量。但是,使用存储桶名称和对象名称,您可以调用 Objects: get检索对象元数据的 API。

使用projection full参数,您将收到以下响应对象:

{
"kind": "storage#object",
"id": string,
"selfLink": string,
"name": string,
"bucket": string,
"generation": long,
"metageneration": long,
"contentType": string,
"timeCreated": datetime,
"updated": datetime,
"timeDeleted": datetime,
"temporaryHold": boolean,
"eventBasedHold": boolean,
"retentionExpirationTime": datetime,
"storageClass": string,
"timeStorageClassUpdated": datetime,
"size": unsigned long,
"md5Hash": string,
"mediaLink": string,
"contentEncoding": string,
"contentDisposition": string,
"contentLanguage": string,
"cacheControl": string,
"metadata": {
(key): string
},
"acl": [
objectAccessControls Resource
],
"owner": {
"entity": string,
"entityId": string
},
"crc32c": string,
"componentCount": integer,
"etag": string,
"customerEncryption": {
"encryptionAlgorithm": string,
"keySha256": string
},
"kmsKeyName": string
}

其中所有者是:

The owner of the object. This will always be the uploader of the object. If iamConfiguration.uniformBucketLevelAccess.enabled is set to true, this field does not apply, and is omitted in responses.

Google Cloud Storage Objects

我使用了 Api,所有者实体是用于上传文件的服务帐户。

关于python - 通过 GCS 触发器在云功能中获取用户信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60219273/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com