Tuesday, 4 May 2021

S3 file upload using boto3 in python


import boto3

import requests


access_key='key'

secret_access='key'

region = 'region'

s3 = boto3.client('s3', region_name=region, aws_access_key_id=access_key,

                                   aws_secret_access_key=secret_access)

 

s3.upload_file('source','bucket','destination/filename')

print("uploaded")


S3 file download using boto3


import boto3

import requests


access_key='key'

secret_access='key'

region = 'your-region'

s3 = boto3.client('s3', region_name=region, aws_access_key_id=access_key,

                                   aws_secret_access_key=secret_access)

 

s3.download_file('bucket','folder/file','download-destination')

print("downloaded")


S3 list all files in bucket


import boto3


access_key='your-key'

secret_access='your-key'

region = 'ap-south-1'

s3 = boto3.client('s3', region_name=region, aws_access_key_id=access_key,

                                   aws_secret_access_key=secret_access)

 

for key in s3.list_objects(Bucket='bucketname',Prefix='folder/')['Contents']:

    print(key['Key'])