Sorl Thumbnail + Amazon S3

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
# sorl/thumbnail/base.py:
def generate(self):
    """
    Generates the thumbnail if it doesn't exist or if the file date of the
    source file is newer than that of the thumbnail.
    """
    # Ensure dest(ination) attribute is set
    if not self.dest:
        raise ThumbnailException("No destination filename set.")
    
    new_generated = False
    if not isinstance(self.dest, basestring):
        # We'll assume dest is a file-like instance if it exists but isn't
        # a string.
        self._do_generate()
        new_generated = True
        
    elif not isfile(self.dest) or (self.source_exists and
        getmtime(self.source) > getmtime(self.dest)):

        import events.s3 as s3_events
        if s3_events.is_on_s3(self.relative_dest):
             # "thumb is on s3"
            s3_events.pull_from_s3(self.relative_dest)
            self._source_exists = True
        else:
             # "thumb not on s3"
            if not self.source_exists:
                # file's missing.
                if s3_events.is_on_s3(self.relative_source):
                    s3_events.pull_from_s3(self.relative_source)
                    self._source_exists = True
                else:
                     # "source is not on S3!"
                    self._source_exists = False

            if self.source_exists:
                # Ensure the directory exists
                directory = dirname(self.dest)
                if not isdir(directory):
                    os.makedirs(directory)

                self._do_generate()
                new_generated = True

    if new_generated:
        s3_events.push_to_s3(self.relative_dest)

def _get_relative_source(self):
    # Hack.
    try:
        start_str = self.relative_dest[:7]
        return self.source[self.source.find(start_str):]
    except:
        return self.source
relative_source = property(_get_relative_source)




# events/s3.py
from django.conf import settings
import libraries.backends.s3 as s3

def push_to_s3(file_path):
    s3_storage = s3.S3Storage()
    img_file = open("%s%s" % (settings.MEDIA_ROOT,file_path),'r')
    s3_img_file = s3_storage.open("%s" % (file_path), 'w')
    s3_img_file.write(img_file.read())
    img_file.close()
    s3_img_file.close()

def is_on_s3(file_path):
    s3_storage = s3.S3Storage() 
    return s3_storage.exists(file_path)
    
def pull_from_s3(file_path):
    s3_storage = s3.S3Storage()     
    img_file = open("%s%s" % (settings.MEDIA_ROOT,file_path),'w')
    s3_img_file = s3_storage.open(file_path, 'r')
    img_file.write(s3_img_file.read())
    s3_img_file.close()
    img_file.close()


# models.py
class Screenshot(SixLinksModel):
    shot = models.ImageField("Screenshot",upload_to="screenshots")

    def save(self):
        super(Screenshot, self).save()      
        import events.s3 as s3_events
        s3_events.push_to_s3(self.shot)

    def __unicode__(self):
        return "%s" % (self.shot)


# assumes django-storage is sitting in libraries, e.g. libraries/backends/s3.py is a file

# settings.py
AWS_ACCESS_KEY_ID = "YOUR-KEY"
AWS_SECRET_ACCESS_KEY = "YOUR-SECRET-KEY"
AWS_STORAGE_BUCKET_NAME = "YOUR-BUCKET"
from S3 import CallingFormat
AWS_CALLING_FORMAT = CallingFormat.PATH

More like this

  1. Custom collectstatic that uses etag and md5 digests to determine whether files on S3 have changed by millarm 1 year, 2 months ago
  2. Admin list thumbnail by theetderks 6 years, 11 months ago
  3. load m2m fields objects by dirol 3 years, 10 months ago
  4. ResizeImageField by wim 3 years, 6 months ago
  5. ImageWithThumbsField by zenx 5 years, 3 months ago

Comments

(Forgotten your password?)