boto3には、オブジェクトの内容をファイルハンドルにコピーするget_contents_to_file
に相当するものがありますか?私はboto3で同等のものを発見していないboto3のget_contents_to_fileに相当します。
from tempfile import TemporaryFile
key = code_that_gets_key()
with TemporaryFile() as tmp_file:
key.get_contents_to_file(key, tmpfile)
:私はS3オブジェクトkey
を持っている場合
はのbotoでは、私は一時ファイルに内容をコピーすることができます。
get_contents_to_filename
の使用をdownload_file
に置き換えることができました。ただし、これはファイル名を指定する場合に適用されます。この場合、引数としてファイルハンドルを指定したいと思います。
現在、私は、コードは次のように身体を反復処理によりboto3で動作するように取得することができます:
with TemporaryFile() as tmp_file:
body = key.get()['Body']
for chunk in iter(lambda: body.read(4096), b''):
filehandle.write(chunk)
はboto3でこれを行うには良い方法はありますか?
'tmp_file.name'を' download_file() 'に渡せませんか? – helloV
@helloVこの場合、ファイル名を持つ['NamedTemporaryFile'](https://docs.python.org/2/library/tempfile.html#tempfile.NamedTemporaryFile)を使用できます。 'download_file'メソッドは同じファイルのために新しいファイルハンドルを開くので、それは良い考えかどうか分かりません。一般に、すべてのファイルハンドルにファイル名(例えば、 'StringIO'、' TemporaryFile')があるわけではありません。 – Alasdair
これは[download_fileobj](https://boto3.readthedocs.io/en/latest/reference/services/s3.html#S3.Bucket.download_fileobj)の目的ではありませんか? –