1

I am using django-storages' s3boto backend as my default storage backend.

If I upload to my publicly accessible bucket via AWS console, I have no problem accessing it. If I use my Django form to save a file, the file appears in the AWS console with its correct size. But the link to file displays a broken image, both using AWS's open menu command and in the Django admin.

My relavent settings are:

AWS_ACCESS_KEY_ID = '***'
AWS_SECRET_ACCESS_KEY = '***'
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
AWS_STORAGE_BUCKET_NAME = 'mybucket'
AWS_QUERYSTRING_AUTH = False

Update:

I checked the image url with curl -v, the response is HTTP/1.1 200 OK. The response headers are:

< HTTP/1.1 200 OK
< x-amz-id-2: /kR/hcgWI2vUOP7/1+kqAsKfSSx4BWtxnipNcQAadeRaJzmNuBIgD17Wff9N856T
< x-amz-request-id: 17C13B7A2B20E50A
< Date: Thu, 19 Jul 2012 05:05:58 GMT
< Last-Modified: Wed, 18 Jul 2012 19:26:48 GMT
< ETag: "35b2ef1fb7552b850cf477f04141dddc"
< Accept-Ranges: bytes
< Content-Type: image/jpeg
< Content-Length: 101086
< Server: AmazonS3

Then the binary bytes start flowing. So the file must be corrupted during the upload somehow.

Because I am using <input type="file" multiple> in the upload form, I handle the file upload manually like this:

if self.request.FILES:
    files = self.request.FILES.getlist('files')
    for file in files:
        try:
            # Check the file is an image
            Image.open(file).verify()
        except:
            # not an image
            pass
        else:
            photo = Photo()
            photo.image = file
            photo.save()

This works fine with the default storage backend, but apparently there is something wrong when using s3boto backend.

What do you think I might be doing wrong?

onurmatik
  • 5,105
  • 7
  • 42
  • 67
  • What is the HTTP response code you get when you try to access the URL? – enticedwanderer Jul 18 '12 at 22:31
  • I checked with `curl -v`, it is `HTTP/1.1 200 OK`, and the binary file starts flowing on the terminal. Then it must be that the file is corrupted somehow, right? Thanks for the insight, I will update the question. – onurmatik Jul 19 '12 at 05:11
  • It could be its corrupted or it may have wrong MIME set on it and its not recognized as an image but rather as a binary blob. See if you can use curl to dump it into a file and open in image editor to confirm its not corrupted. – enticedwanderer Jul 19 '12 at 05:31
  • I download the image and it is corrupt. MIME looks correct, as can be seen from the response header. The corrupt one is 101086 bytes (as in the response header), the original is 101760 bytes. Apparently some bytes are lost somewhere during upload or download. I tried this many times with different images, always a smal part is left out. – onurmatik Jul 19 '12 at 05:44
  • 1
    It turned out that the problem is `Image.open()` check I make to make sure the file is actually an image. Then the file should be reset using `file.seek(0)`. – onurmatik Jul 19 '12 at 06:58
  • @omat thanks so much for that find. `file.seek(0)` fixed it for me as well. – jasongregori Aug 20 '13 at 17:23

0 Answers0