gpt4 book ai didi

python - 从 Cloud Function 写入 Google Cloud Storage(python)

转载 作者:太空狗 更新时间:2023-10-29 18:02:43 25 4
gpt4 key购买 nike

我正在尝试从云函数中将文件上传到谷歌云存储。不过,我无法将云存储库导入到我的函数中。

能否以这种方式在云函数中使用云存储?

云函数

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 - 从 Cloud Function 写入 Google Cloud Storage(python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52249978/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com