我目前正在运行一个将数据库上传到bigquery的过程,我查询并操作数据,然后将这些数据发送回我的google云存储。然而,我想自动化这个文件被发送到slack,然而我需要它保存到我的文件中才能做到这一点。我可以将文件从我的GCS导出到BQ,但我不知道如何将文件导出到我的计算机。谢谢你的帮助。我使用的是python。
def export_data_to_gcs(data, test12, destination):
bigquery_client = bigquery.Client(data)
dataset = bigquery_client.dataset('FirebaseArchive')
table = dataset.table('test12')
job_name = str(uuid.uuid4())
job = bigquery_client.extract_table_to_storage(
job_name, table, 'gs://firebase_results/test12.csv')
job.source_format = 'CSV'
job.begin()
wait_for_job(job)
def wait_for_job(job):
while True:
job.reload()
if job.state == 'DONE':
if job.error_result:
raise RuntimeError(job.errors)
return
time.sleep(1)
export_data_to_gcs(data, 'test12', destination)
这是我到目前为止拥有的代码,它将表从BQ导出到我的云中。
发布于 2017-07-14 01:46:45
我想通了。这里是所有需要它的人。与上面给出的答案类似,但稍作修改。
client = storage.Client(project=data)
bucket = client.get_bucket('firebase_results')
blob = bucket.blob('test2.csv')
with open('test2.csv', 'w') as file_obj:
blob.download_to_file(file_obj)
发布于 2017-07-13 01:42:09
将表导出到GCS后,可以使用GCS客户端下载文件:https://googlecloudplatform.github.io/google-cloud-python/stable/storage-blobs.html#google.cloud.storage.blob.Blob.download_to_file
from google.cloud.storage import Blob
client = storage.Client(project='my-project')
bucket = client.get_bucket('firebase_results')
encryption_key = 'c7f32af42e45e85b9848a6a14dd2a8f6'
blob = Blob('test12.csv', bucket, encryption_key=encryption_key)
with open('/tmp/my-downloaded-file', 'wb') as file_obj:
blob.download_to_file(file_obj)
https://stackoverflow.com/questions/45069968
复制相似问题