从Google Cloud Storage读取时指定读取缓冲区大小

时间:2019-03-05 03:07:18

标签: python google-cloud-platform google-cloud-storage

我正在尝试使用cloudstorage库打开Goog​​le Cloud Storage中的文件。 我收到模块cloudstorage没有属性“打开”的错误。

当我将文件从Google Cloud Storage加载到Google BigQuery时,我想指定读取缓冲区的大小。这是我要使用的功能。参数需要类似object的文件。

Client.load_table_from_file(file_obj, destination, rewind=False, size=None, num_retries=6, job_id=None, job_id_prefix=None, location=None, project=None, job_config=None)[source]

从类似文件的对象上上传该表的内容。

还有其他方法可以将Cloud Storage文件作为该方法的对象传递吗?或者是另一种在指定读取缓冲区大小的同时将文件从云存储加载到Google BigQuery的方法。

 from google.cloud import bigquery
 from google.cloud import storage 
 import cloudstorage as gcs
 def hello_gcs(event, context):
    gcs_file = gcs.open('no-trigger/transaction.csv')
    job_config = bigquery.LoadJobConfig()
    job_config.autodetect = False
    job_config.max_bad_records=1
    job_config.create_disposition = 'CREATE_IF_NEEDED'
    job_config.source_format = bigquery.SourceFormat.CSV
    load_job = bclient.load_table_from_file(
    gcs_file,
    dataset_ref.table(temptablename),
    location='asia-northeast1',
    size=2147483648,
    job_config=job_config)  # API request

1 个答案:

答案 0 :(得分:0)

您可以使用load_table_from_uri()方法来消除从GCS下载的需要,并在上载时提供类似文件的对象。这样的事情应该起作用:

client.load_table_from_uri(
    'gs://your-bucket/path/to/file.csv',
    destination
)