web-dev-qa-db-ja.com

boto3を使ってS3オブジェクトをファイルに保存する方法

AWS用の新しい boto クライアントを使用して「Hello World」をやろうとしています。

私が持っているユースケースは非常に簡単です:S3からオブジェクトを取得してファイルに保存する。

Boto 2.Xでは、私はこのようにするでしょう:

import boto
key = boto.connect_s3().get_bucket('foo').get_key('foo')
key.get_contents_to_filename('/tmp/foo')

で3。私は同じことをするきれいな方法を見つけることができないので、私は手動で "ストリーミング"オブジェクトを反復処理します。

import boto3
key = boto3.resource('s3').Object('fooo', 'docker/my-image.tar.gz').get()
with open('/tmp/my-image.tar.gz', 'w') as f:
    chunk = key['Body'].read(1024*8)
    while chunk:
        f.write(chunk)
        chunk = key['Body'].read(1024*8)

または

import boto3
key = boto3.resource('s3').Object('fooo', 'docker/my-image.tar.gz').get()
with open('/tmp/my-image.tar.gz', 'w') as f:
    for chunk in iter(lambda: key['Body'].read(4096), b''):
        f.write(chunk)

そしてそれはうまくいきます。私は同じタスクを実行する "ネイティブ"のboto3関数があるのだろうかと思いましたか?

102
Vor

最近Boto3に入ったカスタマイズがあり、これは(とりわけ)これを助けます。現在は低レベルのS3クライアントに公開されており、次のように使用できます。

s3_client = boto3.client('s3')
open('hello.txt').write('Hello, world!')

# Upload the file to S3
s3_client.upload_file('hello.txt', 'MyBucket', 'hello-remote.txt')

# Download the file from S3
s3_client.download_file('MyBucket', 'hello-remote.txt', 'hello2.txt')
print(open('hello2.txt').read())

これらの関数は自動的にファイルの読み書きを処理し、大きなファイルに対してマルチパートアップロードを並行して行います。

171
Daniel

Boto2のようなset_contents_from_stringをシミュレートしたいあなたのために、あなたは試すことができます。

import boto3
from cStringIO import StringIO

s3c = boto3.client('s3')
contents = 'My string to save to S3 object'
target_bucket = 'hello-world.by.vor'
target_file = 'data/hello.txt'
fake_handle = StringIO(contents)

# notice if you do fake_handle.read() it reads like a file handle
s3c.put_object(Bucket=target_bucket, Key=target_file, Body=fake_handle.read())

Python 3の場合:

Python3では、両方とも StringIOとcStringIOは消えています 。次のようにStringIOインポートを使用します。

from io import StringIO

両方のバージョンをサポートするには

try:
   from StringIO import StringIO
except ImportError:
   from io import StringIO
36
cgseller
# Preface: File is json with contents: {'name': 'Android', 'status': 'ERROR'}

import boto3
import io

s3 = boto3.resource(
    's3',
    aws_access_key_id='my_access_id',
    aws_secret_access_key='my_secret_key'
)

obj = s3.Object('my-bucket', 'key-to-file.json')
data = io.BytesIO()
obj.download_fileobj(data)

# object is now a bytes string, Converting it to a dict:
new_dict = json.loads(data.getvalue().decode("utf-8"))

print(new_dict['status']) 
# Should print "Error"
11
Lord Sumner

デフォルトの設定とは異なる設定でファイルを読みたい場合は、直接 mpu.aws.s3_download(s3path, destination) を使用するか、コピーしたコードを使用してください。

def s3_download(source, destination,
                exists_strategy='raise',
                profile_name=None):
    """
    Copy a file from an S3 source to a local destination.

    Parameters
    ----------
    source : str
        Path starting with s3://, e.g. 's3://bucket-name/key/foo.bar'
    destination : str
    exists_strategy : {'raise', 'replace', 'abort'}
        What is done when the destination already exists?
    profile_name : str, optional
        AWS profile

    Raises
    ------
    botocore.exceptions.NoCredentialsError
        Botocore is not able to find your credentials. Either specify
        profile_name or add the environment variables AWS_ACCESS_KEY_ID,
        AWS_SECRET_ACCESS_KEY and AWS_SESSION_TOKEN.
        See https://boto3.readthedocs.io/en/latest/guide/configuration.html
    """
    exists_strategies = ['raise', 'replace', 'abort']
    if exists_strategy not in exists_strategies:
        raise ValueError('exists_strategy \'{}\' is not in {}'
                         .format(exists_strategy, exists_strategies))
    session = boto3.Session(profile_name=profile_name)
    s3 = session.resource('s3')
    bucket_name, key = _s3_path_split(source)
    if os.path.isfile(destination):
        if exists_strategy is 'raise':
            raise RuntimeError('File \'{}\' already exists.'
                               .format(destination))
        Elif exists_strategy is 'abort':
            return
    s3.Bucket(bucket_name).download_file(key, destination)
1
Martin Thoma

注:認証を個別に設定していることを前提としています。以下のコードはS3バケットから単一のオブジェクトをダウンロードすることです。

import boto3

#initiate s3 client 
s3 = boto3.resource('s3')

#Download object to the file    
s3.Bucket('mybucket').download_file('hello.txt', '/tmp/hello.txt')
1
Tushar Niras