在 Cloud 运行 实例中访问和使用 Cloud Storage 中的 csv 文件
Accessing and using csv file from Cloud Storage in Cloud Run instance
我知道如何从云 运行 实例中的云存储下载文件。但是,我在 python 中找不到读取文件的语法。我希望立即将 csv 文件转换为 pandas 数据帧,只需使用 pd.read_csv('testing.csv')
。所以我的个人代码看起来像,
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
)
)
使用'testing.csv'等文件名表示将文件写入当前目录。当前目录是什么?相反,指定一个已知目录位置的绝对路径。
下载到/tmp/目录,例如'/tmp/testing.csv'。使用文件系统 space 会消耗内存,因为文件系统是基于 RAM 的。确保 Cloud 运行 实例有足够的内存。
Excerpt from Cloud Run Container Runtime Contact:
您的容器的文件系统是可写的,并受以下行为影响:
- 这是一个内存文件系统,因此写入它会使用容器实例的内存。
- 当容器实例停止时,写入文件系统的数据不会保留。
download_as_bytes
是你要找的函数,如果你想直接加载到内存中的话。
storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(source_blob_name)
data = blob.download_as_bytes()
pd.read_csv(StringIO(data))
Pandas 还支持直接从 Google 云存储读取。 https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html
Any valid string path is acceptable. The string could be a URL. Valid
URL schemes include http, ftp, s3, gs, and file.
所以类似于“gs://bucket/file”
我知道如何从云 运行 实例中的云存储下载文件。但是,我在 python 中找不到读取文件的语法。我希望立即将 csv 文件转换为 pandas 数据帧,只需使用 pd.read_csv('testing.csv')
。所以我的个人代码看起来像,
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
)
)
使用'testing.csv'等文件名表示将文件写入当前目录。当前目录是什么?相反,指定一个已知目录位置的绝对路径。
下载到/tmp/目录,例如'/tmp/testing.csv'。使用文件系统 space 会消耗内存,因为文件系统是基于 RAM 的。确保 Cloud 运行 实例有足够的内存。
Excerpt from Cloud Run Container Runtime Contact:
您的容器的文件系统是可写的,并受以下行为影响:
- 这是一个内存文件系统,因此写入它会使用容器实例的内存。
- 当容器实例停止时,写入文件系统的数据不会保留。
download_as_bytes
是你要找的函数,如果你想直接加载到内存中的话。
storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(source_blob_name)
data = blob.download_as_bytes()
pd.read_csv(StringIO(data))
Pandas 还支持直接从 Google 云存储读取。 https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html
Any valid string path is acceptable. The string could be a URL. Valid URL schemes include http, ftp, s3, gs, and file.
所以类似于“gs://bucket/file”