Skip to Content
Python and AWS Cookbook
book

Python and AWS Cookbook

by Mitch Garnaat
October 2011
Intermediate to advanced
74 pages
1h 43m
English
O'Reilly Media, Inc.
Content preview from Python and AWS Cookbook

Chapter 3. S3 Recipes

Create a Bucket

Problem

Before you can store anything in S3, you need to first create a bucket to contain the objects.

Solution

Use the create_bucket method to create a new bucket.

Discussion

Example 3-1 shows a function that takes the name of the bucket you want to create as a parameter. It will then try to create that bucket. If the bucket already exists, it will print an error.

Example 3-1. Create a Bucket
import boto

def create_bucket(bucket_name):
    """
    Create a bucket.  If the bucket already exists and you have
    access to it, no error will be returned by AWS.
    Note that bucket names are global to S3
    so you need to choose a unique name.
    """
    s3 = boto.connect_s3()

    # First let's see if we already have a bucket of this name.
    # The lookup method will return a Bucket object if the
    # bucket exists and we have access to it or None.
    bucket = s3.lookup(bucket_name)
    if bucket:
        print 'Bucket (%s) already exists' % bucket_name
    else:
        # Let's try to create the bucket.  This will fail if
        # the bucket has already been created by someone else.
        try:
            bucket = s3.create_bucket(bucket_name)
        except s3.provider.storage_create_error, e:
            print 'Bucket (%s) is owned by another user' % bucket_name
    return bucket

Create a Bucket in a Specific Location

Problem

You want to create a bucket in a specific geographic location.

Solution

Use the location parameter to the create_bucket method to specify the location of the new bucket.

Discussion

Originally, there was just a single S3 endpoint and all data was ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

AWS Cookbook

AWS Cookbook

John Culkin, Mike Zazon
Data Engineering with Python and AWS Lambda LiveLessons

Data Engineering with Python and AWS Lambda LiveLessons

Noah Gift, Robert Jordan, Kennedy Behrman
Python for DevOps

Python for DevOps

Noah Gift, Kennedy Behrman, Alfredo Deza, Grig Gheorghiu
Terraform Cookbook

Terraform Cookbook

Kerim Satirli, Taylor Dolezal

Publisher Resources

ISBN: 9781449308100Errata Page