从 Cloud Function (python) 写入 Google Cloud Storage
Posted
技术标签:
【中文标题】从 Cloud Function (python) 写入 Google Cloud Storage【英文标题】:Write to Google Cloud Storage from Cloud Function (python) 【发布时间】:2019-02-14 10:14:56 【问题描述】:我正在尝试从云功能中将文件上传到谷歌云存储。不过,我无法将云存储库导入到我的函数中。
能否以这种方式从云功能中使用云存储?
云功能
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 (google-cloud-storage) 添加到您的要求中? cloud.google.com/functions/docs/concepts/… 抱歉,您如何在 Cloud Functions 中测试这种参数化函数? 【参考方案1】:您需要将google-cloud-storage
包添加到您的requirements.txt
文件中,以将其安装到Cloud Functions 环境中。
【讨论】:
感谢您的回答,它也解决了我的问题,但是您如何在 Cloud Functions 中测试这种多参数化函数 我不确定你的意思,你能问一个更详细的新问题吗? 在这里提问,谢谢 - ***.com/questions/65148708/…以上是关于从 Cloud Function (python) 写入 Google Cloud Storage的主要内容,如果未能解决你的问题,请参考以下文章