[英]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.我有一个 Django 应用程序,它允许用户上传文件并将其保存到 s3 存储桶。 My question is how could I rename the file before uploading to the bucket?
我的问题是如何在上传到存储桶之前重命名文件?
This is my models.py
class:这是我的
models.py
类:
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:这是我的
backend_storages.py
类:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.