[717] Using Amazon's S3 service with Django (1/1)
in series: More Django: Intermediate level Django screencasts
video tutorial by Siddhi
Name:
[1982] Siddhi
Member:
30 months
Authored:
4 videos
Description:
I'm an entrepreneur from Chennai, India and I have my own company `Silver Stripe Software <http://www.silverstripesoftware.com>`_ where I develop tools for agile software development using Python and ...
Our authors tell us that feedback from you is a big motivator. Please take a few moments to let them know what you think of their work.
Amazon's S3 service has been a hit with developers who need to store and serve large amounts of data in a scalable way. In this screencast we'll see how we can use S3 in a Django application.
We'll build a simple gallery app, but instead of storing the photos locally, we'll store them on S3 and then serve the photos directly from the S3 server. In the process, we'll learn the basics of using the boto library to interact with Amazon Web Services.
This screencast assumes you know the basics of Django. If you are new to Django, take a look at the Learn Django series on showmedo.
Links
boto: python library for Amazon web services Amazon S3 home page
Video Tutorials related by tag:
Got any questions?
Get answers in the ShowMeDo Learners Google Group.
Video statistics:
- Video's rank shown in the most popular listing
- Video plays: 2085 (since July 30th)
- Plays in last week: 12
- Published: 20 months ago
# Create your views here.
import mimetypes
from django.shortcuts import render_to_response
from django import newforms as forms
from django.conf import settings
from boto.s3.connection import S3Connection
from boto.s3.key import Key
from models import PhotoUrl
class UploadForm(forms.Form):
file = forms.ImageField(label='Select photo to upload')
def index(request):
def store_in_s3(filename, content):
conn = S3Connection(settings.ACCESS_KEY, settings.PASS_KEY)
b = conn.create_bucket("siddhartafiles")
mime = mimetypes.guess_type(filename)[0]
k = Key(b)
k.key = filename
k.set_metadata("Content-Type", mime)
k.set_contents_from_string(content)
k.set_acl("public-read")
photos = PhotoUrl.objects.all().order_by("-uploaded")
if not request.method == "POST":
f = UploadForm()
return render_to_response("index.html", {"form":f, "photos":photos})
f = UploadForm(request.POST, request.FILES)
if not f.is_valid():
return render_to_response("index.html", {"form":f, "photos":photos})
file = request.FILES["file"]
filename = file["filename"]
content = file["content"]
store_in_s3(filename, content)
p = PhotoUrl(url="http://siddhartafiles.s3.amazonaws.com/" + filename)
p.save()
photos = PhotoUrl.objects.all().order_by("-uploaded")
return render_to_response("index.html", {"form":f, "photos":photos})
Thank-yous, questions and comments
If this video tutorial was helpful please take some time to say thank-you to the authors for their hard work. Feel free to ask questions. Let the author know why their video tutorial was useful - what are you learning about? Did the video tutorial save you time? Would you like to see more?
You may also want to see our ShowMeDo Google Group to speak to our active users and authors.
like it
very nice tutorial. keep up the good work.
Nice Tutorial
Thanks for showing how easy it is to use boto with s3.
Very informative! Thanks for taking the time to do this video.
You don't want to transfer the upload contents to your server then to S3. you should instead have the user's browser directly upload to S3.
See:
Thanks so much for this! Very easy to follow!
Do you know how to embed amazon store to our website?
Interesting video. I'm trying to understand the uses of Django with hosting and clouds.
Thanks Siddhi. Great job as always. How about some more intermediate Django vids?
Thunderbird 3: I love Firefox but it loads pages slowly. ,
What is the social philosophy of the document? ,
Great information! Thanks for your work!
that was awesome dude...keep it up, I'm new to django and I'm learning a lot thanks dude
Big thanks, it helps me alot :)
Hey Sid, That was an excellent intro to s3. I have bookmarked your screen cast for future reference. I really hope you find time to upload more AWS screen casts. Maybe something about hosting the Django app on EC2?
Once again, thank you!
Best,
Saravana
Thanks for all the comments. For anyone viewing this video, please keep in mind the changes you need to do for the latest Django version. You can find the changes in the comments.
@anonymous You can find out more about authentication in S3 here - http://docs.amazonwebservices.com/AmazonS3/latest/UsingAuthAccess.html
Hi Siddhi,
Awesomee...this is going to be of great help. I am trying to make an application which is running on Django and stores images on S3. ..almost the same thing which u did in your video. Only thing i want to know/figure out is how to add users on my website...i.e. All users can upload thier images in a one big bucket and user A can not access user Bs data. Any idea...suggestions..?
Fantastic your video about S3,
Its not for me now, but maybe in future.
Thank very much Siddhi
Santi from Spain
I am a fan of you
Nice Tutorial thx for everything
Great! Would love to see a part2 where you integrate the use of SimpleDB to this app.
Thanks Siddhi for your Django tutorials.
I've just finished the Django official tutorial and this is an excelent "desert".
Hey great video, I came here looking for s3 manipulation technicques but ended up watching it anyways cuz I like django too! thanks for sharing! :)
they changed the way fil upload handeling works in latest django code. If you are having problems, try this:
filename = file.name
content = file.read()
eventually do a read by chunks so not to get slammed by large file:
http://docs.djangoproject.com/en/dev/topics/http/file-uploads/#handling-uploaded-files
they changed the way fil upload handeling works in latest django code. If you are having problems, try this:
filename = file.name
content = file.read()
eventually do a read by chunks so not to get slammed by large file:
http://docs.djangoproject.com/en/dev/topics/http/file-uploads/#handling-uploaded-files
Thanks, your presentation was excellent. Some of you code may now need to be modified now, fyi. If I'm not mistaken, I think newforms has been replaced with the following code (which is the old way):
from django import forms
(instead of, from django import newforms as forms)
see documentation at the following link: http://www.djangoproject.com/documentation/0.96/newforms/
Thanks again!
Very useful and clear!
Thanks a million, It's very clear and easy to follow. I'm trying to learn python + django in order to develop a few small webs and place them in the amazon cloud and I can say that you have helped me greatly (Oh! And python looks great)
Thanks Siddhi, that was exactly what I was looking for! Other examples made it all seem very complicated, now that I've seen the basics I can start adding more layers.
Thanks,
Tyson
They changed the upload handling. in the newest trunk. http://www.djangoproject.com/documentation/upload_handling/
Anyone here, who knows what i have to change ???
Ahhh, sorry my foult, it was my firewall at port 443 !!!
When i test this script, my django dev server hangs-up after a picture post. I use the newest django trunk.
Wow great tutorial, really professional.
Siddhi, your videos are outstanding. Thanks for the great job, your screencasts are inspiring.
Randomly checked ShowMeDo for updates today and saw you added a new Screenscat so I just HAD to watch it :)
Wow that was quick :) Thanks for your comment.
Yet another great screencast Siddhi!
You're doing a great job. I appreciate that.
Video published, thanks for contributing to ShowMeDo



