4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Upload-Download File From S3 with Boto3 Python

Last updated at Posted at 2018-06-07

#INTRODUCTION
Today we will talk about how to download , upload file to Amazon S3 with Boto3 Python.

#GETTING STARTED

Before we start , Make sure you notice down your S3 access key and S3 secret Key.

###1. AWS Configure
Before we could work with AWS S3. We need to configure it first.

  • Install awscli using pip
pip install awscli
  • Configure
aws configure
AWS Access Key ID [None]: input your access key
AWS Secret Access Key [None]: input your secret access key
Default region name [None]: input your region
Default output format [None]: json
  • Verifies
aws s3 ls

if you see there is your bucket show up. it mean your configure is correct. Ok, Now let's start with upload file.

###2. Upload File

import boto3

bucketName = "Your S3 BucketName"
Key = "Original Name and type of the file you want to upload into s3"
outPutname = "Output file name(The name you want to give to the file after we upload to s3)"

s3 = boto3.client('s3')
s3.upload_file(Key,bucketName,outPutname)

###3. Download File

import boto3
import botocore

Bucket = "Your S3 BucketName"
Key = "Name of the file in S3 that you want to download"
outPutName = "Output file name(The name you want to save after we download from s3)"

s3 = boto3.resource('s3')
try:
    s3.Bucket(Bucket).download_file(Key, outPutName)
except botocore.exceptions.ClientError as e:
    if e.response['Error']['Code'] == "404":
        print("The object does not exist.")
    else:
        raise
4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?