diff options
| author | com4 | 2011-12-13 14:43:56 -0800 |
|---|---|---|
| committer | com4 | 2011-12-13 14:43:56 -0800 |
| commit | beefca8d5588b8db960e600b0372ad4795119ea3 (patch) | |
| tree | a13b16dbd41bb39269c7649fcc2f2ad77137500a /s3-driver.py | |
| parent | db95070a37f70a00f80a1a2a9ba732c55f6bee1b (diff) | |
| parent | 647c078f0c801eb9fa63dfc7ba3c017b30e4d467 (diff) | |
| download | amazons3-py-master.tar.gz amazons3-py-master.zip | |
Added the project to pypi.
Diffstat (limited to 's3-driver.py')
| -rw-r--r-- | s3-driver.py | 118 |
1 files changed, 0 insertions, 118 deletions
diff --git a/s3-driver.py b/s3-driver.py deleted file mode 100644 index 29f700b..0000000 --- a/s3-driver.py +++ /dev/null | |||
| @@ -1,118 +0,0 @@ | |||
| 1 | #!/usr/bin/env python | ||
| 2 | |||
| 3 | # This software code is made available "AS IS" without warranties of any | ||
| 4 | # kind. You may copy, display, modify and redistribute the software | ||
| 5 | # code either by itself or as incorporated into your code; provided that | ||
| 6 | # you do not remove any proprietary notices. Your use of this software | ||
| 7 | # code is at your own risk and you waive any claim against Amazon | ||
| 8 | # Digital Services, Inc. or its affiliates with respect to your use of | ||
| 9 | # this software code. (c) 2006-2007 Amazon Digital Services, Inc. or its | ||
| 10 | # affiliates. | ||
| 11 | |||
| 12 | import S3 | ||
| 13 | import time | ||
| 14 | import sys | ||
| 15 | |||
| 16 | AWS_ACCESS_KEY_ID = '<INSERT YOUR AWS ACCESS KEY ID HERE>' | ||
| 17 | AWS_SECRET_ACCESS_KEY = '<INSERT YOUR AWS SECRET ACCESS KEY HERE>' | ||
| 18 | # remove these next two lines when you've updated your credentials. | ||
| 19 | print "update s3-driver.py with your AWS credentials" | ||
| 20 | sys.exit(); | ||
| 21 | |||
| 22 | # convert the bucket to lowercase for vanity domains | ||
| 23 | # the bucket name must be lowercase since DNS is case-insensitive | ||
| 24 | BUCKET_NAME = AWS_ACCESS_KEY_ID.lower() + '-test-bucket' | ||
| 25 | KEY_NAME = 'test-key' | ||
| 26 | |||
| 27 | conn = S3.AWSAuthConnection(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) | ||
| 28 | generator = S3.QueryStringAuthGenerator(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) | ||
| 29 | |||
| 30 | |||
| 31 | # Check if the bucket exists. The high availability engineering of | ||
| 32 | # Amazon S3 is focused on get, put, list, and delete operations. | ||
| 33 | # Because bucket operations work against a centralized, global | ||
| 34 | # resource space, it is not appropriate to make bucket create or | ||
| 35 | # delete calls on the high availability code path of your application. | ||
| 36 | # It is better to create or delete buckets in a separate initialization | ||
| 37 | # or setup routine that you run less often. | ||
| 38 | if (conn.check_bucket_exists(BUCKET_NAME).status == 200): | ||
| 39 | print '----- bucket already exists! -----' | ||
| 40 | else: | ||
| 41 | print '----- creating bucket -----' | ||
| 42 | print conn.create_located_bucket(BUCKET_NAME, S3.Location.DEFAULT).message | ||
| 43 | # to create an EU bucket | ||
| 44 | #print conn.create_located_bucket(BUCKET_NAME, S3.Location.EU).message | ||
| 45 | |||
| 46 | print '----- bucket location -----' | ||
| 47 | print conn.get_bucket_location(BUCKET_NAME).location | ||
| 48 | |||
| 49 | print '----- listing bucket -----' | ||
| 50 | print map(lambda x: x.key, conn.list_bucket(BUCKET_NAME).entries) | ||
| 51 | |||
| 52 | print '----- putting object (with content type) -----' | ||
| 53 | print conn.put( | ||
| 54 | BUCKET_NAME, | ||
| 55 | KEY_NAME, | ||
| 56 | S3.S3Object('this is a test'), | ||
| 57 | { 'Content-Type': 'text/plain' }).message | ||
| 58 | |||
| 59 | print '----- listing bucket -----' | ||
| 60 | print map(lambda x: x.key, conn.list_bucket(BUCKET_NAME).entries) | ||
| 61 | |||
| 62 | print '----- getting object -----' | ||
| 63 | print conn.get(BUCKET_NAME, KEY_NAME).object.data | ||
| 64 | |||
| 65 | print '----- query string auth example -----' | ||
| 66 | print "\nTry this url out in your browser (it will only be valid for 60 seconds).\n" | ||
| 67 | generator.set_expires_in(60); | ||
| 68 | url = generator.get(BUCKET_NAME, KEY_NAME) | ||
| 69 | print url | ||
| 70 | print '\npress enter> ', | ||
| 71 | sys.stdin.readline() | ||
| 72 | |||
| 73 | print "\nNow try just the url without the query string arguments. it should fail.\n" | ||
| 74 | print generator.make_bare_url(BUCKET_NAME, KEY_NAME) | ||
| 75 | print '\npress enter> ', | ||
| 76 | sys.stdin.readline() | ||
| 77 | |||
| 78 | |||
| 79 | print '----- putting object with metadata and public read acl -----' | ||
| 80 | print conn.put( | ||
| 81 | BUCKET_NAME, | ||
| 82 | KEY_NAME + '-public', | ||
| 83 | S3.S3Object('this is a publicly readable test'), | ||
| 84 | { 'x-amz-acl': 'public-read' , 'Content-Type': 'text/plain' } | ||
| 85 | ).message | ||
| 86 | |||
| 87 | print '----- anonymous read test ----' | ||
| 88 | print "\nYou should be able to try this in your browser\n" | ||
| 89 | public_key = KEY_NAME + '-public' | ||
| 90 | print generator.make_bare_url(BUCKET_NAME, public_key) | ||
| 91 | print "\npress enter> ", | ||
| 92 | sys.stdin.readline() | ||
| 93 | |||
| 94 | print "----- getting object's acl -----" | ||
| 95 | print conn.get_acl(BUCKET_NAME, KEY_NAME).object.data | ||
| 96 | |||
| 97 | print "\n----- path style url example -----"; | ||
| 98 | print "Non-location-constrained buckets can also be specified as part of the url path. (This was the original url style supported by S3.)\n"; | ||
| 99 | print "Try this url out in your browser (it will only be valid for 60 seconds).\n" | ||
| 100 | generator.calling_format = S3.CallingFormat.PATH | ||
| 101 | url = generator.get(BUCKET_NAME, KEY_NAME) | ||
| 102 | print url | ||
| 103 | print "\npress enter> ", | ||
| 104 | sys.stdin.readline() | ||
| 105 | |||
| 106 | print '----- deleting objects -----' | ||
| 107 | print conn.delete(BUCKET_NAME, KEY_NAME).message | ||
| 108 | print conn.delete(BUCKET_NAME, KEY_NAME + '-public').message | ||
| 109 | |||
| 110 | print '----- listing bucket -----' | ||
| 111 | print map(lambda x: x.key, conn.list_bucket(BUCKET_NAME).entries) | ||
| 112 | |||
| 113 | print '----- listing all my buckets -----' | ||
| 114 | print map(lambda x: x.name, conn.list_all_my_buckets().entries) | ||
| 115 | |||
| 116 | print '----- deleting bucket ------' | ||
| 117 | print conn.delete_bucket(BUCKET_NAME).message | ||
| 118 | |||