Django上传图片 - 从表单到Rackspace / S3,无需操纵 [英] Django upload image - From a form to Rackspace/S3 with no manipulation
问题描述
我只想使用表单上传图片(JPG),然后将该图像发送到RackspaceCloud Files或AmazonS3。
I simply want to upload an image (JPG) using a form, then send that image to Rackspace 'Cloud Files' or Amazon 'S3'.
- 无法处理该文件。
- 不保存到磁盘,所有内容(托管在一个云服务器)
- 图像大小不会超过75kb
/ strong>(两个警告):
Update (Two Caveats):
- 一:在手机应用发布数据时也需要工作。
- 二:需要发送到Rackspace Cloud Files以及S3(以CF开头)。
下面的代码工作,但它是WAY太重了。
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。
推荐答案
忽略python所有在一起,直接上传到s3?
How about ignoring python all together and just uploading directly to s3?
您可以配置您的s3桶,以禁止上传任何大于$ X字节的文件。
You can configure your s3 bucket to disallow uploading any files larger than $X bytes.
这是一个简单的例子,说明直接上传到s3(并忽略了你的图像宽度/高度条件)
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
代码:
<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>
这篇关于Django上传图片 - 从表单到Rackspace / S3,无需操纵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!