Skip to content

Commit

Permalink
fix s3 client error on gcp (#2359)
Browse files Browse the repository at this point in the history
  • Loading branch information
sainak authored Aug 14, 2024
1 parent 2221b06 commit e95d98d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
15 changes: 9 additions & 6 deletions care/facility/api/serializers/facility.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import boto3
from django.conf import settings
from django.contrib.auth import get_user_model
from rest_framework import serializers

Expand Down Expand Up @@ -168,12 +169,14 @@ def save(self, **kwargs):
config, bucket_name = get_client_config(BucketType.FACILITY)
s3 = boto3.client("s3", **config)
image_location = f"cover_images/{facility.external_id}_cover.{image_extension}"
s3.put_object(
Bucket=bucket_name,
Key=image_location,
Body=image.file,
ACL="public-read",
)
boto_params = {
"Bucket": bucket_name,
"Key": image_location,
"Body": image.file,
}
if settings.BUCKET_HAS_FINE_ACL:
boto_params["ACL"] = "public-read"
s3.put_object(**boto_params)
facility.cover_image_url = image_location
facility.save()
return facility
18 changes: 12 additions & 6 deletions care/utils/csp/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class ClientConfig(TypedDict):
class CSProvider(enum.Enum):
AWS = "AWS"
GCP = "GCP"
DIGITAL_OCEAN = "DIGITAL_OCEAN"
MINIO = "MINIO"
DOCKER = "DOCKER" # localstack in docker
LOCAL = "LOCAL" # localstack on host

Expand All @@ -31,9 +33,11 @@ def get_facility_bucket_config(external) -> tuple[ClientConfig, BucketName]:
"region_name": settings.FACILITY_S3_REGION,
"aws_access_key_id": settings.FACILITY_S3_KEY,
"aws_secret_access_key": settings.FACILITY_S3_SECRET,
"endpoint_url": settings.FACILITY_S3_BUCKET_EXTERNAL_ENDPOINT
if external
else settings.FACILITY_S3_BUCKET_ENDPOINT,
"endpoint_url": (
settings.FACILITY_S3_BUCKET_EXTERNAL_ENDPOINT
if external
else settings.FACILITY_S3_BUCKET_ENDPOINT
),
}, settings.FACILITY_S3_BUCKET


Expand All @@ -42,9 +46,11 @@ def get_patient_bucket_config(external) -> tuple[ClientConfig, BucketName]:
"region_name": settings.FILE_UPLOAD_REGION,
"aws_access_key_id": settings.FILE_UPLOAD_KEY,
"aws_secret_access_key": settings.FILE_UPLOAD_SECRET,
"endpoint_url": settings.FILE_UPLOAD_BUCKET_EXTERNAL_ENDPOINT
if external
else settings.FILE_UPLOAD_BUCKET_ENDPOINT,
"endpoint_url": (
settings.FILE_UPLOAD_BUCKET_EXTERNAL_ENDPOINT
if external
else settings.FILE_UPLOAD_BUCKET_ENDPOINT
),
}, settings.FILE_UPLOAD_BUCKET


Expand Down
1 change: 1 addition & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,7 @@
BUCKET_SECRET = env("BUCKET_SECRET", default="")
BUCKET_ENDPOINT = env("BUCKET_ENDPOINT", default="")
BUCKET_EXTERNAL_ENDPOINT = env("BUCKET_EXTERNAL_ENDPOINT", default=BUCKET_ENDPOINT)
BUCKET_HAS_FINE_ACL = env.bool("BUCKET_HAS_FINE_ACL", default=False)

if BUCKET_PROVIDER not in csp_config.CSProvider.__members__:
print(f"Warning Invalid CSP Found! {BUCKET_PROVIDER}")
Expand Down

0 comments on commit e95d98d

Please sign in to comment.