本文介绍了如何使用boto3将文件或数据写入S3对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在boto 2中,可以使用以下方法写入S3对象:
In boto 2, you can write to an S3 object using these methods:
- Key.set_contents_from_string()
- Key.set_contents_from_file()
- Key.set_contents_from_filename()
- Key.set_contents_from_stream()
- Key.set_contents_from_string()
- Key.set_contents_from_file()
- Key.set_contents_from_filename()
- Key.set_contents_from_stream()
是否有boto 3等效项?将数据保存到S3上存储的对象的boto3方法是什么?
Is there a boto 3 equivalent? What is the boto3 method for saving data to an object stored on S3?
推荐答案
在boto 3中,"Key.set_contents_from_"方法被替换为
In boto 3, the 'Key.set_contents_from_' methods were replaced by
例如:
import boto3
some_binary_data = b'Here we have some data'
more_binary_data = b'Here we have some more data'
# Method 1: Object.put()
s3 = boto3.resource('s3')
object = s3.Object('my_bucket_name', 'my/key/including/filename.txt')
object.put(Body=some_binary_data)
# Method 2: Client.put_object()
client = boto3.client('s3')
client.put_object(Body=more_binary_data, Bucket='my_bucket_name', Key='my/key/including/anotherfilename.txt')
或者,二进制数据可以来自读取文件,如比较boto 2和boto 3的官方文档:
Alternatively, the binary data can come from reading a file, as described in the official docs comparing boto 2 and boto 3:
# Boto 2.x
from boto.s3.key import Key
key = Key('hello.txt')
key.set_contents_from_file('/tmp/hello.txt')
# Boto 3
s3.Object('mybucket', 'hello.txt').put(Body=open('/tmp/hello.txt', 'rb'))
这篇关于如何使用boto3将文件或数据写入S3对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!