简体   繁体   中英

How can I rename an uploaded file on Django before sending it to amazon s3 bucket?

I have a Django App which allows a user to upload a file and save it to an s3 bucket. My question is how could I rename the file before uploading to the bucket?

This is my models.py class:

from converter.storage_backends import CsvStorage
from django.db import models
from django.utils import timezone


class CSVUpload(models.Model):
  csv_file = models.FileField(storage=CsvStorage())

  def __str__(self):
    return self.csv_file

And this is my backend_storages.py class:

from storages.backends.s3boto3 import S3Boto3Storage 
from django.conf import settings


class CsvStorage(S3Boto3Storage):    
   location = settings.AWS_CSV_LOCATION    
   file_overwrite = False

Could anyone help me understand how to go about it? I appreciate any help you can provide

You would need to override the save function. I did it something like this:

class Documents(AuditFields):
document_file = models.FileField(max_length=10000)

def save(self, *args, **kwargs):
    self.document_file.name = 'some_prefix' + self.document_file.name
    super(PlantDocument, self).save(*args, **kwargs)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM