Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python S3Transfer download\u文件出错,但client.download\u文件工作正常_Python_Amazon Web Services_Amazon S3_Boto3 - Fatal编程技术网

Python S3Transfer download\u文件出错,但client.download\u文件工作正常

Python S3Transfer download\u文件出错,但client.download\u文件工作正常,python,amazon-web-services,amazon-s3,boto3,Python,Amazon Web Services,Amazon S3,Boto3,我正在使用Python中的Boto从我的S3存储桶下载一个文件 这很好: import boto3 s3_client = boto3.resource('s3') s3_client.meta.client.download_file('mybucket', 'db/file.00.txt', '/work/testing/file.00.txt') 我想使用S3Transfer自动使用multipart,因为我将处理一些相当大的文件(900 MB+) 但是,当我尝试以下操作时,它失败了:

我正在使用Python中的Boto从我的S3存储桶下载一个文件

这很好:

import boto3
s3_client = boto3.resource('s3')
s3_client.meta.client.download_file('mybucket', 'db/file.00.txt', '/work/testing/file.00.txt')
我想使用S3Transfer自动使用multipart,因为我将处理一些相当大的文件(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'
回溯(最近一次呼叫最后一次):
文件“/work/sparkrun/CommonBlast.py”,第126行,在
transfer.download_文件('mybucket','db/file.00.txt','/work/testing/file.00.txt'))
文件“/usr/local/lib/python2.7/dist packages/boto3/s3/transfer.py”,第658行,在下载文件中
object\u size=self.\u object\u size(桶、键、额外参数)
文件“/usr/local/lib/python2.7/dist packages/boto3/s3/transfer.py”,第723行,大小为
返回self.\u client.head\u对象(
AttributeError:'s3.ServiceResource'对象没有属性'head\u object'

下载文件方法的参数相同。我使用的是最新版本的boto(1.2.3)。发生了什么事?

您在传递资源时,
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')

啊,这就是为什么。现在效果很好,谢谢。我想这会教我更加注意。