我想實作一個 aws lambda 函式,它將執行以下 python 腳本:
directory = os.fsencode(directory_in_string)
def transform_csv(csv):
for file in os.listdir(directory):
filename = os.fsdecode(file)
d = open(r'C:\Users\r.reibold\Documents\GitHub\groovy_dynamodb_api\historische_wetterdaten\{}'.format(filename))
data = json.load(d)
df_historical = pd.json_normalize(data)
#Transform to datetime
df_historical["dt"] = pd.to_datetime(df_historical["dt"], unit='s', errors='coerce').dt.strftime("%m/%d/%Y %H:%M:%S")
df_historical["dt"] = pd.to_datetime(df_historical["dt"])
.
.
.
.
我現在的問題是:
我該如何更改os. 命令,因為我需要參考 s3 存盤桶而不是我的本地目錄?
我的第一次嘗試看起來像這樣
DIRECTORY = 's3://weatherdata-templates/historische_wetterdaten/New/'
BUCKET = 'weatherdata-templates'
s3 = boto3.client('s3')
paginator = s3.get_paginator('list_objects_v2')
pages = paginator.paginate(Bucket=BUCKET, Prefix=DIRECTORY)
def lambda_handler(event, context):
for page in pages:
for obj in page['Contents']:
filename = s3.fsdecode(obj)
d = open(r's3://102135091842-weatherdata-templates/historische_wetterdaten/New/{}'.format(filename))
data = json.load(d)
df_historical = pd.json_normalize(data)
.
.
.
我是在正確的軌道上還是完全錯誤?謝謝。
uj5u.com熱心網友回復:
還沒到:)
不幸的是,您不能open(...)直接在 S3 URL 上執行操作,因為它不是檔案物件。
要加載物件內容而不將檔案存盤在本地,請嘗試使用 S3 Boto3 資源,該資源提供對 S3 SDK 的更高級別訪問。
- 從 中獲取物件的鍵
obj['Key']。 - 使用
obj.get()['Body']獲得的內容作為StreamingBody - 電話
.read()上StreamingBody獲取物件的位元組格式和其解碼為UTF-8字串(或任何其他編碼,您的檔案(S)是) - 使用以下命令將 JSON 字串轉換為 JSON 物件
json.loads(...)
import boto3
s3_resource = boto3.resource('s3')
...
def lambda_handler(event, context):
for page in pages:
for obj in page['Contents']:
obj_reference = s3_resource.Object(BUCKET, obj['Key'])
body = obj_reference.get()['Body'].read().decode('utf-8')
data = json.loads(body)
df_historical = pd.json_normalize(data)
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/354348.html
標籤:Python 亚马逊网络服务 亚马逊-s3 aws-lambda boto3
