gpt4 book ai didi

python-3.x - Google Datalab 从云存储中读取

转载 作者:行者123 更新时间:2023-12-01 22:23:37 24 4
gpt4 key购买 nike

我知道这个问题已被问过很多次,但所有答案都不符合我的要求。我想从 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

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

最佳答案

是的,这是可能的。假设您希望将其保存到文件中,可以使用 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 .

引用文献:

关于python-3.x - Google Datalab 从云存储中读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51526572/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com