![](/img/trans.png)
[英]Create new csv file in Google Cloud Storage from cloud function
[英]How to access csv file from Google Cloud Storage in a Google Cloud Function via Pandas?
我是雲功能的新手,所以我遵循了默認的 GCP 雲 function “hello world”教程。 它工作正常並按預期打印“hello world”。 我只更改了 requirements.txt 文件以包含 pandas 和 google-cloud-storage。 同樣,我對 main.py 腳本的所有編輯都在 function 定義之前的導入部分和 function 的 else 部分中。
要求.txt
pandas
google-cloud-storage
主要.py:
import pandas as pd
from google.cloud import storage
def hello_world(request):
"""Responds to any HTTP request.
Args:
request (flask.Request): HTTP request object.
Returns:
The response text or any set of values that can be turned into a
Response object using
`make_response <http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>`.
"""
request_json = request.get_json()
if request.args and 'message' in request.args:
return request.args.get('message')
elif request_json and 'message' in request_json:
return request_json['message']
else:
storage_client = storage.Client()
bucket = storage_client.bucket('my_bucket')
model_filename = "my_file.csv"
blob = bucket.blob(model_filename)
blob.download_to_filename('temp.csv')
with open('temp.csv','rb') as f:
df = pd.read_csv(f)
return str(df.columns)
當我在 GCP 的“測試雲功能”區域測試 function 時,在日志中捕獲了以下錯誤。 前 7 行似乎是樣板錯誤,而后兩行特定於我的實際程序。 File "/layers/google.python.pip/pip/lib/python3.8/site-packages/google/cloud/storage/blob.py", line 1183, in download_to_filename with open(filename, "wb") as file_obj: OSError: [Errno 30] Read-only file system: 'temp.csv'
。 我不知道為什么會觸發此錯誤。
錯誤:
Traceback (most recent call last): File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app response = self.full_dispatch_request()
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request rv = self.handle_user_exception(e)
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1821, in handle_user_exception reraise(exc_type, exc_value, tb)
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise raise value
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request rv = self.dispatch_request()
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1936, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args)
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/functions_framework/__init__.py", line 87, in view_func return function(request._get_current_object())
File "/workspace/main.py", line 25, in hello_world blob.download_to_filename('temp.csv')
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/google/cloud/storage/blob.py", line 1183, in download_to_filename with open(filename, "wb") as file_obj: OSError: [Errno 30] Read-only file system: 'temp.csv'
對於上下文,我已經將憑據添加到適當的服務帳戶,此雲 function 根據我設置的配置使用該憑據。 所以,除了授權,我不知道為什么 function 會失敗。 我應該改變什么?
對於上下文,我只是嘗試從 pandas 的雲存儲中打開任意 csv 文件,並將列的名稱作為字符串返回。 這沒有實際價值,只是在構建有價值的東西之前進行功能測試。
Edit1:據我所知,賦予與雲 function 對應的服務帳戶的特定 IAM 角色是“角色/編輯器”,這應該足夠了。
Edit2:GCP 雲功能似乎在只讀環境中運行。 所以必須有其他方法來打開文件,而不使用blob.download_to_filename
命令。
您是 Cloud Functions 的新手,需要了解一些知識並避免一些陷阱。 其中之一:Cloud Functions 是無狀態的,你不能在文件系統上寫。
除了/tmp
目錄。 它是 memory 文件系統中的一個(正確調整您的 Cloud Functions memory 大小以考慮您的應用程序 memory 占用空間 + 存儲在 /tmp 目錄中的文件大小)
像這樣更新您的雲 Function
....
else:
storage_client = storage.Client()
bucket = storage_client.bucket('my_bucket')
model_filename = "my_file.csv"
blob = bucket.blob(model_filename)
blob.download_to_filename('/tmp/temp.csv')
with open('/tmp/temp.csv','rb') as f:
df = pd.read_csv(f)
return str(df.columns)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.