2👍
The following is a rough outline of a possible design. I don’t know the details of your project so I’m making many assumptions. If you’re using S3, you would want to use the boto client. But before that, make sure all files in your bucket are private. Get an access key for you app so that you can make API calls to S3. Then, once you have all this, you could so something like this:
Create a new videofile
app and define a VideoFile
model:
import boto3 as boto
from django.conf import settings
from django.db import models
class VideoFile(models.Model):
owner = models.ForeignKey(settings.AUTH_USER_MODEL)
name = models.CharField(max_length=75)
content_key = models.CharField(max_length=200, unique=True)
def generate_download_url(self, ):
s3 = boto.client('s3')
url = s3.generate_presigned_url(
ClientMethod='get_object',
Params={
'Bucket': settings.S3_BUCKET_NAME,
'Key': self.content_key
}
)
return url
def __str__(self):
return self.name
What does this do? It creates a table that stores S3 file ids (in the content_key
column) and each file has an owner
which is a foreign key to the User object in your django app. generate_download_url
generates presigned urls that expire after a while.
To upload a new video to S3, perhaps something like:
def create_document(file_object, key_name, owner):
try:
s3 = boto.resource('s3')
s3.Object(settings.S3_BUCKET_NAME, key_name).put(Body=file_object)
except Exception, err:
print("Could not create object in s3")
return
video_file = VideoFile.objects.create(owner=owner, name=file_object.name, content_key=key_name)
return video_file
Finally, you can perform the “is_friend_of()” check in the appropriate DRF view and return the temporary url.
class FileStreamUrl(APIView):
def get(self, request):
my_man = User.objects.get(...)
if request.user.is_friend_of(my_man):
video_file = some_user.objects.video_file_set.filter(...)
url = video_file.generate_download_url()
return Response(url)
else:
return Response(status=403) # Forbidden
- [Django]-How do I correctly pass multiple arguments (from the template/context) to a view in Django?