boto3.s3.inject

Module Contents

inject_s3_transfer_methods(class_attributes, **kwargs)
inject_bucket_methods(class_attributes, **kwargs)
inject_object_methods(class_attributes, **kwargs)
inject_object_summary_methods(class_attributes, **kwargs)
bucket_load(self, *args, **kwargs)

Calls s3.Client.list_buckets() to update the attributes of the Bucket resource.

object_summary_load(self, *args, **kwargs)

Calls s3.Client.head_object to update the attributes of the ObjectSummary resource.

upload_file(self, Filename, Bucket, Key, ExtraArgs=None, Callback=None, Config=None)

Upload a file to an S3 object.

Usage:

import boto3
s3 = boto3.resource('s3')
s3.meta.client.upload_file('/tmp/hello.txt', 'mybucket', 'hello.txt')

Similar behavior as S3Transfer’s upload_file() method, except that parameters are capitalized. Detailed examples can be found at S3Transfer’s Usage.

Parameters:
  • Filename (str) – The path to the file to upload.
  • Bucket (str) – The name of the bucket to upload to.
  • Key (str) – The name of the key to upload to.
  • ExtraArgs (dict) – Extra arguments that may be passed to the client operation.
  • Callback (function) – A method which takes a number of bytes transferred to be periodically called during the upload.
  • Config (boto3.s3.transfer.TransferConfig) – The transfer configuration to be used when performing the transfer.
download_file(self, Bucket, Key, Filename, ExtraArgs=None, Callback=None, Config=None)

Download an S3 object to a file.

Usage:

import boto3
s3 = boto3.resource('s3')
s3.meta.client.download_file('mybucket', 'hello.txt', '/tmp/hello.txt')

Similar behavior as S3Transfer’s download_file() method, except that parameters are capitalized. Detailed examples can be found at S3Transfer’s Usage.

Parameters:
  • Bucket (str) – The name of the bucket to download from.
  • Key (str) – The name of the key to download from.
  • Filename (str) – The path to the file to download to.
  • ExtraArgs (dict) – Extra arguments that may be passed to the client operation.
  • Callback (function) – A method which takes a number of bytes transferred to be periodically called during the download.
  • Config (boto3.s3.transfer.TransferConfig) – The transfer configuration to be used when performing the transfer.
bucket_upload_file(self, Filename, Key, ExtraArgs=None, Callback=None, Config=None)

Upload a file to an S3 object.

Usage:

import boto3
s3 = boto3.resource('s3')
s3.Bucket('mybucket').upload_file('/tmp/hello.txt', 'hello.txt')

Similar behavior as S3Transfer’s upload_file() method, except that parameters are capitalized. Detailed examples can be found at S3Transfer’s Usage.

Parameters:
  • Filename (str) – The path to the file to upload.
  • Key (str) – The name of the key to upload to.
  • ExtraArgs (dict) – Extra arguments that may be passed to the client operation.
  • Callback (function) – A method which takes a number of bytes transferred to be periodically called during the upload.
  • Config (boto3.s3.transfer.TransferConfig) – The transfer configuration to be used when performing the transfer.
bucket_download_file(self, Key, Filename, ExtraArgs=None, Callback=None, Config=None)

Download an S3 object to a file.

Usage:

import boto3
s3 = boto3.resource('s3')
s3.Bucket('mybucket').download_file('hello.txt', '/tmp/hello.txt')

Similar behavior as S3Transfer’s download_file() method, except that parameters are capitalized. Detailed examples can be found at S3Transfer’s Usage.

Parameters:
  • Key (str) – The name of the key to download from.
  • Filename (str) – The path to the file to download to.
  • ExtraArgs (dict) – Extra arguments that may be passed to the client operation.
  • Callback (function) – A method which takes a number of bytes transferred to be periodically called during the download.
  • Config (boto3.s3.transfer.TransferConfig) – The transfer configuration to be used when performing the transfer.
object_upload_file(self, Filename, ExtraArgs=None, Callback=None, Config=None)

Upload a file to an S3 object.

Usage:

import boto3
s3 = boto3.resource('s3')
s3.Object('mybucket', 'hello.txt').upload_file('/tmp/hello.txt')

Similar behavior as S3Transfer’s upload_file() method, except that parameters are capitalized. Detailed examples can be found at S3Transfer’s Usage.

Parameters:
  • Filename (str) – The path to the file to upload.
  • ExtraArgs (dict) – Extra arguments that may be passed to the client operation.
  • Callback (function) – A method which takes a number of bytes transferred to be periodically called during the upload.
  • Config (boto3.s3.transfer.TransferConfig) – The transfer configuration to be used when performing the transfer.
object_download_file(self, Filename, ExtraArgs=None, Callback=None, Config=None)

Download an S3 object to a file.

Usage:

import boto3
s3 = boto3.resource('s3')
s3.Object('mybucket', 'hello.txt').download_file('/tmp/hello.txt')

