# Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"). You # may not use this file except in compliance with the License. A copy of # the License is located at # # http://aws.amazon.com/apache2.0/ # # or in the "license" file accompanying this file. 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. from boto3.s3.transfer import S3Transfer from boto3 import utils from botocore.exceptions import ClientError def inject_s3_transfer_methods(class_attributes, **kwargs): utils.inject_attribute(class_attributes, 'upload_file', upload_file) utils.inject_attribute(class_attributes, 'download_file', download_file) def inject_bucket_methods(class_attributes, **kwargs): utils.inject_attribute(class_attributes, 'load', bucket_load) utils.inject_attribute(class_attributes, 'upload_file', bucket_upload_file) utils.inject_attribute( class_attributes, 'download_file', bucket_download_file) def inject_object_methods(class_attributes, **kwargs): utils.inject_attribute(class_attributes, 'upload_file', object_upload_file) utils.inject_attribute( class_attributes, 'download_file', object_download_file) def inject_object_summary_methods(class_attributes, **kwargs): utils.inject_attribute(class_attributes, 'load', object_summary_load) def bucket_load(self, *args, **kwargs): """Calls s3.Client.list_buckets() to update the attributes of the Bucket resource.""" # The docstring above is phrased this way to match what the autogenerated # docs produce. # We can't actually get the bucket's attributes from a HeadBucket, # so we need to use a ListBuckets and search for our bucket. response = self.meta.client.list_buckets() for bucket_data in response['Buckets']: if bucket_data['Name'] == self.name: self.meta.data = bucket_data break else: raise ClientError({'Error': {'Code': '404', 'Message': 'NotFound'}}, 'ListBuckets') def object_summary_load(self, *args, **kwargs): """Calls s3.Client.head_object to update the attributes of the ObjectSummary resource.""" response = self.meta.client.head_object( Bucket=self.bucket_name, Key=self.key) if 'ContentLength' in response: response['Size'] = response.pop('ContentLength') self.meta.data = response def 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 :ref:`S3Transfer's Usage `. """ transfer = S3Transfer(self, Config) return transfer.upload_file( filename=Filename, bucket=Bucket, key=Key, extra_args=ExtraArgs, callback=Callback) def 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 :ref:`S3Transfer's Usage `. """ transfer = S3Transfer(self, Config) return transfer.download_file( bucket=Bucket, key=Key, filename=Filename, extra_args=ExtraArgs, callback=Callback) def 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 :ref:`S3Transfer's Usage `. """ return self.meta.client.upload_file( Filename=Filename, Bucket=self.name, Key=Key, ExtraArgs=ExtraArgs, Callback=Callback, Config=Config) def 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 :ref:`S3Transfer's Usage `. """ return self.meta.client.download_file( Bucket=self.name, Key=Key, Filename=Filename, ExtraArgs=ExtraArgs, Callback=Callback, Config=Config) def 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 :ref:`S3Transfer's Usage `. """ return self.meta.client.upload_file( Filename=Filename, Bucket=self.bucket_name, Key=self.key, ExtraArgs=ExtraArgs, Callback=Callback, Config=Config) def 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 :ref:`S3Transfer's Usage `. """ return self.meta.client.download_file( Bucket=self.bucket_name, Key=self.key, Filename=Filename, ExtraArgs=ExtraArgs, Callback=Callback, Config=Config)