Skip to content

Commit

Permalink
Move the method for retrieving the S3 URI for a specific authorized u…
Browse files Browse the repository at this point in the history
…ser to the AWSAccessPoint model.
  • Loading branch information
Chrystinne committed Jan 22, 2025
1 parent c7ee305 commit 93b7cc5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
26 changes: 13 additions & 13 deletions physionet-django/project/modelcomponents/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,12 @@ class AWS(models.Model):
class Meta:
default_permissions = ()

def get_public_s3_uri(self):
def public_s3_uri(self):
"""
Construct the S3 URI for public projects.
"""
return f's3://{self.bucket_name}/{self.project.slug}/{self.project.version}/'

def get_private_s3_uri(self, access_point_name):
"""
Construct the S3 URI for private projects using an access point.
"""
if not access_point_name:
return None

return (
f's3://arn:aws:s3:us-east-1:{settings.AWS_ACCOUNT_ID}:accesspoint/'
f'{access_point_name}/{self.project.slug}/{self.project.version}/'
)

def __str__(self):
return f"AWS instance for project: {self.project.slug}"

Expand All @@ -93,6 +81,18 @@ class AWSAccessPoint(models.Model):
related_name='linked_access_points'
)

class Meta:
default_permissions = ()

def private_s3_uri(self):
"""
Construct the S3 URI for private projects using an access point.
"""
return (
f's3://arn:aws:s3:us-east-1:{settings.AWS_ACCOUNT_ID}:accesspoint/'
f'{self.name}/{self.aws.project.slug}/{self.aws.project.version}/'
)


class AWSAccessPointUser(models.Model):
access_point = models.ForeignKey(
Expand Down
10 changes: 4 additions & 6 deletions physionet-django/project/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1936,13 +1936,11 @@ def published_project(request, project_slug, version, subdir=''):
try:
if project.aws.is_private:
if has_signed_dua and request.user.is_authenticated:
access_point_name = get_access_point_name_for_user_and_project(
request.user,
project.aws
)
s3_uri = project.aws.get_private_s3_uri(access_point_name)
access_point = project.aws.access_points.filter(linked_users__user=request.user).first()
if access_point:
s3_uri = access_point.private_s3_uri()
else:
s3_uri = '--no-sign-request ' + project.aws.get_public_s3_uri()
s3_uri = '--no-sign-request ' + project.aws.public_s3_uri()
except AWS.DoesNotExist:
s3_uri = None

Expand Down

0 comments on commit 93b7cc5

Please sign in to comment.