| 1 |
.. Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 |
|
| 3 |
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 |
| 4 |
International License (the "License"). You may not use this file except in compliance with the |
| 5 |
License. A copy of the License is located at http://creativecommons.org/licenses/by-nc-sa/4.0/. |
| 6 |
|
| 7 |
This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
| 8 |
either express or implied. See the License for the specific language governing permissions and |
| 9 |
limitations under the License. |
| 10 |
|
| 11 |
|
| 12 |
################# |
| 13 |
Downloading files |
| 14 |
################# |
| 15 |
|
| 16 |
The methods provided by the AWS SDK for Python to download files are similar |
| 17 |
to those provided to upload files. |
| 18 |
|
| 19 |
The ``download_file`` method accepts the names of the bucket and object to |
| 20 |
download and the filename to save the file to. |
| 21 |
|
| 22 |
.. code-block:: python |
| 23 |
|
| 24 |
import boto3 |
| 25 |
|
| 26 |
s3 = boto3.client('s3') |
| 27 |
s3.download_file('amzn-s3-demo-bucket', 'OBJECT_NAME', 'FILE_NAME') |
| 28 |
|
| 29 |
|
| 30 |
The ``download_fileobj`` method accepts a writeable file-like object. The file |
| 31 |
object must be opened in binary mode, not text mode. |
| 32 |
|
| 33 |
.. code-block:: python |
| 34 |
|
| 35 |
s3 = boto3.client('s3') |
| 36 |
with open('FILE_NAME', 'wb') as f: |
| 37 |
s3.download_fileobj('amzn-s3-demo-bucket', 'OBJECT_NAME', f) |
| 38 |
|
| 39 |
|
| 40 |
Like their upload cousins, the download methods are provided by the |
| 41 |
S3 ``Client``, ``Bucket``, and ``Object`` classes, and each class provides |
| 42 |
identical functionality. Use whichever class is convenient. |
| 43 |
|
| 44 |
Also like the upload methods, the download methods support the optional |
| 45 |
``ExtraArgs`` and ``Callback`` parameters. |
| 46 |
|
| 47 |
The list of valid ``ExtraArgs`` settings for the download methods is |
| 48 |
specified in the ``ALLOWED_DOWNLOAD_ARGS`` attribute of the ``S3Transfer`` |
| 49 |
object at :py:attr:`boto3.s3.transfer.S3Transfer.ALLOWED_DOWNLOAD_ARGS`. |
| 50 |
|
| 51 |
The download method's ``Callback`` parameter is used for the same purpose |
| 52 |
as the upload method's. The upload and download methods can both invoke the |
| 53 |
same ``Callback`` class. |
| 54 |
|
| 55 |
|