haw*_*ier 5 python google-cloud-storage google-cloud-functions
我尝试使用云函数将换行符分隔的 JSON 字符串存储在 GCS 存储桶中,但看到错误。我首先将数据帧转换为 ndjson,然后尝试将其上传到我的 GCS 存储桶,如下所示。上面还有更多代码,但与我的问题无关:
import pandas as pd
from google.cloud import storage
from google.cloud.storage import blob
df = df.to_json(orient="records", lines=True)
storage_client = storage.Client(project='my-project')
bucket = storage_client.get_bucket('my-bucket')
blob = bucket.blob('my-blob')
blob.upload_from_string(df)
Run Code Online (Sandbox Code Playgroud)
运行此程序时,我在日志中发现以下错误:
Exception on / [POST] Traceback (most recent call last):
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/flask/app.py", line 2073, in wsgi_app response = self.full_dispatch_request()
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/flask/app.py", line 1518, in full_dispatch_request rv = self.handle_user_exception(e)
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/flask/app.py", line 1516, in full_dispatch_request rv = self.dispatch_request()
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/flask/app.py", line 1502, in dispatch_request return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/functions_framework/__init__.py", line 99, in view_func return function(request._get_current_object())
File "/workspace/main.py", line 66, in my_request bucket = storage_client.get_bucket('my-bucket')
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/google/cloud/storage/client.py", line 787, in get_bucket retry=retry,
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/google/cloud/storage/bucket.py", line 1037, in reload retry=retry,
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/google/cloud/storage/_helpers.py", line 244, in reload _target_object=self,
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/google/cloud/storage/client.py", line 373, in _get_resource _target_object=_target_object,
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/google/cloud/storage/_http.py", line 73, in api_request return call()
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/google/api_core/retry.py", line 288, in retry_wrapped_func on_error=on_error,
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/google/api_core/retry.py", line 190, in retry_target return target() TypeError: api_request() got an unexpected keyword argument 'extra_api_info'
Run Code Online (Sandbox Code Playgroud)
这个“extra_api_info”参数似乎是罪魁祸首,但我不知道这意味着什么,并且在遵循完全相同的方法时从未遇到过此错误,所以我想知道这是否归因于不同版本的“ google.cloud' Python 模块。
我自己找到了问题的答案。正如我怀疑的那样,这确实是一个模块版本问题。
google.cloud.storage==1.44.0在我的文件中指定requirements.txt解决了问题,因为我的代码似乎与该模块的最新版本不兼容(出于我无法理解的原因)。
| 归档时间: |
|
| 查看次数: |
2937 次 |
| 最近记录: |