Error uploading files with the same name.
I'm using django-storages on my django project, and I need upload to S3 my project images... On my setting I have:
- Django-storages AWS_CALLING_FORMAT = CallingFormat.SUBDOMAIN
- Default storage DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
- Keys AWS_ACCESS_KEY_ID = 'access-key' AWS_SECRET_ACCESS_KEY = 'secret-access-key' AWS_STORAGE_BUCKET_NAME = 'my-bucket'
When I upload some image, it was save on s3 correctly, but, if I upload another file with the same name, it overwrites my first image, and I don't need it... The ideal situation would be that the new file would rename another default name... How I can fix it?
And, on my templates, waht is the correct way to call my images from amazon s3? I don't want copy the complete amazon url on my template... Can somebody help me??
Thanks...
Comments (3)
-
Ian Lewis
-
aaquirogal
Ok.. thanks...
Then, I have created a function to add the datetime to the filename...
I want to share it..
import datetime def fileholder(instance,filename): now_str = str(datetime.datetime.now()) now = now_str.replace(' ','-') print now ext = "xxx" try : title_str = filename.split(".")[-0] title = title_str.replace(' ', '-') ext = filename.split(".")[-1] except: pass return '/'.join(["uploads/images/image",title+now+"."+ext])
and on my model ...
from django.db import models from utils import fileholder class Image(models.Model): """ Representa una imagen """ title = models.CharField( max_length=200, ) image = models.ImageField( max_length=100, upload_to= fileholder, ) content_file = models.FileField( upload_to=fileholder, blank=True, null=True, )
Thanks... :o)
-
Can you please updated your documentation to include this rather important setting?
Thanks.
- Log in to comment »
There is a parameter called AWS_S3_FILE_OVERWRITE which defaults to True. This parameter controls whether the s3boto backend will overwrite files or not when they have the same name. You can set this setting to False in your settings.py to enable file name checking.
However,
Checking for the same name of a file means that the backend has to make extra requests to s3 to check if the file exists or not which creates overhead. It also creates concurrency problems since a file could be written between the time that the backend checks that it exists and when it actually writes it.
For these reasons it's recommended that you use a file naming scheme that generates unique file names for all uploaded files rather than writing to a unique file name after checking if it exists.