python - 从 Cloud Function (python) 写入 Google Cloud Storage
问题描述
我正在尝试从云功能中将文件上传到谷歌云存储。不过,我无法将云存储库导入到我的函数中。
可以以这种方式从云功能中使用云存储吗?
云功能
from google.cloud import storage
def upload_blob(bucket_name, blob_text, destination_blob_name):
"""Uploads a file to the bucket."""
storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name)
blob = bucket.blob(destination_blob_name)
blob.upload_from_string(blob_text)
print('File {} uploaded to {}.'.format(
source_file_name,
destination_blob_name))
def log_data(request):
request_json = request.get_json()
BUCKET_NAME = 'my-bucket'
BLOB_NAME = 'test-blob'
BLOB_STR = '{"blob": "some json"}'
upload_blob(BUCKET_NAME, BLOB_STR, BLOB_NAME)
return f'Success!'
错误
Deployment failure:
Function load error: Code in file main.py can't be loaded.
File "/user_code/main.py", line 1, in <module>
from google.cloud import storage
ImportError: cannot import name 'storage' from 'google.cloud' (unknown location)
解决方案
您需要将google-cloud-storage
包添加到requirements.txt
文件中才能将其安装在 Cloud Functions 环境中。
推荐阅读
- python-3.x - 在 CalibratedClassifierCV 内使用管道分类器
- objective-c - 在 Objective-C 测试中测试 Swift 的问题
- xslt - 使用 XSLT 使用属性值更改元素名称
- smartsheet-api - 没有这样的主机是已知的 (api.smartsheet.com) - Java SDK
- functional-programming - 累积错误的结果生成器
- django - Django用m2m字段保存表单
- c# - 获取没有文本框的文本输入
- typescript - 谁能在 Typescript 中解释这种行为?
- r - R data.table 子集问题/错误消息
- reactjs - 从不同的组件调用 onclick 事件