本文介绍了从Cloud Function(Python)写入Google Cloud Storage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从云功能中将文件上传到Google云存储.但是,我无法将云存储库导入到我的函数中.

I am trying to upload a file to google cloud storage from within a cloud function. I can't import the cloud storage library into my function, though.

可以通过这种方式在云功能内部使用云存储吗?

Can cloud storage be used from within cloud functions in this manner?

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环境中.

You need to add the google-cloud-storage package to your requirements.txt file to install it in the Cloud Functions environment.

这篇关于从Cloud Function(Python)写入Google Cloud Storage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-20 17:03
查看更多