-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_aws_objects.py
More file actions
75 lines (55 loc) · 2.49 KB
/
Copy pathget_aws_objects.py
File metadata and controls
75 lines (55 loc) · 2.49 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import boto3.resources.base
import botocore.client
from boto3 import Session
from get_datafile_credentials import get_datafile_credentials
def get_aws_session(
username: str, password: str, environment: str = "stage"
) -> boto3.Session | None:
"""Returns a Boto3 Session object configured with AWS credentials for accessing the DataCite Monthly Data File.
:param username: Your DataCite username
:param password: Your DataCite password
:param environment: The target environment, either "stage" or None (for production)
:rtype: boto3.Session (if successful) | None (if an error)
"""
aws_credentials = get_datafile_credentials(username, password, environment)
if not aws_credentials:
return None
session = Session(
aws_access_key_id=aws_credentials[0],
aws_secret_access_key=aws_credentials[1],
aws_session_token=aws_credentials[2],
region_name="eu-west-1",
)
return session if session else None
def get_client(
username: str, password: str, environment: str = "stage"
) -> botocore.client.BaseClient | None:
"""Returns a Boto3 client object for S3, configured with AWS credentials for accessing the DataCite Monthly Data File.
:param username: Your DataCite username
:param password: Your DataCite password
:param environment: The target environment, either "stage" or None (for production)
:rtype: client.S3 (if successful) | None (if an error)
"""
session = get_aws_session(username, password, environment)
if not session:
return None
aws_client = session.client("s3")
return aws_client if aws_client else None
def get_bucket(
username: str, password: str, environment: str = "stage"
) -> boto3.resources.base.ServiceResource | None:
"""Returns a Boto3 S3 Bucket resource, configured with AWS credentials for accessing the DataCite Monthly Data File and the appropriate S3 bucket.
:param username: Your DataCite username
:param password: Your DataCite password
:param environment: The target environment, either "stage" or None (for production)
:rtype: s3.Bucket (if successful) | None (if an error)
"""
session = get_aws_session(username, password, environment)
if not session:
return None
bucket = (
f"monthly-datafile{'.' + environment + '.' if environment else '.'}datacite.org"
)
s3_resource = session.resource("s3")
bucket_resource = s3_resource.Bucket(bucket)
return bucket_resource if bucket_resource else None