Search This Blog

Tuesday, April 14, 2020

Python AWS S3 - Upload a File

import os
import boto3
from botocore.exceptions import NoCredentialsError

ACCESS_KEY = 'AK....'
AWS_ACCESS_SECRET_KEY = 'DZKtksHidEexxxxxxx'

def upload_to_aws(local_file, bucket, s3_file):
    s3 = boto3.client('s3', aws_access_key_id=ACCESS_KEY,
                      aws_secret_access_key=AWS_ACCESS_SECRET_KEY)

    try:
        s3.upload_file(local_file, bucket, s3_file)
        print("Upload Successful")
        return True
    except FileNotFoundError:
        print("The file was not found")
        return False
    except NoCredentialsError:
        print("Credentials not available")
        return False



file = open('test.png', 'r+')

key = file.name
bucket = 'dev-nw'

uploaded = upload_to_aws('test.png', bucket, key)
    


#download a file
s3 = boto3.client( 's3', aws_access_key_id=ACCESS_KEY, aws_secret_access_key=AWS_ACCESS_SECRET_KEY );   
s3.download_file( bucket, key, 'D:/airtel-download.png' );



No comments:

Hit Counter


View My Stats