使用Django将图像上传到Amazon S3 [英] Upload Images to Amazon S3 using Django

查看:144
本文介绍了使用Django将图像上传到Amazon S3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户上传图片时,我目前正在调整图片的大小。原始图片存储在Amazon S3上,称为djangobucket。在这个桶中,包含数千个文件夹。



每个文件夹都以用户名命名。我不必担心创建桶或文件夹,因为所有这些都是从客户端处理的。



这是一个图:

  djangobucket ------------>鲍勃---------> picture1.jpg 
picture2.jpg
picture3.jpg
picture4.jpg



你可以看到,鲍勃有很多照片。一旦用户将图片上传到S3,我将通过Django通过URL进行下载,在这种情况下,它将是: http://s3.amazonaws.com/djangobucket/bob/picture1.jpg



我下载图像并执行图像处理并保存在我的django应用服务器上处理的图像。



我想将此处理后的图像发送回bob的文件夹,以便可以在 http://s3.amazonaws.com/djangobucket/bob/picture1_processed.jpg



客户端已经可以访问亚马逊密钥和密钥,以便他或她可以将图片上传到此存储桶。服务上的所有用户使用相同的密钥。我也将使用相同的键。



我听说过Boto的东西,但它涉及到Bucket的创建,我不知道如何做上传部分。我只关心将图片上传到相应的用户文件夹。



我已经研究了几个小时,所以我转向了这里的专家。



我的代码,只是为了更好地了解我在做什么。

  user ='bob'

url ='http://s3.amazonaws.com/djangobucket/bob/picture1.jpg'
filename = url.split('/')[ - 1] .split('。 ')[0]

download_photo = urllib.urlretrieve(url,/home/ubuntu/Desktop/Resized_Images/%s.jpg%(filename))
downloaded_photo = Image.open (/home/ubuntu/Desktop/Resized_Images/%s.jpg%(filename))

resized_photo = downloaded_photo.resize((300,300),Image.ANTIALIAS)
new_filename = filename +_processed

resized_photo.save(/ home / ubuntu / Desktop / Resized_Images /%s.jpg%(new_filename))
/ pre>

我想将保存在/ home / ubuntu / Desktop / Resized_Images /
中的已调整大小的照片发送到Amazon S3上的djangobucket中的Bob的文件夹,公开对待



感谢您的帮助。



编辑
I发现此链接: http:// www.laurentluce.com/posts/upload-and-download-files-tofrom-amazon-s3-using-pythondjango/



不完全如何使用对于我的情况,但我认为我在正确的轨道上。

解决方案

boto 是最好的方法。



您可以使用以下方式获取现有的存储桶:

  get_bucket(bucket_name ,validate = True,headers = None)

安装boto后,此代码应该做你所需要的从

  from boto.s3.connection import S3Connection 
from boto.s3.key import Key

conn = S3Connection('&aws access key>','&aws secret key>')
bucket = conn.get_bucket('< bucket name>')
k = Key桶)
k.key ='file_path_on_s3'#例如'images / bob / resized_image1.png'
k.set_contents_from_file(resized_photo)

以下是 Boto API 的一些链接和 Boto S3 Doc


I'm currently resizing images on the fly when a user uploads a picture. The original picture is stored on Amazon S3 in a bucket called djangobucket. Inside this bucket, contains thousands of folders.

Each folder is named after the user. I don't have to worry about bucket creation or folder creation since all of that is handled from the client side.

Here is a diagram:

djangobucket  ------------> bob ---------> picture1.jpg
                                           picture2.jpg
                                           picture3.jpg
                                           picture4.jpg

As you can see, Bob has many pictures. Once a user uploads a picture to S3, I download it through Django via a URL, in this case it would be: http://s3.amazonaws.com/djangobucket/bob/picture1.jpg

I download the image and perform image processing and save the processed image on my django app server.

I would like to send this processed image back into bob's folder so that it can be publicly reached at http://s3.amazonaws.com/djangobucket/bob/picture1_processed.jpg

The client already has access to the amazon key and secret key so that he or she can upload pictures to this bucket. All users on the service use the same keys. I too will be using the same keys.

I've heard of something called Boto, but it involves Bucket creation and I'm unsure of how to do just the uploading part. I'm only concerned about uploading the picture to the appropriate user folder.

I've been researching this for hours so I've turned to the experts here.

Here is my code, just so that you can get a better understanding of what I'm doing.

user = 'bob'

url = 'http://s3.amazonaws.com/djangobucket/bob/picture1.jpg'
filename = url.split('/')[-1].split('.')[0]

download_photo = urllib.urlretrieve(url, "/home/ubuntu/Desktop/Resized_Images/%s.jpg" % (filename))
downloaded_photo = Image.open("/home/ubuntu/Desktop/Resized_Images/%s.jpg" % (filename))

resized_photo = downloaded_photo.resize((300, 300), Image.ANTIALIAS)
new_filename = filename + "_processed"

resized_photo.save("/home/ubuntu/Desktop/Resized_Images/%s.jpg" % (new_filename)) 

I would like to send the resized photo saved in /home/ubuntu/Desktop/Resized_Images/ to Bob's folder in the djangobucket on Amazon S3 and make it publicly visible.

Thanks for your help.

EDIT I found this link: http://www.laurentluce.com/posts/upload-and-download-files-tofrom-amazon-s3-using-pythondjango/

Not quite how to use it for my case, but I think I'm on the right track.

解决方案

boto is the best way to do this.

You can get an existing bucket using:

get_bucket(bucket_name, validate=True, headers=None)

After installing boto, this code should do what you need to do

from boto.s3.connection import S3Connection
from boto.s3.key import Key

conn = S3Connection('<aws access key>', '<aws secret key>')
bucket = conn.get_bucket('<bucket name>')
k = Key(bucket)
k.key = 'file_path_on_s3' # for example, 'images/bob/resized_image1.png'
k.set_contents_from_file(resized_photo)

Here are some links to the Boto API and Boto S3 Doc

这篇关于使用Django将图像上传到Amazon S3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