Last active
November 23, 2021 05:42
-
-
Save matheusdigital/581d0083a8ad0fe144f6952237e97ca1 to your computer and use it in GitHub Desktop.
Uploading entire local folder or directory to Digital Ocean Spaces in Python with Boto3 and OS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from boto3 import session | |
from boto3.s3.transfer import S3Transfer | |
# Use the API Keys you generated at Digital Ocean | |
ACCESS_ID = 'YOUR_DO_SPACE_ACCESS_KEY' | |
SECRET_KEY = 'YOUR_DO_SPACE_SECRET_KEY' | |
# Initiate session | |
session = session.Session() | |
client = session.client('s3', # It uses the same AWS S3 upload module | |
region_name='nyc3', # enter your own region_name (from your Digital Ocean space) | |
endpoint_url='https://nyc3.digitaloceanspaces.com', # enter your own endpoint url | |
aws_access_key_id=ACCESS_ID, | |
aws_secret_access_key=SECRET_KEY) | |
transfer = S3Transfer(client) | |
local_directory = "C:\your-folder-path" # Local directory path you want to upload all files inside | |
space_name = "your_space_name" # Name of your spaces name on Digital Ocean (also called bucket on AWS S3) | |
destination = "path/to/folder/" # Folder where you want to upload the files in your DO Space. Leave it blank "" if you want upload files to root space folder | |
# enumerate local files recursively | |
for root, dirs, files in os.walk(local_directory): | |
for filename in files: | |
# for every file found: | |
local_path = os.path.join(root, filename) # construct the full local path | |
destination_path = os.path.join(destination, filename) # construct the full Digital Ocean Space path | |
do_space_path = destination_path.replace(os.sep,'/') # I used it to convert Windows file path to UNIX, changing '\' to '/'. It's not necessary if you aren't using windows | |
try: | |
print("Uploading file {} to path {}...".format(local_path, do_space_path)) | |
transfer.upload_file(local_path, space_name, do_space_path) # Upload the file | |
except Exception as e: | |
print(e) # It will print exception if some error occurs | |
else: | |
print("SUCCESS on upload file {} to path {}".format(local_path, do_space_path)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment