![](/img/trans.png)
[英]Reading from .csv to dataframe from Google Cloud Storage has a bug when ran in Google Cloud Functions
[英]Reading Data From Cloud Storage Via Cloud Functions
我正在尝试对在 Python 中构建数据处理管道的概念进行快速验证。 为此,我想构建一个 Google 函数,该函数将在某些 .csv 文件放入 Cloud Storage 时触发。
我遵循了这个 Google Functions Python 教程,虽然示例代码确实会在删除文件时触发函数来创建一些简单的日志,但我真的被困在我必须进行的调用上才能实际读取数据的内容。 我试图搜索 SDK/API 指南文档,但我找不到。
如果这是相关的,一旦我处理了 .csv,我希望能够将我从中提取的一些数据添加到 GCP 的 Pub/Sub 中。
该函数实际上并不接收文件的内容,只是一些关于它的元数据。
您需要使用google-cloud-storage
客户端。 有关更多详细信息,请参阅“下载对象”指南。
把它和你正在使用的教程放在一起,你会得到一个像这样的函数:
from google.cloud import storage
storage_client = storage.Client()
def hello_gcs_generic(data, context):
bucket = storage_client.get_bucket(data['bucket'])
blob = bucket.blob(data['name'])
contents = blob.download_as_string()
# Process the file contents, etc...
这是使用pandas
的替代解决方案:
云函数代码:
import pandas as pd
def GCSDataRead(event, context):
bucketName = event['bucket']
blobName = event['name']
fileName = "gs://" + bucketName + "/" + blobName
dataFrame = pd.read_csv(fileName, sep=",")
print(dataFrame)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.