部署时云功能抛出错误

时间:2020-08-18 15:46:35

标签: google-cloud-platform google-cloud-functions google-cloud-storage

部署云功能时出现以下错误

我在Requirements.txt中使用最新版本的云存储 另外,我正在使用存储客户端的list_blob功能,从那时起,我在部署该功能时面临以下错误

ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Function failed on loading user code. 
Error message: __init__() takes 2 positional arguments but 3 were given
Detailed stack trace:
from datetime import datetime
Traceback (most recent call last):
  File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker_v1.py", line 315, in check_
or_load_user_function
    _function_handler.load_user_function()
  File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker_v1.py", line 190, in load_u
ser_function
    spec.loader.exec_module(main_module)
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/user_code/main.py", line 52, in <module>
    CS = storage.Client()
  File "/env/local/lib/python3.7/site-packages/google/cloud/storage/client.py", line 129, in __init__
    self._connection = Connection(self, **kw_args)
  File "/env/local/lib/python3.7/site-packages/google/cloud/storage/_http.py", line 35, in __init__
    super(Connection, self).__init__(client, client_info)
TypeError: __init__() takes 2 positional arguments but 3 were given

1 个答案:

答案 0 :(得分:2)

我测试了您的代码,将其包装在correct HTTP function format

中后,它可以完美地工作
#Your function
def list_blobs(bucket_name):
    from google.cloud import storage
    storage_client = storage.Client()
    blobs = storage_client.list_blobs(bucket_name)
    fileList = []
    for blob in blobs:
        fileList.append(blob.name)
    return fileList

#function wrapper
def test_list_blob(request):
    ret = list_blobs('temp-veolia-export')
    return str(ret),200

并部署

gcloud functions deploy --trigger-http --entry-point=test_list_blob --runtime=python37 \
  --region=us-central1 --allow-unauthenticated python-test-storage