🚀 DevOps & SRE Certification Program 📅 Starting: 1st of Every Month 🤝 +91 8409492687 🔍 Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

AWS Tutorials: Python code to sync S3 Buckets


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)
view raw s3.py hosted with ❤ by GitHub
Subscribe
Notify of
guest


0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x