【问题标题】:GCP functions deployment function failedGCP 功能部署功能失败
【发布时间】:2021-10-05 19:24:16
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案1】:

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

    例如:

    google-cloud-storage==1.41.0
    

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 我用的是官方的功能码。通过触发函数修改元数据。
      • 在日志中查看以下内容:ImportError: cannot import name 'storage' from 'google.cloud' (unknown location)
      • 好的,我理解你的评论,这是一个插入存储桶的后台函数,而不是一个 http 函数,对吧?
      • 是的,这个函数的触发器是存储类型的
      猜你喜欢
      • 2022-09-01
      • 2018-12-06
      • 2021-09-22
      • 2020-10-19
      • 2021-12-29
      • 1970-01-01
      • 2022-12-17
      • 2019-01-30
      • 2020-11-13
      相关资源
      最近更新 更多