Similar behavior as S3Transfer’s download_file() method, except that parameters are capitalized. Detailed examples can be found at S3Transfer’s Usage.

Parameters:
  • Filename (str) – The path to the file to download to.
  • ExtraArgs (dict) – Extra arguments that may be passed to the client operation.
  • Callback (function) – A method which takes a number of bytes transferred to be periodically called during the download.
  • Config (boto3.s3.transfer.TransferConfig) – The transfer configuration to be used when performing the transfer.
copy(self, CopySource, Bucket, Key, ExtraArgs=None, Callback=None, SourceClient=None, Config=None)

Copy an object from one S3 location to another.

This is a managed transfer which will perform a multipart copy in multiple threads if necessary.

Usage:

import boto3
s3 = boto3.resource('s3')
copy_source = {
    'Bucket': 'mybucket',
    'Key': 'mykey'
}
s3.meta.client.copy(copy_source, 'otherbucket', 'otherkey')
Parameters:
  • CopySource (dict) – The name of the source bucket, key name of the source object, and optional version ID of the source object. The dictionary format is: {'Bucket': 'bucket', 'Key': 'key', 'VersionId': 'id'}. Note that the VersionId key is optional and may be omitted.
  • Bucket (str) – The name of the bucket to copy to
  • Key (str) – The name of the key to copy to
  • ExtraArgs (dict) – Extra arguments that may be passed to the client operation
  • Callback (function) – A method which takes a number of bytes transferred to be periodically called during the copy.
  • SourceClient (botocore or boto3 Client) – The client to be used for operation that may happen at the source object. For example, this client is used for the head_object that determines the size of the copy. If no client is provided, the current client is used as the client for the source object.
  • Config (boto3.s3.transfer.TransferConfig) – The transfer configuration to be used when performing the copy.
bucket_copy(self, CopySource, Key, ExtraArgs=None, Callback=None, SourceClient=None, Config=None)

Copy an object from one S3 location to an object in this bucket.

This is a managed transfer which will perform a multipart copy in multiple threads if necessary.

Usage:

import boto3
s3 = boto3.resource('s3')
copy_source = {
    'Bucket': 'mybucket',
    'Key': 'mykey'
}
bucket = s3.Bucket('otherbucket')
bucket.copy(copy_source, 'otherkey')
Parameters:
  • CopySource (dict) – The name of the source bucket, key name of the source object, and optional version ID of the source object. The dictionary format is: {'Bucket': 'bucket', 'Key': 'key', 'VersionId': 'id'}. Note that the VersionId key is optional and may be omitted.
  • Key (str) – The name of the key to copy to
  • ExtraArgs (dict) – Extra arguments that may be passed to the client operation
  • Callback (function) – A method which takes a number of bytes transferred to be periodically called during the copy.
  • SourceClient (botocore or boto3 Client) – The client to be used for operation that may happen at the source object. For example, this client is used for the head_object that determines the size of the copy. If no client is provided, the current client is used as the client for the source object.
  • Config (boto3.s3.transfer.TransferConfig) – The transfer configuration to be used when performing the copy.
object_copy(self, CopySource, ExtraArgs=None, Callback=None, SourceClient=None, Config=None)

Copy an object from one S3 location to this object.

This is a managed transfer which will perform a multipart copy in multiple threads if necessary.

Usage:

import boto3
s3 = boto3.resource('s3')
copy_source = {
    'Bucket': 'mybucket',
    'Key': 'mykey'
}
bucket = s3.Bucket('otherbucket')
obj = bucket.Object('otherkey')
obj.copy(copy_source)
Parameters:
  • CopySource (dict) – The name of the source bucket, key name of the source object, and optional version ID of the source object. The dictionary format is: {'Bucket': 'bucket', 'Key': 'key', 'VersionId': 'id'}. Note that the VersionId key is optional and may be omitted.
  • ExtraArgs (dict) – Extra arguments that may be passed to the client operation
  • Callback (function) – A method which takes a number of bytes transferred to be periodically called during the copy.
  • SourceClient (botocore or boto3 Client) – The client to be used for operation that may happen at the source object. For example, this client is used for the head_object that determines the size of the copy. If no client is provided, the current client is used as the client for the source object.
  • Config (boto3.s3.transfer.TransferConfig) – The transfer configuration to be used when performing the copy.
upload_fileobj(self, Fileobj, Bucket, Key, ExtraArgs=None, Callback=None, Config=None)

Upload a file-like object to S3.

The file-like object must be in binary mode.

This is a managed transfer which will perform a multipart upload in multiple threads if necessary.

Usage:

import boto3
s3 = boto3.client('s3')

with open('filename', 'rb') as data:
    s3.upload_fileobj(data, 'mybucket', 'mykey')
