使用Python脚本中的Google Cloud Functions从Google Cloud Storage中读取CSV

蒂亚戈·古德

我是GCP的新手,我正在尝试使用Cloud Functions做一个简单的API。此API需要从Google Cloud Storage存储桶中读取CSV并返回JSON。为此,我可以在本地正常运行,打开一个文件。

但是在Cloud Functions中,我从存储桶中收到了一个Blob,并且不知道如何操作,我收到了错误

我尝试将blob转换为Bytes和字符串,但我不知道该怎么做

在我的本地环境中工作的代码:


data1 = '2019-08-20'
data1 = datetime.datetime.strptime(data1, '%Y-%m-%d')

data2 = '2019-11-21' 
data2 = datetime.datetime.strptime(data2, '%Y-%m-%d')


with open("/home/thiago/mycsvexample.csv", "r") as fin:
    #create a CSV dictionary reader object
    print(type(fin))
    csv_dreader = csv.DictReader(fin)
#iterate over all rows in CSV dict reader
    for row in csv_dreader:
        #check for invalid Date values

        #convert date string to a date object
        date = datetime.datetime.strptime(row['date'], '%Y-%m-%d')
        #check if date falls within requested range
        if date >= data1 and date <= data2:
            total = total + float(row['total'])

print(total)

Google Cloud Functions中的代码:

import csv, datetime
from google.cloud import storage
from io import BytesIO 

def get_orders(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 'token' in request.args:
        if request.args['token'] == 'mytoken888888':
            client = storage.Client()
            bucket = client.get_bucket('mybucketgoogle.appspot.com')

            blob = bucket.get_blob('mycsvfile.csv')
            byte_stream = BytesIO()
            blob.download_to_file(byte_stream)
            byte_stream.seek(0)
            file = byte_stream

            #with open(BytesIO(blob), "r") as fin:

                #create a CSV dictionary reader object
            csv_dreader = csv.DictReader(file)
                #iterate over all rows in CSV dict reader
            for row in csv_dreader:
                #check for invalid Date values


                date = datetime.datetime.strptime(row['date'], '%Y-%m-%d')
                    #check if date falls within requested range
                if date >= datetime.datetime.strptime(request.args['start_date']) and date <= datetime.datetime.strptime(request.args['end_date']):
                    total = total + float(row['total'])
            dict = {'total_faturado' : total}



            return dict
        else:

            return f'Passe parametros corretos'
    else:
        return f'Passe parametros corretos'

Google Cloud Functions中的错误:

Traceback (most recent call last): File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 346, in run_http_function result = _function_handler.invoke_user_function(flask.request) File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 217, in invoke_user_function return call_user_function(request_or_event) File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 210, in call_user_function return self._user_function(request_or_event) File "/user_code/main.py", line 31, in get_orders_tramontina for row in csv_dreader: File "/opt/python3.7/lib/python3.7/csv.py", line 111, in __next__ self.fieldnames File "/opt/python3.7/lib/python3.7/csv.py", line 98, in fieldnames self._fieldnames = next(self.reader) _csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)

我尝试做其他事情,但没有成功...

有人可以帮助我解决这个问题,进行转换或以正确的方式进行操作吗?

谢谢你们

玛丽安·弗拉多伊

这是对我有用的代码:

from google.cloud import storage 
import csv


client = storage.Client()

bucket = client.get_bucket('source')

blob = bucket.blob('file')

dest_file = '/tmp/file.csv'

blob.download_to_filename(dest_file)
dict = {}
total = 0

 with open(dest_file) as fh:
    # assuming your csv is del by comma
    rd = csv.DictReader(fh, delimiter=',')
    for row in rd:
         date = datetime.datetime.strptime(row['date'], '%Y-%m-%d')
                #check if date falls within requested range
         if date >= datetime.datetime.strptime(request.args['start_date']) and date <= datetime.datetime.strptime(request.args['end_date']):
                total = total + float(row['total'])
         dict['total_faturado'] = total 

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Google Cloud Functions中的Python

努力从 Google Cloud Storage 存储桶中读取 csv 文件

如何使用带有 Python 的 Google Cloud Functions 将列表写入 Google Cloud Storage 中的文件

如何从Google Cloud Functions NodeJS连接到Google Cloud Storage

使用Google Cloud Dataflow合并Google Cloud Storage中的文件

Google Storage // Cloud Function // Python 修改 Bucket 中的 CSV 文件

如何从 Google Cloud Functions 中删除文件

Google Cloud Functions中未处理的拒绝

Cloud Functions中的Cloud Firestore

Google Cloud Functions - 重试

从Cloud Function(Python)写入Google Cloud Storage

Google Cloud Functions(Python)中的随机连接错误

Python子进程在Google Cloud Functions中不起作用

Google Cloud Functions 中的 HTTPS 是否支持使用 PKI 的 mTLS?

Google Cloud Storage与Google Cloud CDN

如何通过 Pandas 从 Google Cloud Function 中的 Google Cloud Storage 访问 csv 文件?

如何从Cloud Functions连接Google Cloud SQL?

使用Dataflow + Beam + Python从Google Cloud Storage读取Shapefile

使用Python从Google Cloud Storage逐行读取巨大的JSON

用于通过REST API在Google Cloud Platform中的Cloud Armor中更新规则的Python脚本

如何使用Cloud Functions中的Cloud DataStore或Cloud SQL for Firebase?

Google Cloud Functions的开发环境

Google Cloud Functions预热时间

Google Cloud Functions通知iOS

如何使用python脚本提取多部分zip文件以在Google Cloud Function中运行

在 Google Cloud Storage Bucket 中运行 PHP

Google Cloud Storage中的速率限制

Google Cloud Storage中备份的隐私

Google Cloud Storage中的Concat Avro文件