Skip to content

Commit 14c3573

Browse files
committed
Actually use AWS IAM when connecting to S3 for uploading and downloading files.
Had only updated the dependencies that deal with pre-sigend URLs in S3.
1 parent 83ff545 commit 14c3573

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

backend/app/dependencies.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import logging
2-
from typing import Generator, AsyncGenerator
2+
from typing import AsyncGenerator
33

44
import boto3
55
import pika
@@ -14,13 +14,28 @@
1414
logger.setLevel(logging.DEBUG)
1515

1616

17-
async def get_fs() -> Generator:
18-
file_system = Minio(
19-
settings.MINIO_SERVER_URL,
20-
access_key=settings.MINIO_ACCESS_KEY,
21-
secret_key=settings.MINIO_SECRET_KEY,
22-
secure=False,
23-
)
17+
async def get_fs() -> AsyncGenerator[Minio, None]:
18+
# Either use AWS Identity and Access Management (IAM) to connect to S3 or connect to Minio server
19+
if settings.AWS_IAM:
20+
logger.debug("AWS IAM enabled for s3 authentication")
21+
session = boto3.Session()
22+
credentials = session.get_credentials()
23+
credentials = credentials.get_frozen_credentials()
24+
file_system = Minio(
25+
settings.MINIO_EXTERNAL_SERVER_URL,
26+
access_key=credentials.access_key,
27+
secret_key=credentials.secret_key,
28+
session_token=credentials.token,
29+
secure=settings.MINIO_SECURE.lower() == "true",
30+
)
31+
else:
32+
logger.debug("Local MinIO authentication")
33+
file_system = Minio(
34+
settings.MINIO_EXTERNAL_SERVER_URL,
35+
access_key=settings.MINIO_ACCESS_KEY,
36+
secret_key=settings.MINIO_SECRET_KEY,
37+
secure=settings.MINIO_SECURE.lower() == "true",
38+
)
2439
clowder_bucket = settings.MINIO_BUCKET_NAME
2540
if not file_system.bucket_exists(clowder_bucket):
2641
file_system.make_bucket(clowder_bucket)
@@ -30,7 +45,7 @@ async def get_fs() -> Generator:
3045

3146
# This will be needed for generating presigned URL for sharing
3247
async def get_external_fs() -> AsyncGenerator[Minio, None]:
33-
# Either use AWS Identity and Access Management (IAM) to connect to S3 Ïor connect to Minio server
48+
# Either use AWS Identity and Access Management (IAM) to connect to S3 or connect to Minio server
3449
if settings.AWS_IAM:
3550
logger.debug("AWS IAM enabled for s3 authentication")
3651
session = boto3.Session()

0 commit comments

Comments
 (0)