【发布时间】:2022-01-04 05:39:31
【问题描述】:
我知道如何从云运行实例中的云存储下载文件。但是,我找不到在 python 中读取文件的语法。我希望通过使用pd.read_csv('testing.csv') 立即将 csv 文件转换为 pandas 数据帧。所以我的个人代码看起来像,
download_blob(bucket_name, source_blob_name, 'testing.csv')。那么我不应该能够在云运行实例中执行pd.read_csv('testing.csv') 吗?这样做时,我在加载页面时不断获得内部服务器。这似乎是一个简单的问题,但我无法在任何地方找到它的示例。一切都只是下载文件,我从来没有看到它被使用过。
def download_blob(bucket_name, source_blob_name, destination_file_name):
"""Downloads a blob from the bucket."""
# The ID of your GCS bucket
# bucket_name = "your-bucket-name"
# The ID of your GCS object
# source_blob_name = "storage-object-name"
# The path to which the file should be downloaded
# destination_file_name = "local/path/to/file"
storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
# Construct a client side representation of a blob.
# Note `Bucket.blob` differs from `Bucket.get_blob` as it doesn't retrieve
# any content from Google Cloud Storage. As we don't need additional data,
# using `Bucket.blob` is preferred here.
blob = bucket.blob(source_blob_name)
blob.download_to_filename(destination_file_name)
print(
"Downloaded storage object {} from bucket {} to local file {}.".format(
source_blob_name, bucket_name, destination_file_name
)
)
【问题讨论】:
标签: python csv google-cloud-platform google-cloud-storage google-cloud-run