简体   繁体   English

如何将 memory 值中的字典数据直接写入 csv 文件中的 s3 存储桶而不写入文件然后上传

[英]How to write dictionary data in memory value directly to s3 bucket as in csv file without writing in file then uploading it

import boto3
import pandas as pd

BUCKET_NAME = ''
ACCESS_KEY_ID = ''
ACCESS_SECRET_KEY = ''
Fraudfilekey = 'fraud_CT_ID_IM_NO/ CT_PROFILE_One_to_Many_Mapping /yyyy=2021/mm=02/dd=05/2021_02_05_CT_TEST.csv'

d = {"A" : ["John","Deep","Julia","Kate","Sandy"],
                     "MonthSales" : [25,30,35,40,45]}
df = pd.DataFrame(d)

s3 = boto3.client('s3', region_name='ap-south-1', aws_access_key_id=ACCESS_KEY_ID,
                  aws_secret_access_key=ACCESS_SECRET_KEY)
def write_to_s3_oneim_to_onect(df):
    s3.put_object(Body=df, Bucket=BUCKET_NAME, Key=Fraudfilekey)

write_to_s3_oneim_to_onect(df)

How can I write dictionary value directly to s3 bucket, I get Body below error raise ParamValidationError(report=report.generate_report()) botocore.exceptions.ParamValidationError: Parameter validation failed: Invalid type for parameter Body, value: A MonthSales如何将字典值直接写入 s3 存储桶,我得到 Body 下面的错误 raise ParamValidationError(report=report.generate_report()) botocore.exceptions.ParamValidationError: Parameter validation failed: Invalid type for parameter Body, value: A MonthSales

Note: I want Headers col as IM No, CT ID in csv file注意:我希望在 csv 文件中将 Headers col 作为 IM No、CT ID

There are few ways.有几种方法。 One would be to use BytesIO as a memory buffer for the file:一种是使用BytesIO作为文件的 memory 缓冲区:

import io

def write_to_s3_oneim_to_onect(df):
    bytes_io = io.BytesIO()
    df.to_csv(bytes_io)
    s3.put_object(Body=bytes_io.getvalue(), 
                  Bucket=BUCKET_NAME, 
                  Key=Fraudfilekey)

Other would be to use s3fs which pandas supports.其他方法是使用pandas支持的 s3fs。 This would require you to install s3fs and setup AWS credetnails for it to use.这将需要您安装 s3fs 并设置 AWS credetnails以供其使用。 But once setup, writing to S3 would be:但是一旦设置,写入 S3 将是:

def write_to_s3_oneim_to_onect(df):
    df.to_csv(f"s3://{BUCKET_NAME}/{Fraudfilekey}")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从S3 bucket中直接读取图片文件到memory? - How to read image file from S3 bucket directly into memory? 尝试直接在 S3 中创建 CSV 文件并在 CSV 中写入数据 - Trying to create CSV file directly in S3 and write data in CSV 如何从s3存储桶中仅读取5条记录并在不获取csv文件的所有数据的情况下返回它 - How to read only 5 records from s3 bucket and return it without getting all data of csv file 使用 boto 3 写入 S3 存储桶中的 CSV 文件 - Writing to a CSV file in an S3 bucket using boto 3 如何将S3存储桶策略写入文件? - How to write S3 bucket policy to file? 从 S3 存储桶中的 CSV 文件中读取数据并将其存储在 python 的字典中 - Read data from a CSV file in S3 bucket and store it in a dictionary in python 如何将 .npy 文件直接写入 s3? - how to write .npy file to s3 directly? 如何在Python中将HDF5文件直接上传到S3存储桶 - How to upload HDF5 file directly to S3 bucket in Python 将 pandas 数据帧作为压缩的 CSV 直接写入 Amazon s3 存储桶? - Write pandas dataframe as compressed CSV directly to Amazon s3 bucket? 如何从 S3 存储桶中读取 CSV 文件,对其应用某些 if 语句,并编写新的更新的 CSV 文件并将其放入 S3 存储桶? - How can I read from a CSV file from an S3 bucket, apply certain if-statements to it, and write a new updated CSV file and place it in the S3 bucket?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM