AWS Code Sample
Catalog

upload_file.py

upload_file.py demonstrates how to upload a file to Amazon S3.

# Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. # # This file is 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/ # # 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. import logging import boto3 from botocore.exceptions import ClientError def upload_file(file_name, bucket, object_name=None): """Upload a file to an S3 bucket :param file_name: File to upload :param bucket: Bucket to upload to :param object_name: S3 object name. If not specified then same as file_name :return: True if file was uploaded, else False """ # If S3 object_name was not specified, use file_name if object_name is None: object_name = file_name # Upload the file s3_client = boto3.client('s3') try: response = s3_client.upload_file(file_name, bucket, object_name) except ClientError as e: logging.error(e) return False return True def main(): """Exercise upload_file()""" # Set these values before running the program bucket_name = 'BUCKET_NAME' file_name = 'FILE_NAME' object_name = 'OBJECT_NAME' # Set up logging logging.basicConfig(level=logging.DEBUG, format='%(levelname)s: %(asctime)s: %(message)s') # Upload a file response = upload_file(file_name, bucket_name, object_name) if response: logging.info('File was uploaded') if __name__ == '__main__': main()

Sample Details

Service: s3

Last tested: 2019-03-22

Author: AWS

On this page: