|
16 | 16 | import boto
|
17 | 17 | import uuid
|
18 | 18 |
|
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 |
20 | 28 | s3 = boto.connect_s3()
|
21 | 29 |
|
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 |
23 | 35 | bucket_name = "python-sdk-sample-%s" % uuid.uuid4()
|
24 | 36 | print "Creating new bucket with name: " + bucket_name
|
25 | 37 | bucket = s3.create_bucket(bucket_name)
|
26 | 38 |
|
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 |
28 | 45 | from boto.s3.key import Key
|
29 | 46 | k = Key(bucket)
|
30 |
| -k.key = 'hello_world.txt' |
| 47 | +k.key = 'python_sample_key.txt' |
31 | 48 |
|
32 | 49 | 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!') |
34 | 51 |
|
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 |
37 | 61 | print
|
38 |
| -print k.get_contents_as_string() |
| 62 | +print k.generate_url(expires_in_seconds) |
39 | 63 | print
|
| 64 | +raw_input("Press enter to delete both the object and the bucket...") |
40 | 65 |
|
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." |
42 | 69 | k.delete()
|
43 | 70 |
|
44 |
| -print "And finally, delete the bucket." |
| 71 | +# Now that the bucket is empty, we can delete it. |
| 72 | +print "Deleting the bucket." |
45 | 73 | s3.delete_bucket(bucket_name)
|
0 commit comments