Skip to content

Commit 59c5c59

Browse files
committed
🚚(back) serve legacy thumbnails from Scaleway S3
Thumbnails are now served from the aws/ directory in Scaleway S3. They are served using the django-storage already in place. As the newer thumbnails are already stored in Scaleway, we do not rename these files and allow them to be served without requiring signed URLs.
1 parent e697478 commit 59c5c59

File tree

5 files changed

+58
-157
lines changed

5 files changed

+58
-157
lines changed

src/backend/marsha/core/models/video.py

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
from marsha.core.defaults import (
2222
APPROVAL,
23+
AWS_STORAGE_BASE_DIRECTORY,
2324
CELERY_PIPELINE,
2425
DELETED,
2526
DELETED_STORAGE_BASE_DIRECTORY,
@@ -793,30 +794,6 @@ class Meta:
793794
)
794795
]
795796

796-
def get_source_s3_key(self, stamp=None):
797-
"""Compute the S3 key in the source bucket.
798-
799-
It is built from the video ID + ID of the thumbnail + version stamp.
800-
801-
Parameters
802-
----------
803-
stamp: Type[string]
804-
Passing a value for this argument will return the source S3 key for the thumbnail
805-
assuming its active stamp is set to this value. This is useful to create an
806-
upload policy for this prospective version of the thumbnail, so that the client can
807-
upload the file to S3 and the confirmation lambda can set the `uploaded_on` field
808-
to this value only after the file upload and processing is successful.
809-
810-
Returns
811-
-------
812-
string
813-
The S3 key for the thumbnail file in the source bucket, where uploaded files are
814-
stored before they are converted and copied to the destination bucket.
815-
816-
"""
817-
stamp = stamp or self.uploaded_on_stamp()
818-
return f"{self.video.pk}/thumbnail/{self.pk}/{stamp}"
819-
820797
def get_storage_prefix(
821798
self,
822799
stamp=None,
@@ -844,6 +821,10 @@ def get_storage_prefix(
844821
"""
845822
stamp = stamp or self.uploaded_on_stamp()
846823
base = base_dir
824+
825+
if base_dir == AWS_STORAGE_BASE_DIRECTORY:
826+
return f"{base}/{self.video.pk}/thumbnails"
827+
847828
if base_dir == DELETED_STORAGE_BASE_DIRECTORY:
848829
base = f"{DELETED_STORAGE_BASE_DIRECTORY}/{VOD_STORAGE_BASE_DIRECTORY}"
849830

src/backend/marsha/core/serializers/thumbnail.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from rest_framework import serializers
77

8-
from marsha.core.defaults import CELERY_PIPELINE
8+
from marsha.core.defaults import AWS_STORAGE_BASE_DIRECTORY, CELERY_PIPELINE
99
from marsha.core.models import Thumbnail
1010
from marsha.core.serializers.base import TimestampField
1111
from marsha.core.storage.storage_class import file_storage
@@ -122,10 +122,10 @@ def get_urls(self, obj):
122122
urls[resolution] = file_storage.url(f"{base}/{resolution}.jpg")
123123
return urls
124124

125-
# Default AWS fallback:
126-
base = f"{settings.AWS_S3_URL_PROTOCOL}://{settings.CLOUDFRONT_DOMAIN}/{obj.video.pk}"
127-
urls = {}
125+
# Default fallback to location under "aws" directory
126+
base = obj.get_storage_prefix(base_dir=AWS_STORAGE_BASE_DIRECTORY)
128127
stamp = time_utils.to_timestamp(obj.uploaded_on)
128+
urls = {}
129129
for resolution in settings.VIDEO_RESOLUTIONS:
130-
urls[resolution] = f"{base}/thumbnails/{stamp}_{resolution}.jpg"
130+
urls[resolution] = file_storage.url(f"{base}/{stamp}_{resolution}.jpg")
131131
return urls

src/backend/marsha/core/tests/api/thumbnails/test_retrieve.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
)
2424

2525

26+
# flake8: noqa: E501
27+
# pylint: disable=line-too-long
28+
29+
2630
class ThumbnailRetrieveApiTest(TestCase):
2731
"""Test the retrieve API of the thumbnail object."""
2832

@@ -199,7 +203,6 @@ def test_api_thumbnail_instructor_read_detail_in_read_only(self):
199203

200204
self.assertEqual(response.status_code, 403)
201205

202-
@override_settings(CLOUDFRONT_SIGNED_URLS_ACTIVE=False)
203206
def test_api_thumbnail_read_detail_token_user(self):
204207
"""Instructors should be able to read details of thumbnail associated to their video."""
205208
video = VideoFactory(
@@ -245,7 +248,6 @@ def test_api_thumbnail_administrator_read_detail_in_read_only(self):
245248

246249
self.assertEqual(response.status_code, 403)
247250

248-
@override_settings(CLOUDFRONT_SIGNED_URLS_ACTIVE=False)
249251
def test_api_thumbnail_read_detail_admin_user(self):
250252
"""Admin should be able to read details of thumbnail associated to their video."""
251253
video = VideoFactory(
@@ -278,6 +280,7 @@ def test_api_thumbnail_read_detail_admin_user(self):
278280
},
279281
)
280282

283+
@override_settings(MEDIA_URL="https://abc.svc.edge.scw.cloud/")
281284
def test_api_thumbnail_read_ready_thumbnail(self):
282285
"""A ready thumbnail should have computed urls."""
283286
video = VideoFactory(
@@ -310,15 +313,15 @@ def test_api_thumbnail_read_ready_thumbnail(self):
310313
"is_ready_to_show": True,
311314
"upload_state": "ready",
312315
"urls": {
313-
"240": "https://abc.cloudfront.net/78338c1c-356e-4156-bd95-5bed71ffb655/"
316+
"240": "https://abc.svc.edge.scw.cloud/aws/78338c1c-356e-4156-bd95-5bed71ffb655/"
314317
"thumbnails/1533686400_240.jpg",
315-
"360": "https://abc.cloudfront.net/78338c1c-356e-4156-bd95-5bed71ffb655/"
318+
"360": "https://abc.svc.edge.scw.cloud/aws/78338c1c-356e-4156-bd95-5bed71ffb655/"
316319
"thumbnails/1533686400_360.jpg",
317-
"480": "https://abc.cloudfront.net/78338c1c-356e-4156-bd95-5bed71ffb655/"
320+
"480": "https://abc.svc.edge.scw.cloud/aws/78338c1c-356e-4156-bd95-5bed71ffb655/"
318321
"thumbnails/1533686400_480.jpg",
319-
"720": "https://abc.cloudfront.net/78338c1c-356e-4156-bd95-5bed71ffb655/"
322+
"720": "https://abc.svc.edge.scw.cloud/aws/78338c1c-356e-4156-bd95-5bed71ffb655/"
320323
"thumbnails/1533686400_720.jpg",
321-
"1080": "https://abc.cloudfront.net/78338c1c-356e-4156-bd95-5bed71ffb655/"
324+
"1080": "https://abc.svc.edge.scw.cloud/aws/78338c1c-356e-4156-bd95-5bed71ffb655/"
322325
"thumbnails/1533686400_1080.jpg",
323326
},
324327
"video": str(video.id),

0 commit comments

Comments
 (0)