2016-01-25 57 views
1

我在Python中使用Boto從S3存儲桶下載文件。S3Transfer download_file出錯,但client.download_file正常工作

這工作得很好:

import boto3 
s3_client = boto3.resource('s3') 
s3_client.meta.client.download_file('mybucket', 'db/file.00.txt', '/work/testing/file.00.txt') 

我想用S3Transfer到多自動使用,我將用一些非常巨大的文件(900 MB +)來工作。

但是,當我嘗試以下方法,它失敗:

import boto3 
from boto3.s3.transfer import S3Transfer 
s3_client = boto3.resource('s3') 
transfer = S3Transfer(s3_client) 
transfer.download_file('mybucket', 'db/file.00.txt', '/work/testing/file.00.txt') 

我得到的錯誤如下:

Traceback (most recent call last): 
    File "/work/sparkrun/CommonBlast.py", line 126, in <module> 
    transfer.download_file('mybucket', 'db/file.00.txt', '/work/testing/file.00.txt') 
    File "/usr/local/lib/python2.7/dist-packages/boto3/s3/transfer.py", line 658, in download_file 
    object_size = self._object_size(bucket, key, extra_args) 
    File "/usr/local/lib/python2.7/dist-packages/boto3/s3/transfer.py", line 723, in _object_size 
    return self._client.head_object(
AttributeError: 's3.ServiceResource' object has no attribute 'head_object' 

參數的download_file方法是相同的。我正在使用最新版本的boto(1.2.3)。到底是怎麼回事?

回答

4

S3Transfer需要客戶端時,您正在傳遞資源。

無論如何,您也永遠不需要創建自己的對象S3Transfer,因爲它的方法已經添加到客戶端和資源。

import boto3 
client = boto3.client('s3') 
client.download_file('bucket', 'key', 'filename.txt') 

resource = boto3.resource('s3') 
bucket = resource.Bucket('bucket') 
bucket.download_file('key', 'filename.txt') 

obj = bucket.Object('key') 
obj.download_file('filename.txt') 

docs

+0

啊,那是爲什麼。現在效果很好,謝謝。我想這會教會我更加關注。 –

相關問題