gpt4 book ai didi

python - Django 上传图像 - 从表单到 Rackspace/S3,无需任何操作

转载 作者:行者123 更新时间:2023-12-03 00:59:31 24 4
gpt4 key购买 nike

我只想使用表单上传图像 (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。

最佳答案

完全忽略 python 并直接上传到 s3 怎么样?

您可以将 s3 存储桶配置为禁止上传任何大于 $X 字节的文件。

这是一个简单的示例来说明直接上传到 s3(并忽略图像宽度/高度条件)

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>

关于python - Django 上传图像 - 从表单到 Rackspace/S3,无需任何操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6911567/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com