About Manipulating Files from the Default Storage
When you use Django default_storage
with boto3
for object storage, such as AWS S3 or DigitalOcean Spaces, it's not always possible to inspect or modify specific files directly from the object storage, as some libraries support only the file system.
However, you can download the file from default_storage
temporarily and manipulate a copy locally:
import os
import tempfile
from django.core.files.storage import default_storage
from huey.contrib import djhuey as huey
from .models import Video
@huey.db_task()
def extract_data_from_video(video_pk):
video = Video.objects.get(pk=video_pk)
# Create a temporary file to store the video
with tempfile.NamedTemporaryFile(delete=False) as temp_video:
temp_video.write(default_storage.open(video.video_file.name).read())
temp_video_path = temp_video.name
# Do something with the file at temp_video_path...
# Finally, clean up the temporary video file
os.unlink(temp_video_path)
Tips and Tricks Programming Architecture Django 5.x Django 4.2 Django 3.2 Huey Celery django-storages Boto3
Also by me
Django Paddle Subscriptions app
For Django-based SaaS projects.
Django GDPR Cookie Consent app
For Django websites that use cookies.