我有一些 json 然后將其轉換為 csv 檔案,現在同一個檔案應該保存到 s3 而不是我的本地檔案夾。
logs = {
"testing1": "testing1_value",
"testing2": "testing2_value",
"testing3": {"testing3a": "testing1_value3a"},
"testing4": {"testing4a": {"testing4a1": "testing_value4a1"}}
}
file_name = "testing_file.csv"
bucket_name = "testing_bucket"
file_to_save_in_path = "path_in_s3/testing_file.csv"
client = boto3.client("s3")
from fastapi.responses import StreamingResponse
stream = await create_csv_for_download(logs, file_name)
response = StreamingResponse(iter([stream]), media_type="text/csv")
response.headers["Content-Disposition"] = f"attachment; filename={file_name}"
client.put_object(Bucket=bucket_name, Key=file_to_save_in_path, Body=response)
client.upload_file(response, bucket_name, file_to_save_in_path)
現在回應就像一些東西 => <starlette.responses.StreamingResponse object at 0x7fe084e75fd0>
如何將該回應保存在 s3 中的正確 csv 檔案中。
當我使用 client.put_object 時出錯:如下所示
**
Parameter validation failed:
Invalid type for parameter Body, value: <starlette.responses.StreamingResponse object at 0x7fe084e75fd0>, type: <class 'starlette.responses.StreamingResponse'>, valid types: <class 'bytes'>, <class 'bytearray'>, file-like object
**
uj5u.com熱心網友回復:
錯誤資訊相當清楚:
Invalid type for parameter Body, value: <starlette.responses.StreamingResponse object at 0x7fe084e75fd0>,
type: <class 'starlette.responses.StreamingResponse'>,
valid types: <class 'bytes'>, <class 'bytearray'>, file-like object
它告訴您不能將StreamingResponse物件傳遞給put_object(),它必須是位元組陣列或檔案物件。假設您的create_csv_for_download()函式回傳一個流物件,您應該只從中讀取位元組,并將其發送到put_object().
此外,您設定的 HTTP 標頭StreamingResponse應直接傳遞給put_object():
import boto3
import csv
import io
def create_csv_for_download(logs, filename):
# Just a stub so this is a self-contained example
ret = io.StringIO()
cw = csv.writer(ret)
for key, value in logs.items():
cw.writerow([key, str(value)])
return ret
logs = {
"testing1": "testing1_value",
"testing2": "testing2_value",
"testing3": {"testing3a": "testing1_value3a"},
"testing4": {"testing4a": {"testing4a1": "testing_value4a1"}}
}
file_name = "testing_file.csv"
bucket_name = "testing_bucket"
file_to_save_in_path = "path_in_s3/testing_file.csv"
client = boto3.client("s3")
stream = create_csv_for_download(logs, file_name)
# Ready out the body from the stream returned
body = stream.read()
if isinstance(body, str):
# If this stream returns a string, encode it to a byte array
body = body.encode("utf-8")
client.put_object(
Bucket=bucket_name,
Key=file_to_save_in_path,
Body=body,
ContentDisposition=f"attachment; filename={file_name}",
ContentType="test/csv",
# Uncomment the following line if you want the link to be
# publicly downloadable from S3 without credentials:
# ACL="public-read",
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/439653.html
標籤:python-3.x 亚马逊-s3 博托3
