Django 上传图像 - 从表单到 Rackspace/S3,无需任何操作
我只想使用表单上传图像 (JPG),然后将该图像发送到 Rackspace“Cloud Files”或 Amazon“S3”。
- 没有操作文件。
- 不保存到磁盘,所有内容都保存到内存(托管在云服务器上)
- 图像大小不会超过 75kb
更新(两个注意事项):
- 一:从手机发布数据时它也需要工作应用程序。
- 二:需要发送到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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
完全忽略 python 并直接上传到 s3 怎么样?
您可以将 s3 存储桶配置为禁止上传任何大于 $X 字节的文件。
这是一个简单的示例来说明直接上传到 s3(并忽略图像宽度/高度条件)
http://sente.cc /upload_to_s3.html
代码:
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:
整理出来了
找到了一种更简单优雅的方法,并为没有早点使用它而感到愚蠢。
Sorted out.
Found a simpler elegant approach and feel stupid for not getting to it earlier.