![](/img/trans.png)
[英]Rewriting a csv file with cloud function from cloud Storage and send it to BigQuery
[英]Accessing and using csv file from Cloud Storage in Cloud Run instance
我知道如何從雲運行實例中的雲存儲下載文件。 但是,我在 python 中找不到讀取文件的語法。 我希望立即將 csv 文件轉換為 pandas dataframe,只需使用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' 。 使用文件系統空間會消耗 memory,因為文件系統是基於 RAM 的。 確保 Cloud Run 實例有足夠的 memory。
摘自 Cloud Run Container Runtime Contact :
容器的文件系統是可寫的,並且受以下行為的影響:
如果要將其直接加載到 memory, download_as_bytes
是您正在尋找的 function。
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 還支持直接從谷歌雲存儲讀取。 https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html
任何有效的字符串路徑都是可接受的。 該字符串可以是 URL。 有效的 URL 方案包括 http、ftp、s3、 gs和文件。
所以像“gs://bucket/file”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.