-
Notifications
You must be signed in to change notification settings - Fork 0
/
my-s3-python.py
53 lines (41 loc) · 1.45 KB
/
my-s3-python.py
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
from flask import Flask, Response, jsonify
import boto3
from botocore.exceptions import (
NoCredentialsError,
PartialCredentialsError,
TokenRetrievalError,
)
app = Flask(__name__)
def create_boto3_session(profile_name):
try:
session = boto3.Session(profile_name=profile_name)
return session
except TokenRetrievalError as e:
print(f"Token retrieval error: {e}")
print(
f"Please run `aws sso login --profile {profile_name}` to renew your SSO token."
)
return None
except (NoCredentialsError, PartialCredentialsError) as e:
print(f"Error: {e}")
return None
def get_file_from_s3(bucket_name, file_name, profile_name):
session = create_boto3_session(profile_name)
if not session:
return "AWS SSO token has expired or there is an issue with credentials."
s3_client = session.client("s3", region_name="us-east-1")
try:
obj = s3_client.get_object(Bucket=bucket_name, Key=file_name)
return obj["Body"].read().decode("utf-8")
except s3_client.exceptions.NoSuchKey:
return "The specified key does not exist."
except Exception as e:
return f"Error retrieving file: {e}"
profile_name = "aws_admin_user1"
bucket_name = "my-object-storage-bucket"
file_name = "my-s3-file.txt"
@app.route("/hello")
def hello():
return get_file_from_s3(bucket_name, file_name, profile_name)
if __name__ == "__main__":
app.run(debug=True)