【问题标题】:Google Datalab read from cloud storageGoogle Datalab 从云存储中读取
【发布时间】:2018-07-25 19:44:26
【问题描述】:

我知道这个问题已被问过很多次,但所有答案都不符合我的要求。 我想从 datalab 检索存储到云存储中的 csv 文件。 为了在普通应用程序中重复使用代码,我不想使用 datalab.storage 库,而是使用官方云存储,并且没有任何魔法。

有可能吗? 到目前为止,我做到了:

from google.cloud import storage

client = storage.Client()
bucket = client.get_bucket(BUCKET_NAME)
blob = storage.Blob(gs_path, bucket)
# here I should put something equivalent to 
# data = data_obj.read_stream() if using datalab.storage
# %gcs read --object $uri --variable data if using magic

如何使用干净的存储库? 谢谢

【问题讨论】:

    标签: python-3.x google-cloud-storage google-cloud-datalab


    【解决方案1】:

    是的,这是可能的。假设您希望将其保存到文件中,您可以使用 blob.download_to_filename()

    def download_blob(bucket_name, source_blob_name, destination_file_name):
        """Downloads a blob from the bucket."""
        storage_client = storage.Client()
        bucket = storage_client.get_bucket(bucket_name)
        blob = bucket.blob(source_blob_name)
    
        blob.download_to_filename(destination_file_name)
    
        print('Blob {} downloaded to {}.'.format(
            source_blob_name,
            destination_file_name))
    

    download_as_string() 和 download_to_file() 等其他选项为 available as well

    参考资料:

    【讨论】:

      猜你喜欢
      • 2017-10-08
      • 1970-01-01
      • 2018-08-18
      • 1970-01-01
      • 2019-04-20
      • 2021-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多