5

I simply want to upload an image (JPG) using a form, then send that image to Rackspace 'Cloud Files' or Amazon 'S3'.

  • No manipulating the file.
  • No saving to disk, everything to memory (am hosted on a cloud server)
  • Image size won't exceed 75kb

Update (Two Caveats):

  • One: It also needs to work when data is posted from a phone app.
  • Two: It needs to be sent to Rackspace Cloud Files as well as S3 (starting with CF).

The code below works but it is way WAY too heavy.

import cloudfiles as cf
def uploadImage(request, id):

  cf_con = cf.get_connection(username='YYY', api_key='XXX', serviceNet=True)
  container = cf_con.get_container('container_name')

  file = request.FILES["item_photo"]
  f = StringIO(file.read())
  f = Image.open(f)

  ### Only works if I resize for some reason, otherwise uploads a broken file
  image = f.resize((600,600), Image.ANTIALIAS)
  o = StringIO()
  image.save(o, "JPEG", quality=80)
  image = o.getvalue()

  file_name  = "%s/%s" % (id, '600x600.jpeg')

  ### This simply uploads to Rackspace Cloud files.
  put_file(container, file_name, image)

Thanks so much, Hope all is well ...

d.

Danny
  • 199
  • 10
  • 1
    I'm confused, your example code resizes the image but your description says _No manipulating the file_. Which is it? If you want to resize the image then a lot of what you're doing is necessary. – sente Aug 02 '11 at 14:27
  • 1
    Thanks @stuart, I don't want to resize the image, I was trying to upload without the manipulation but it uploaded a broken file, the code above works but is too heavy and forces a resize. In a perfect world I'd love to simply: `file = request.FILES["item_photo"]` `file_name = "%s/%s" % (id, '600x600.jpeg')` `put_file(container, file_name, file)` But that doesn't work for me. – Danny Aug 02 '11 at 14:31

2 Answers2

3

How about ignoring python all together and just uploading directly to s3?

You can configure your s3 bucket to disallow uploading any files larger than $X bytes.

Here's a simple example to illustrate uploading directly to s3 (and ignoring your image width/height conditions)

http://sente.cc/upload_to_s3.html

code:

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  </head>
  <body>
  <h3>refresh the page after you've submitted to see your new image</h3>
    <div style="width:300px">
    <form action="http://s3.amazonaws.com/dev.sente" method="post" enctype="multipart/form-data">
      <fieldset>
      <input type="hidden" name="acl" value="public-read" /> <br />
      <i>name of key:</i><input type="text" name="key" readonly="true" value="image.jpg" /> <br />
      <input name="file" type="file" /> <br />
      <input name="submit" value="Upload" type="submit" />
    </fieldset>
    </form>
  </div>
    <br>
    <a href="http://s3.amazonaws.com/dev.sente/image.jpg">http://s3.amazonaws.com/dev.sente/image.jpg</a>
      <br>
      <a href="http://s3.amazonaws.com/dev.sente/image.jpg"><img src="http://s3.amazonaws.com/dev.sente/image.jpg"></a>
    </a>
  </body>
</html>
sente
  • 2,327
  • 2
  • 18
  • 24
  • 1
    Hey, that's pretty awesome. I hadn't thought of that. My only problem is two caveats. - One: It also needs to work when data is posted from a phone app. - Two: It needs to be sent to Rackspace Cloud Files. – Danny Aug 02 '11 at 12:55
2

Sorted out. Found a simpler elegant approach and feel stupid for not getting to it earlier.

file = request.FILES["item_photo"]
file_name = "%s/%s" % (id, '600.jpeg')
put_file(container, file_name, file.read())
Danny
  • 199
  • 10