Skip to content
This repository was archived by the owner on May 29, 2025. It is now read-only.

Commit d8d32eb

Browse files
author
Andrew Fitz Gibbon
committed
Expand the comments to be more descriptive
1 parent 9b795d4 commit d8d32eb

File tree

1 file changed

+38
-10
lines changed

1 file changed

+38
-10
lines changed

s3_sample.py

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,58 @@
1616
import boto
1717
import uuid
1818

19-
# Create an S3 client
19+
# Instantiate a new client for Amazon Simple Storage Service (S3). With no
20+
# parameters or configuration, the AWS SDK for Python (Boto) will look for
21+
# access keys in these environment variables:
22+
#
23+
# AWS_ACCESS_KEY_ID='...'
24+
# AWS_SECRET_ACCESS_KEY='...'
25+
#
26+
# For more information about this interface to Amazon S3, see:
27+
# http://boto.readthedocs.org/en/latest/s3_tut.html
2028
s3 = boto.connect_s3()
2129

22-
# Create a new bucket.
30+
# Everything uploaded to Amazon S3 must belong to a bucket. These buckets are
31+
# in the global namespace, and must have a unique name.
32+
#
33+
# For more information about bucket name restrictions, see:
34+
# http://docs.aws.amazon.com/AmazonS3/latest/dev/BucketRestrictions.html
2335
bucket_name = "python-sdk-sample-%s" % uuid.uuid4()
2436
print "Creating new bucket with name: " + bucket_name
2537
bucket = s3.create_bucket(bucket_name)
2638

27-
# Upload some data
39+
# Files in Amazon S3 are called "objects" and are stored in buckets. A specific
40+
# object is referred to by its key (i.e., name) and holds data. Here, we create
41+
# a new object with the key "python_sample_key.txt" and content "Hello World!".
42+
#
43+
# For more information on keys and set_contents_from_string, see:
44+
# http://boto.readthedocs.org/en/latest/s3_tut.html#storing-data
2845
from boto.s3.key import Key
2946
k = Key(bucket)
30-
k.key = 'hello_world.txt'
47+
k.key = 'python_sample_key.txt'
3148

3249
print "Uploading some data to " + bucket_name + " with key: " + k.key
33-
k.set_contents_from_string('This is a test of S3. Hello World!')
50+
k.set_contents_from_string('Hello World!')
3451

35-
# Fetch the key to show that we stored something.
36-
print "Downloading the object we just uploaded:"
52+
# Fetch the key to show that we stored something. Key.generate_url will
53+
# construct a URL that can be used to access the object for a limited time.
54+
# Here, we set it to expire in 30 minutes.
55+
#
56+
# For a more detailed overview of generate_url's options, see:
57+
# http://boto.readthedocs.org/en/latest/ref/s3.html#boto.s3.key.Key.generate_url
58+
expires_in_seconds = 1800
59+
60+
print "Generating a public URL for the object we just uploaded. This URL will be active for %d seconds" % expires_in_seconds
3761
print
38-
print k.get_contents_as_string()
62+
print k.generate_url(expires_in_seconds)
3963
print
64+
raw_input("Press enter to delete both the object and the bucket...")
4065

41-
print "Now delete the same object"
66+
# Buckets cannot be deleted unless they're empty. Since we still have a
67+
# reference to the key (object), we can just delete it.
68+
print "Deleting the object."
4269
k.delete()
4370

44-
print "And finally, delete the bucket."
71+
# Now that the bucket is empty, we can delete it.
72+
print "Deleting the bucket."
4573
s3.delete_bucket(bucket_name)

0 commit comments

Comments
 (0)