简体   繁体   English

有没有办法使用boto3直接将文件写入S3?

[英]Any way to write files DIRECTLY to S3 using boto3?

I wrote a python script to process very large files (few TB in total), which I'll run on an EC2 instance. 我写了一个python脚本来处理非常大的文件(总共几TB),我将在EC2实例上运行。 Afterwards, I want to store the processed files in an S3 bucket. 之后,我想将处理过的文件存储在S3存储桶中。 Currently, my script first saves the data to disk and then uploads it to S3. 目前,我的脚本首先将数据保存到磁盘,然后将其上传到S3。 Unfortunately, this will be quite costly given the extra time spent waiting for the instance to first write to disk and then upload. 不幸的是,考虑到等待实例首先写入磁盘然后上载所花费的额外时间,这将是非常昂贵的。

Is there any way to use boto3 to write files directly to an S3 bucket? 有没有办法使用boto3直接将文件写入S3存储桶?

Edit: to clarify my question, I'm asking if I have an object in memory, writing that object directly to S3 without first saving the object onto disk. 编辑:为了澄清我的问题,我问我是否在内存中有一个对象,直接将该对象写入S3而不先将对象保存到磁盘上。

You can use put_object for this. 你可以使用put_object Just pass in your file object as body. 只需将您的文件对象作为正文传入。

For example: 例如:

import boto3

client = boto3.client('s3')
response = client.put_object( 
    Bucket='your-s3-bucket-name',
    Body='bytes or seekable file-like object',
    Key='Object key for which the PUT operation was initiated'
)

It's working with the S3 put_object method: 它使用S3 put_object方法:

key = 'filename'
response = s3.put_object(Bucket='Bucket_Name',
                         Body=json_data,
                         Key=key)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM