GCP 功能部署功能失败

时间:2021-07-30 08:23:58

标签: python-3.x function google-cloud-platform storage

在 GCP 函数中创建了一个 python 函数来修改存储中对象的元数据。部署时报错,报错信息如下:

Function failed on loading user code. This is likely due to a bug in the user code. 
Error message: Error: please examine your function logs to see the error cause: 
https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs. Additional 
troubleshooting documentation can be found at 
https://cloud.google.com/functions/docs/troubleshooting#logging. Please visit 
https://cloud.google.com/functions/docs/troubleshooting for in-depth troubleshooting 
documentation.

我的函数代码如下:

from google.cloud import storage

def set_blob_metadata(bucket_name, blob_name):
    """Set a blob's metadata."""
    # bucket_name = 'your-bucket-name'
    # blob_name = 'your-object-name'

    storage_client = storage.Client()
    bucket = storage_client.bucket(lrving)
    blob = bucket.get_blob(index.html)
    metadata = {'Cache-Control': 'no-store'}
    blob.metadata = metadata
    blob.patch()

    print("The metadata for the blob {} is {}".format(blob.name, blob.metadata))

如何解决这个问题?谢谢!

2 个答案:

答案 0 :(得分:1)

根据您提到的错误消息,该问题是由您的运行时中没有 Cloud Storage 模块引起的。除了 Guillaume 的回答之外,您还需要在函数部署期间安装该依赖项。您可以通过将 google-cloud-storage 添加到您的 requirements.txt 文件来实现。

例如:

google-cloud-storage==1.41.0

有关如何为 Cloud Functions 指定 Python 依赖项的更多信息,请参见 here

答案 1 :(得分:0)

你的函数签名是错误的。你需要comply with the platform definition

您将在函数参数中获得所需的数据。