繁体   English   中英

谷歌云 function python 无法与 blob 交互

[英]Google cloud function python cannot interact with blobs

我正在尝试将文件上传到存储桶,解析为 json 并将其插入到 bigquery 中的表中。 我收到这个错误

AttributeError: 'str' object has no attribute 'blob'

我相信这是一个依赖问题,但我不知道如何解决。

我正在直接编码到云 function gui。

def create_gcs(event, context):
    """Triggered by a change to a Cloud Storage bucket.
    Args:
         event (dict): Event payload.
         context (google.cloud.functions.Context): Metadata for the event.
    """
    file = event
    print(f"********The file {file['name']} was created in {file['bucket']} at {event['timeCreated']}")
    
    
    bucket = file['bucket']
    blob = bucket.get_blob(file['name'])
    data = blob.download_as_string()
    table_id="tableid"
    client=bigquery.Client()
    client.insert_rows_json(table_id,[data])
    print(blob)

我认为你应该首先创建一个存储客户端 object 并调用get_blob function。

斑点/对象

from google.cloud import storage
client = storage.Client()
bucket = client.get_bucket(file['bucket'])
blob = bucket.get_blob(file['name'])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM