Issue #102 resolved
Assignee
Ian Lewis
Type
bug
Priority
major
Status
resolved
Component
Milestone
Version
Watchers
2

Error uploading files with the same name.

aaquirogal avataraaquirogal created an issue

I'm using django-storages on my django project, and I need upload to S3 my project images... On my setting I have:

  1. Django-storages AWS_CALLING_FORMAT = CallingFormat.SUBDOMAIN
  2. Default storage DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
  1. 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)

  1. Ian Lewis

    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.

  2. 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)

  3. Log in to comment »
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.