Parameters:
  • Fileobj (a file-like object) – A file-like object to upload. At a minimum, it must implement the read method, and must return bytes.
  • Bucket (str) – The name of the bucket to upload to.
  • Key (str) – The name of the key to upload to.
  • ExtraArgs (dict) – Extra arguments that may be passed to the client operation.
  • Callback (function) – A method which takes a number of bytes transferred to be periodically called during the upload.
  • Config (boto3.s3.transfer.TransferConfig) – The transfer configuration to be used when performing the upload.
bucket_upload_fileobj(self, Fileobj, Key, ExtraArgs=None, Callback=None, Config=None)

Upload a file-like object to this bucket.

The file-like object must be in binary mode.

This is a managed transfer which will perform a multipart upload in multiple threads if necessary.

Usage:

import boto3
s3 = boto3.resource('s3')
bucket = s3.Bucket('mybucket')

with open('filename', 'rb') as data:
    bucket.upload_fileobj(data, 'mykey')
Parameters:
  • Fileobj (a file-like object) – A file-like object to upload. At a minimum, it must implement the read method, and must return bytes.
  • Key (str) – The name of the key to upload to.
  • ExtraArgs (dict) – Extra arguments that may be passed to the client operation.
  • Callback (function) – A method which takes a number of bytes transferred to be periodically called during the upload.
  • Config (boto3.s3.transfer.TransferConfig) – The transfer configuration to be used when performing the upload.
object_upload_fileobj(self, Fileobj, ExtraArgs=None, Callback=None, Config=None)

Upload a file-like object to this object.

The file-like object must be in binary mode.

This is a managed transfer which will perform a multipart upload in multiple threads if necessary.

Usage:

import boto3
s3 = boto3.resource('s3')
bucket = s3.Bucket('mybucket')
obj = bucket.Object('mykey')

with open('filename', 'rb') as data:
    obj.upload_fileobj(data)
Parameters:
  • Fileobj (a file-like object) – A file-like object to upload. At a minimum, it must implement the read method, and must return bytes.
  • ExtraArgs (dict) – Extra arguments that may be passed to the client operation.
  • Callback (function) – A method which takes a number of bytes transferred to be periodically called during the upload.
  • Config (boto3.s3.transfer.TransferConfig) – The transfer configuration to be used when performing the upload.
download_fileobj(self, Bucket, Key, Fileobj, ExtraArgs=None, Callback=None, Config=None)

Download an object from S3 to a file-like object.

The file-like object must be in binary mode.

This is a managed transfer which will perform a multipart download in multiple threads if necessary.

Usage:

import boto3
s3 = boto3.client('s3')

with open('filename', 'wb') as data:
    s3.download_fileobj('mybucket', 'mykey', data)
Parameters:
  • Fileobj (a file-like object) – A file-like object to download into. At a minimum, it must implement the write method and must accept bytes.
  • Bucket (str) – The name of the bucket to download from.
  • Key (str) – The name of the key to download from.
  • ExtraArgs (dict) – Extra arguments that may be passed to the client operation.
  • Callback (function) – A method which takes a number of bytes transferred to be periodically called during the download.
  • Config (boto3.s3.transfer.TransferConfig) – The transfer configuration to be used when performing the download.
bucket_download_fileobj(self, Key, Fileobj, ExtraArgs=None, Callback=None, Config=None)

Download an object from this bucket to a file-like-object.

The file-like object must be in binary mode.

This is a managed transfer which will perform a multipart download in multiple threads if necessary.

Usage:

import boto3
s3 = boto3.resource('s3')
bucket = s3.Bucket('mybucket')

with open('filename', 'wb') as data:
    bucket.download_fileobj('mykey', data)
Parameters:
  • Fileobj (a file-like object) – A file-like object to download into. At a minimum, it must implement the write method and must accept bytes.
  • Key (str) – The name of the key to download from.
  • ExtraArgs (dict) – Extra arguments that may be passed to the client operation.
  • Callback (function) – A method which takes a number of bytes transferred to be periodically called during the download.
  • Config (boto3.s3.transfer.TransferConfig) – The transfer configuration to be used when performing the download.
object_download_fileobj(self, Fileobj, ExtraArgs=None, Callback=None, Config=None)

Download this object from S3 to a file-like object.

The file-like object must be in binary mode.

This is a managed transfer which will perform a multipart download in multiple threads if necessary.

Usage:

import boto3
s3 = boto3.resource('s3')
bucket = s3.Bucket('mybucket')
obj = bucket.Object('mykey')

with open('filename', 'wb') as data:
    obj.download_fileobj(data)
Parameters:
  • Fileobj (a file-like object) – A file-like object to download into. At a minimum, it must implement the write method and must accept bytes.
  • ExtraArgs (dict) – Extra arguments that may be passed to the client operation.
  • Callback (function) – A method which takes a number of bytes transferred to be periodically called during the download.
  • Config (boto3.s3.transfer.TransferConfig) – The transfer configuration to be used when performing the download.