Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit 984a633

Browse files
AWS s3 uploader
1 parent 74eb163 commit 984a633

3 files changed

Lines changed: 41 additions & 0 deletions

File tree

projects/AWS_s3_upload/main.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import boto3
2+
from botocore.exceptions import NoCredentialsError
3+
4+
ACCESS_KEY = ''
5+
SECRET_KEY = ''
6+
LOCAL_FILE = 'local_file_name'
7+
BUCKET_NAME = 'bucket_name'
8+
S3_FILE_NAME = 'file_name_on_s3'
9+
10+
def upload_to_s3(local_file, bucket, s3_file):
11+
## This function is responsible for uploading the file into the S3 bucket using the specified credentials.
12+
s3 = boto3.client('s3', aws_access_key_id=ACCESS_KEY,
13+
aws_secret_access_key=SECRET_KEY)
14+
try:
15+
s3.upload_file(local_file, bucket, s3_file)
16+
print("Upload Successful")
17+
return True
18+
except FileNotFoundError:
19+
print("The file was not found")
20+
return False
21+
except NoCredentialsError:
22+
print("Credentials not available")
23+
return False
24+
25+
26+
result = upload_to_s3(LOCAL_FILE, BUCKET_NAME, S3_FILE_NAME)

projects/AWS_s3_upload/readme.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
## Simple Python script for AWS S3 file upload.
3+
4+
### Prerequisites
5+
boto3 (pip install boto3) <br />
6+
7+
### How to run the script
8+
- Specify both ACCESS_KEY and SECRET_KEY. You can get them both on your AWS account in "My Security Credentials" section. <br />
9+
- Specify the local file name, bucket name and the name that you want the file to have inside s3 bucket using LOCAL_FILE, BUCKET_NAME and S3_FILE_NAME variables. <br />
10+
- Run "python main.py" <br />
11+
12+
### Author Name
13+
Yashvardhan Singh https://github.com/pythonicboat
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
boto3==1.20.4
2+
botocore==1.23.4

0 commit comments

Comments
 (0)