首页 > 解决方案 > GCP 功能部署功能失败

问题描述

在 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))

如何解决这个问题呢?谢谢!</p>

标签: python-3.xfunctiongoogle-cloud-platformstorage

解决方案


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

例如:

google-cloud-storage==1.41.0

可以在此处找到 有关如何为 Cloud Functions 指定 Python 依赖项的更多信息。


推荐阅读