pythondjangoamazon-web-servicesamazon-s3path

django aws S3 define upload file path and file dynamically


In my Django app, I would like to define upload path and file dynamically when saving to AWS S3. I am able to so far save to S3 directly the file however, I want to set the path and filename it self.

So for example, upon upload i want it to be in the S3 path bucketname\employeeid\file_randomnumber.png

How can i do something like this ?

Below are my code:

https://gitlab.com/firdausmah/railercom/blob/master/railercomapp/api.py

@api_view(['POST'])
def update_employee_image(request):
    # ----- YAML below for Swagger -----
    """
    description: update employee image.
    parameters:
      - name: employee_id
        type: integer
        required: true
        location: form
      - name: face_image
        type: file
        required: true
        location: form
    """
    parser_classes = (FileUploadParser,)
    employee_id = request.POST['employee_id']
    face_image_obj = request.data['face_image']

    employee = Employee.objects.get(id = employee_id)
    logging.debug(f"API employee username {employee.username}")
    #employee.face_image = face_image_obj
    employee.upload = face_image_obj <--- here is where it assign the file to S3
    employee.save()
    return Response("Employee Updated!", status=status.HTTP_200_OK)

https://gitlab.com/firdausmah/railercom/blob/master/railercomapp/models.py

class Employee(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='employee')
    company = models.ForeignKey(Company)
    username = models.CharField(max_length=30, blank=False)
    upload = models.FileField(blank=True) <--- S3 field

https://gitlab.com/firdausmah/railercom/blob/master/railercom/settings.py (AWS settings)

AWS_ACCESS_KEY_ID = config('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = config('AWS_SECRET_ACCESS_KEY')
AWS_STORAGE_BUCKET_NAME = config('AWS_STORAGE_BUCKET_NAME')
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
AWS_S3_OBJECT_PARAMETERS = {
    'CacheControl': 'max-age=86400',
}

DEFAULT_FILE_STORAGE = 'railercomapp.storage_backends.MediaStorage'

https://gitlab.com/firdausmah/railercom/blob/master/railercomapp/storage_backends.py from storages.backends.s3boto3 import S3Boto3Storage

class MediaStorage(S3Boto3Storage):
    location = 'media/yy'
    file_overwrite = False

I have based my django aws S3 solution on https://simpleisbetterthancomplex.com/tutorial/2017/08/01/how-to-setup-amazon-s3-in-a-django-project.html.


Solution

  • You can override the storage get_available_name method.

    Here's an example. Modify to get the exact filename scheme you want.

    class MediaStorage(S3Boto3Storage):
        location = 'media/yy'
        file_overwrite = False
    
        def get_available_name(self, name, max_length=None):
            custom_name = f'/employeeid/{name}_randomnumber.png'
            return super().get_available_name(custom_name, max_length)
    

    Docs: Writing a custom storage system