File: s3-example-download-file.rst

package info (click to toggle)
python-boto3 1.26.27%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 7,880 kB
  • sloc: python: 12,629; makefile: 128
file content (55 lines) | stat: -rw-r--r-- 2,038 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
.. Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.

   This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0
   International License (the "License"). You may not use this file except in compliance with the
   License. A copy of the License is located at http://creativecommons.org/licenses/by-nc-sa/4.0/.

   This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
   either express or implied. See the License for the specific language governing permissions and
   limitations under the License.


#################
Downloading files
#################

The methods provided by the AWS SDK for Python to download files are similar 
to those provided to upload files.

The ``download_file`` method accepts the names of the bucket and object to 
download and the filename to save the file to.

.. code-block:: python

    import boto3

    s3 = boto3.client('s3')
    s3.download_file('BUCKET_NAME', 'OBJECT_NAME', 'FILE_NAME')


The ``download_fileobj`` method accepts a writeable file-like object. The file 
object must be opened in binary mode, not text mode.

.. code-block:: python

    s3 = boto3.client('s3')
    with open('FILE_NAME', 'wb') as f:
        s3.download_fileobj('BUCKET_NAME', 'OBJECT_NAME', f)


Like their upload cousins, the download methods are provided by the 
S3 ``Client``, ``Bucket``, and ``Object`` classes, and each class provides 
identical functionality. Use whichever class is convenient.

Also like the upload methods, the download methods support the optional 
``ExtraArgs`` and ``Callback`` parameters.

The list of valid ``ExtraArgs`` settings for the download methods is 
specified in the ``ALLOWED_DOWNLOAD_ARGS`` attribute of the ``S3Transfer`` 
object at :py:attr:`boto3.s3.transfer.S3Transfer.ALLOWED_DOWNLOAD_ARGS`.

The download method's ``Callback`` parameter is used for the same purpose 
as the upload method's. The upload and download methods can both invoke the 
same ``Callback`` class.