import boto3 | |
import os | |
def sync_s3_bucket_to_local(bucket_name, local_directory): | |
# Initialize S3 client | |
s3_client = boto3.client('s3') | |
# Create local directory if it does not exist | |
if not os.path.exists(local_directory): | |
os.makedirs(local_directory) | |
# List all objects in the S3 bucket | |
bucket_objects = s3_client.list_objects_v2(Bucket=bucket_name) | |
if 'Contents' in bucket_objects: | |
for obj in bucket_objects['Contents']: | |
key = obj['Key'] | |
local_file_path = os.path.join(local_directory, key) | |
# Check if directory, if so, create directory | |
if '/' in key: | |
directory = os.path.dirname(local_file_path) | |
if not os.path.exists(directory): | |
os.makedirs(directory) | |
if key.endswith('/'): | |
# It's a directory, no need to download | |
continue | |
# Download file from S3 to local directory | |
print(f"Downloading {key} to {local_file_path}") | |
s3_client.download_file(bucket_name, key, local_file_path) | |
else: | |
print("No objects found in the bucket.") | |
# Sync the specified S3 bucket to the local directory | |
bucket_name = 'devops4342343244344342' | |
local_directory = 'raj' | |
sync_s3_bucket_to_local(bucket_name, local_directory) |
I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND