How to upload files in Amazon S3 bucket through python?

In this post, we will tell you a very easy way to configure then upload and download files from your . If you are landed on this page then surely you mugged up your head on Amazon’s long and tedious documentation about the usage of its service called AWS before, which you surely got on the first search results from Google. But feel good, you are definitely going to get the thing which you are looking, from this article. So,  before starting the way to upload files on Amazon bucket with python, we will talk a little about “What is Amazon bucket?” so that noob visitors also get covered. You can skip this part if you already know what actually is Amazon web services(AWS)’s bucket and where it can be used.

What is Bucket?

S3 stands for Simple Storage Service, and yes as the name suggests it’s simply a cloud storage service provided by Amazon, where you can upload or download files directly using the s3 website itself or dynamically via your program written in Python, PHP, etc. Many companies use it as a database for utilities like storing users information, for example, photos, which their user upload on their website ar app. Amazon S3 bucket is easy to use, secure, scalable(resizeable as per your need) and can be accessed from anywhere. It is also very affordable. It supports almost all file type and has 99.9% uptime. Due to these many qualities, Amazon S3 bucket got the attraction of many big companies also and is famous in the services of these kinds.

How to upload files in the Amazon s3 bucket through Python?

In the following paragraphs, I will show you how to configure and finally upload/download files in/from Amazon S3 bucket through your Python application, step by step.

Configure the environment

Before uploading the file, you need to make your application connect to your amazon s3 bucket, that you have created after making an AWS account. For doing that you need your S3 access key and S3 secret Key. So, keep it noted.

Install the awscli package in your working python environment:

  • pip install awscli

->There may lot of errors came after running this commañd, like not able to uninstall PyYAML. Just google tḫem you will get the solution.

-> Still if you got unlucky and not able install awscli via pip, then download awscli’s window installer from here. Sorry for Mac and Linux users, you can only install via command lines.

After successful installation of awscli run:

  • aws configure

Then it will ask you to fill access key, secret key, region, and output file type like this:

AWS S3 Configuration Image

-> For the access key and secret key, put what you got through your AWS console account.
-> For region write us-east-2 if your s3 bucket server location city is in the east side of US and similarly for other cities. You can skip location input by just hitting Enter. It’s optional. (Many got an error by not putting the location in the correct format).

->For Default output format, just leave it by pressing enter.

Verify the configuration by running the following command, if it returns the list of buckets, Then you done with configuration part.

Upload files in AWS s3 bucket

For uploading the files in s3, you need to use a package called boto3, so install it by running following command:

  • pip install boto3

After installing use the following code to upload files into s3:

  • import boto3
    
    BucketName = "Your AWS S3 Bucket Name"
    LocalFileName = "Name with the path of the file you want to upload"
    S3FileName = "The name of the file you want to give after the successful upload in the s3 bucket"
    
    s3 = boto3.client('s3')
    s3.upload_file(InputFileName,LocalFileName,S3FileName)
    
    

Worth Knowing: How to make any laptop touchscreen?

Download files from AWS s3 bucket

For Downloading a file from S3 bucket to your local system, you can use this code:

*botocore comes along with boto3 install

  • import boto3
    import botocore
    
    BucketName = "Your AWS S3 Bucket Name"
    S3FileName = "The name of the file in S3 bucket that you wish to download"
    LocalFileName = "The name with which, you want to save it after downloading from s3 bucket)"
    
    s3 = boto3.resource('s3')
    try:
        s3.Bucket(BucketName).download_file(S3FileName, LocalFileName)
    except botocore.exceptions.ClientError as e:
        if e.response['Error']['Code'] == "404":
            print("The requested file does not found.")
        else:
            raise

 

So, That’s it guys, understanding this article makes you comfortable with uploading and downloading data from amazon s3 bucket.  Play around with the given codes into your machine.

Lastly, we hope you like this post, don’t forget to share it with your friends who you think is needful. And do comment below if you have any query.

Know about: iOS 13, The New OS by Apple

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here