我只想使用表单上传图像 (JPG),然后将该图像发送到 Rackspace“Cloud Files”或 Amazon“S3”。
- 没有操作该文件。
- 无需保存到磁盘,一切都保存到内存(托管在云服务器上)
- 图片大小不会超过75kb
Update(两个注意事项):
- 一:当数据从手机应用程序发布时,它也需要工作。
- 二:需要发送到Rackspace Cloud Files以及S3(以CF开头)。
下面的代码可以工作,但它太重了。
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)
非常感谢,
希望一切安好 ...
d.
完全忽略 python 并直接上传到 s3 怎么样?
您可以将 s3 存储桶配置为禁止上传任何大于 $X 字节的文件。
这是一个简单的例子来说明直接上传到s3(并忽略你的图像宽度/高度条件)
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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)