import boto3
import json
import os
import threading
import sys
import config
from boto3.s3.transfer import TransferConfig
bucket_name = config.bucket_name
region = config.region_code
path_to_file = config.path_to_file
small_file = config.small_file
WEBSITE_bucket_name = 'YOUR_OWN_DOMAIN_NAME_OR_BUCKET_NAME'
def s3_client():
s3 = boto3.client('s3')
return s3
def s3_resource():
s3 = boto3.resource('s3')
return s3
def create_bucket(bucket_name):
return s3_client().create_bucket(
Bucket=bucket_name,
CreateBucketConfiguration={
'LocationConstraint': region
}
)
def create_bucket_policy():
bucket_policy = {
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AddPerm",
"Effect": "Allow",
"Principal": "*",
"Action": ["s3:*"],
'Resource': 'arn:aws:s3:::' + bucket_name + '/*'
}
]
}
policy_string = json.dumps(bucket_policy)
return s3_client().put_bucket_policy(
Bucket=bucket_name,
Policy=policy_string
)
def list_buckets():
return s3_client().list_buckets()
def get_bucket_policy():
return s3_client().get_bucket_policy(Bucket=bucket_name)
def get_bucket_encryption():
return s3_client().get_bucket_encryption(Bucket=bucket_name)
def update_bucket_policy(bucket_name):
bucket_policy = {
'Version': '2012-10-17',
'Statement': [
{
'Sid': 'AddPerm',
'Effect': 'Allow',
'Principal': '*',
'Action': [
's3:DeleteObject',
's3:GetObject',
's3:PutObject'
],
'Resource': 'arn:aws:s3:::' + bucket_name + '/*'
}
]
}
policy_string = json.dumps(bucket_policy)
return s3_client().put_bucket_policy(
Bucket=bucket_name,
Policy=policy_string
)
def server_side_encrypt_bucket():
return s3_client().put_bucket_encryption(
Bucket=bucket_name,
ServerSideEncryptionConfiguration={
'Rules': [
{
'ApplyServerSideEncryptionByDefault': {
'SSEAlgorithm': 'AES256'
}
}
]
}
)
def delete_bucket():
return s3_client().delete_bucket(Bucket=bucket_name)
def upload_small_file():
file_path = path_to_file+small_file
return s3_client().upload_file(file_path, bucket_name, 'readme.txt')
def upload_large_file():
config = TransferConfig(multipart_threshold=1024 * 25, max_concurrency=10,
multipart_chunksize=1024 * 25, use_threads=True)
file_path = os.path.dirname(__file__) + '/largefile.pdf'
key_path = 'multipart_files/largefile.pdf'
s3_resource().meta.client.upload_file(file_path, bucket_name, key_path,
ExtraArgs={
'ACL': 'public-read', 'ContentType': 'text/pdf'},
Config=config,
Callback=ProgressPercentage(file_path))
class ProgressPercentage(object):
def __init__(self, filename):
self._filename = filename
self._size = float(os.path.getsize(filename))
self._seen_so_far = 0
self._lock = threading.Lock()
def __call__(self, bytes_amount):
with self._lock:
self._seen_so_far += bytes_amount
percentage = (self._seen_so_far / self._size) * 100
sys.stdout.write(
"\r%s %s / %s (%.2f%%)" % (
self._filename, self._seen_so_far, self._size, percentage
)
)
sys.stdout.flush()
def read_object_from_bucket():
object_key = 'readme.txt'
return s3_client().get_object(Bucket=bucket_name, Key=object_key)
def version_bucket_files():
s3_client().put_bucket_versioning(
Bucket=bucket_name,
VersioningConfiguration={
'Status': 'Enabled'
}
)
def upload_a_new_version():
file_path = path_to_file+small_file
return s3_client().upload_file(file_path, bucket_name, small_file)
def put_lifecycle_policy():
lifecycle_policy = {
"Rules": [
{
"ID": "Move readme file to Glacier",
"Prefix": "readme",
"Status": "Enabled",
"Transitions": [
{
"Date": "2019-01-01T00:00:00.000Z",
"StorageClass": "GLACIER"
}
]
},
{
"Status": "Enabled",
"Prefix": "",
"NoncurrentVersionTransitions": [
{
"NoncurrentDays": 2,
"StorageClass": "GLACIER"
}
],
"ID": "Move old versions to Glacier"
}
]
}
s3_client().put_bucket_lifecycle_configuration(
Bucket=bucket_name,
LifecycleConfiguration=lifecycle_policy
)
if __name__ == '__main__':
print(create_bucket(bucket_name))
# print(create_bucket_policy())
# print(list_buckets())
# print(get_bucket_policy())
# print(update_bucket_policy(bucket_name))
# print(get_bucket_encryption())
# print(server_side_encrypt_bucket())
# print(delete_bucket())
# print(upload_small_file())
# upload_large_file()
# print(read_object_from_bucket())
# version_bucket_files()
# upload_a_new_version()
# put_lifecycle_policy()
# host_static_website()
# route_53_record_for_s3_website()