aboutsummaryrefslogtreecommitdiffstats
path: root/src/amazons3/s3-driver.py
diff options
context:
space:
mode:
authorCraig Jackson2011-12-13 14:46:59 -0700
committerCraig Jackson2011-12-13 14:46:59 -0700
commit647c078f0c801eb9fa63dfc7ba3c017b30e4d467 (patch)
treea13b16dbd41bb39269c7649fcc2f2ad77137500a /src/amazons3/s3-driver.py
parentdb95070a37f70a00f80a1a2a9ba732c55f6bee1b (diff)
downloadamazons3-py-647c078f0c801eb9fa63dfc7ba3c017b30e4d467.tar.gz
amazons3-py-647c078f0c801eb9fa63dfc7ba3c017b30e4d467.zip
Made the project available in pypi. Involved moving files and
initializing a setup.py.
Diffstat (limited to 'src/amazons3/s3-driver.py')
-rw-r--r--src/amazons3/s3-driver.py118
1 files changed, 118 insertions, 0 deletions
diff --git a/src/amazons3/s3-driver.py b/src/amazons3/s3-driver.py
new file mode 100644
index 0000000..29f700b
--- /dev/null
+++ b/src/amazons3/s3-driver.py
@@ -0,0 +1,118 @@
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
12import S3
13import time
14import sys
15
16AWS_ACCESS_KEY_ID = '<INSERT YOUR AWS ACCESS KEY ID HERE>'
17AWS_SECRET_ACCESS_KEY = '<INSERT YOUR AWS SECRET ACCESS KEY HERE>'
18# remove these next two lines when you've updated your credentials.
19print "update s3-driver.py with your AWS credentials"
20sys.exit();
21
22# convert the bucket to lowercase for vanity domains
23# the bucket name must be lowercase since DNS is case-insensitive
24BUCKET_NAME = AWS_ACCESS_KEY_ID.lower() + '-test-bucket'
25KEY_NAME = 'test-key'
26
27conn = S3.AWSAuthConnection(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
28generator = 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.
38if (conn.check_bucket_exists(BUCKET_NAME).status == 200):
39 print '----- bucket already exists! -----'
40else:
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
46print '----- bucket location -----'
47print conn.get_bucket_location(BUCKET_NAME).location
48
49print '----- listing bucket -----'
50print map(lambda x: x.key, conn.list_bucket(BUCKET_NAME).entries)
51
52print '----- putting object (with content type) -----'
53print conn.put(
54 BUCKET_NAME,
55 KEY_NAME,
56 S3.S3Object('this is a test'),
57 { 'Content-Type': 'text/plain' }).message
58
59print '----- listing bucket -----'
60print map(lambda x: x.key, conn.list_bucket(BUCKET_NAME).entries)
61
62print '----- getting object -----'
63print conn.get(BUCKET_NAME, KEY_NAME).object.data
64
65print '----- query string auth example -----'
66print "\nTry this url out in your browser (it will only be valid for 60 seconds).\n"
67generator.set_expires_in(60);
68url = generator.get(BUCKET_NAME, KEY_NAME)
69print url
70print '\npress enter> ',
71sys.stdin.readline()
72
73print "\nNow try just the url without the query string arguments. it should fail.\n"
74print generator.make_bare_url(BUCKET_NAME, KEY_NAME)
75print '\npress enter> ',
76sys.stdin.readline()
77
78
79print '----- putting object with metadata and public read acl -----'
80print 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
87print '----- anonymous read test ----'
88print "\nYou should be able to try this in your browser\n"
89public_key = KEY_NAME + '-public'
90print generator.make_bare_url(BUCKET_NAME, public_key)
91print "\npress enter> ",
92sys.stdin.readline()
93
94print "----- getting object's acl -----"
95print conn.get_acl(BUCKET_NAME, KEY_NAME).object.data
96
97print "\n----- path style url example -----";
98print "Non-location-constrained buckets can also be specified as part of the url path. (This was the original url style supported by S3.)\n";
99print "Try this url out in your browser (it will only be valid for 60 seconds).\n"
100generator.calling_format = S3.CallingFormat.PATH
101url = generator.get(BUCKET_NAME, KEY_NAME)
102print url
103print "\npress enter> ",
104sys.stdin.readline()
105
106print '----- deleting objects -----'
107print conn.delete(BUCKET_NAME, KEY_NAME).message
108print conn.delete(BUCKET_NAME, KEY_NAME + '-public').message
109
110print '----- listing bucket -----'
111print map(lambda x: x.key, conn.list_bucket(BUCKET_NAME).entries)
112
113print '----- listing all my buckets -----'
114print map(lambda x: x.name, conn.list_all_my_buckets().entries)
115
116print '----- deleting bucket ------'
117print conn.delete_bucket(BUCKET_NAME).message
118