Django upload image - From a form to Rackspace/S3 with no manipulation

爱⌒轻易说出口 提交于 2019-12-01 18:02:48

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>

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())
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!