How Python Enhances DevOps Efficiency and Automation
Amitt Ashok
1 min read
# Create in function format
import os
import boto3
# Set clinet globally
s3 = boto3.client('s3')
def list_bucket():
response = s3.list_buckets()
# Fetch the buckets form account
bucket_list = (response["Buckets"])
# Iterate over bucket list and add in variable i from bucket_list
for i in bucket_list:
# Only print name of bucket
print(i['Name'])
def file_upload():
# File which want to upload
file_name = "/home/amitt-ashok/break.sh"
# Only take base name of file rather than full path
full_file_name = os.path.basename(file_name)
# bucket-name in which file want to upload
bucket_name = "python-devops-amitt-bucket"
# new name for file in bucket
new_bucket_name = "break1.txt"
#upload file
s3.upload_file(file_name,bucket_name, new_bucket_name)
print(f"{file_name} is succssfully uploaded in {bucket_name}")
def file_download():
# File which want to download from account
file_name = "list_bucket_with_python"
# Bucket name
bucket_name = 'python-devops-amitt-bucket'
# if want to change name locally
new_file_name = 'app-log'
s3.download_file(bucket_name, file_name, new_file_name)
print("file downloaded successfully...")
def invalid_option():
print("Invalid option selected")
def switch_case(user_input):
options = {
1: list_bucket,
2: file_upload,
3: file_download
}
#return options.get(int(user_input),invalid_option())
action= options.get(user_input, invalid_option)
action()
user_input =int(input("Choose an option (1: List buckets, 2: Upload file, 3: Download file): "))
switch_case(user_input)
0
Subscribe to my newsletter
Read articles from Amitt Ashok directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Amitt Ashok
Amitt Ashok
Hey Hi there, Amit here... I love to explore new things and learn new technology.