mirror of
https://github.com/hastagAB/Awesome-Python-Scripts.git
synced 2024-11-27 14:01:09 +00:00
Merge pull request #101 from jramnai/python-files
[ADD] Upload files to s3 using boto3 and python
This commit is contained in:
commit
6d3236eb3e
|
@ -56,6 +56,7 @@ So far, the following projects have been integrated to this repo:
|
|||
|[Squid installer for Ubuntu](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Squid-Proxy-Installer-for-Ubuntu16)|[Berkay Demir]()|
|
||||
|[Subtitle downloader](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Subtitle-downloader)|[Kaushlendra Pratap](https://github.com/kaushl1998)|
|
||||
|[Take Screenshot](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Take_screenshot)|[Moad Mohammed Elhebri](https://github.com/moadmmh)|
|
||||
|[Upload Files to S3](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Upload_files_to_s3)|[Jayram Nai](https://github.com/jramnai)|
|
||||
|[Vinegère Cipher](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/vigenere_cipher)|[victoni](https://github.com/victoni)|
|
||||
|[Web proxy](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Proxy-Request)|[Nikhil Kumar Singh](https://github.com/nikhilkumarsingh)|
|
||||
|[Website blocker](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Website-Blocker)|[Ayush Bhardwaj](https://github.com/hastagAB)|
|
||||
|
|
17
Upload_files_to_s3/README.md
Normal file
17
Upload_files_to_s3/README.md
Normal file
|
@ -0,0 +1,17 @@
|
|||
# Upload files & folders from your machine to Amazon S3
|
||||
|
||||
A python script that will upload your files & folder to Amzzon S3 using python and boto3
|
||||
|
||||
## Requirement
|
||||
|
||||
Python 2.xx
|
||||
boto3
|
||||
```bash
|
||||
pip install boto3
|
||||
```
|
||||
|
||||
#Usage
|
||||
Go to Upload_files_to_s3 directory and add your folder's name you want to upload to s3 and then run upload_files_to_s3.py as below:
|
||||
```bash
|
||||
$ python upload_files_to_s3.py
|
||||
```
|
1
Upload_files_to_s3/requirements.txt
Normal file
1
Upload_files_to_s3/requirements.txt
Normal file
|
@ -0,0 +1 @@
|
|||
boto3==1.9.197 # Amazon Web Services SDK for Python
|
33
Upload_files_to_s3/upload_files_to_s3.py
Normal file
33
Upload_files_to_s3/upload_files_to_s3.py
Normal file
|
@ -0,0 +1,33 @@
|
|||
import boto3
|
||||
import os
|
||||
|
||||
ACL = 'public-read' #access type of the file
|
||||
AWS_ACCESS_KEY_ID = 'your_access_key'
|
||||
AWS_REGION = 'your_region'
|
||||
AWS_SECRET_ACCESS_KEY = 'your_secret_key'
|
||||
AWS_STORAGE_BUCKET_NAME = 'my_bucket'
|
||||
FOLDER_NAME_ON_S3 = 'my_folder_on_s3'
|
||||
FOLDER_PATH = '/home/foo/my_folder'
|
||||
|
||||
|
||||
def upload_files_to_s3(path):
|
||||
"""
|
||||
Upload files to AWS s3 bucket from your machine
|
||||
using python and boto3
|
||||
"""
|
||||
session = boto3.Session(
|
||||
aws_access_key_id=AWS_ACCESS_KEY_ID,
|
||||
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
|
||||
region_name=AWS_REGION
|
||||
)
|
||||
s3 = session.resource('s3')
|
||||
bucket = s3.Bucket(AWS_STORAGE_BUCKET_NAME)
|
||||
for subdir, dirs, files in os.walk(path):
|
||||
for file in files:
|
||||
full_path = os.path.join(subdir, file)
|
||||
with open(full_path, 'rb') as data:
|
||||
key = FOLDER_NAME_ON_S3 + full_path[len(path) + 1:]
|
||||
bucket.put_object(Key=key, Body=data, ACL=ACL)
|
||||
|
||||
if __name__ == "__main__":
|
||||
upload_files_to_s3(FOLDER_PATH)
|
Loading…
Reference in New Issue
Block a user