2016-09-17 11 views
0

私はS3にファイルをアップロードする必要があり、私はどのboto3 API呼び出しを使用すべきか疑問に思っていましたか?AWS S3バケットアップロード/転送with boto3

私はboto3ドキュメントの二つの方法を発見した

:)私は(client.upload_fileを使用しています

を...

#!/usr/bin/python 
import boto3 
session = Session(aws_access_key_id, aws_secret_access_key, region) 
s3 = session.resource('s3') 
s3.Bucket('my_bucket').upload_file('/tmp/hello.txt', 'hello.txt') 

S3Transfer.upload_file()を使用しますか?

#!/usr/bin/python 
import boto3 
session = Session(aws_access_key_id, aws_secret_access_key, region) 
S3Transfer(session).upload_file('/tmp/hello.txt', 'my_bucket', 'hello.txt') 

ご了承ください。前もって感謝します。

。 。 。

可能な解決策...

# http://boto3.readthedocs.io/en/latest/reference/services/s3.html#examples 
# http://boto3.readthedocs.io/en/latest/reference/services/s3.html#S3.Client.put_object 
# http://boto3.readthedocs.io/en/latest/reference/services/s3.html#S3.Client.get_object 

client = boto3.client("s3", "us-west-1", aws_access_key_id = "xxxxxxxx", aws_secret_access_key = "xxxxxxxxxx") 


with open('drop_spot/my_file.txt') as file: 
    client.put_object(Bucket='s3uploadertestdeleteme', Key='my_file.txt', Body=file) 


response = client.get_object(Bucket='s3uploadertestdeleteme', Key='my_file.txt') 

print("Done, response body: {}".format(response['Body'].read())) 
+0

適切な場合は、[AWS Command-Line Interface(CLI)](http://aws.amazon.com/cli/)を使用してファイルをコピーできます。 'aws s3 cp'または' aws s3 sync'コマンドを使うと、すべての面倒な作業ができます。 –

+0

Minioクライアントの 'mc mirror'や' mc cp'でも同じ[https://docs.minio.io/docs/minio-client-complete-guide#mirror](https://docs.minio) .io/docs/minio-client-complete-guide#mirror)それがうまくいくと思っています。 – koolhead17

答えて

1

それは、クライアント上のメソッドを使用することをお勧めします。それらは同じですが、クライアントメソッドを使用すると、自分で設定する必要はありません。

関連する問題