s3-public-download.py 1.33 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/python3

import os
import logging
import boto3


radosgw_access_key = os.environ['AWS_ACCESS_KEY_ID']
radosgw_secret_key = os.environ['AWS_SECRET_ACCESS_KEY']
radosgw_endpoint_url = 'https://rise-s3.zib.de'

radosgw_bucket_name = 'k8s-doc-bucket'
radosgw_key_name = 'Using_Kubernetes_Secrets.pdf'
radosgw_file_name = 'talk.pdf'


# generate s3 client
s3_client = boto3.client('s3',
                    endpoint_url=radosgw_endpoint_url,
                    aws_access_key_id=radosgw_access_key,
                    aws_secret_access_key=radosgw_secret_key
)

################################################################################
# check if bucket exists
bucket_exists = False

# Retrieve the list of existing buckets
response = s3_client.list_buckets()

for bucket in response['Buckets']:
    bucket_name = bucket["Name"]
    if (bucket_name == radosgw_bucket_name):
        bucket_exists = True

# create bucket if not found
if bucket_exists == False:
    print(f"creating bucket '{radosgw_bucket_name}' …")
    response = s3_client.create_bucket(Bucket=radosgw_bucket_name, ACL='public-read')

# upload file
response = s3_client.upload_file(
    radosgw_file_name, radosgw_bucket_name, radosgw_key_name,
    ExtraArgs={'ACL': 'public-read'}
)

print(f"uploaded file to '{radosgw_endpoint_url}/{radosgw_bucket_name}/{radosgw_key_name}'.")