9 This creates a connection so that you can interact with the server.
11 .. code-block:: python
14 import boto.s3.connection
15 access_key = 'put your access key here!'
16 secret_key = 'put your secret key here!'
18 conn = boto.connect_s3(
19 aws_access_key_id = access_key,
20 aws_secret_access_key = secret_key,
21 host = 'objects.dreamhost.com',
22 #is_secure=False, # uncomment if you are not using ssl
23 calling_format = boto.s3.connection.OrdinaryCallingFormat(),
30 This gets a list of Buckets that you own.
31 This also prints out the bucket name and creation date of each bucket.
33 .. code-block:: python
35 for bucket in conn.get_all_buckets():
36 print "{name}\t{created}".format(
38 created = bucket.creation_date,
41 The output will look something like this::
43 mahbuckat1 2011-04-21T18:05:39.000Z
44 mahbuckat2 2011-04-21T18:05:48.000Z
45 mahbuckat3 2011-04-21T18:07:18.000Z
51 This creates a new bucket called ``my-new-bucket``
53 .. code-block:: python
55 bucket = conn.create_bucket('my-new-bucket')
58 Listing a Bucket's Content
59 --------------------------
61 This gets a list of objects in the bucket.
62 This also prints out each object's name, the file size, and last
65 .. code-block:: python
67 for key in bucket.list():
68 print "{name}\t{size}\t{modified}".format(
71 modified = key.last_modified,
74 The output will look something like this::
76 myphoto1.jpg 251262 2011-08-08T21:35:48.000Z
77 myphoto2.jpg 262518 2011-08-08T21:38:01.000Z
85 The Bucket must be empty! Otherwise it won't work!
87 .. code-block:: python
89 conn.delete_bucket(bucket.name)
92 Forced Delete for Non-empty Buckets
93 -----------------------------------
97 not available in python
103 This creates a file ``hello.txt`` with the string ``"Hello World!"``
105 .. code-block:: python
107 key = bucket.new_key('hello.txt')
108 key.set_contents_from_string('Hello World!')
111 Change an Object's ACL
112 ----------------------
114 This makes the object ``hello.txt`` to be publicly readable, and
115 ``secret_plans.txt`` to be private.
117 .. code-block:: python
119 hello_key = bucket.get_key('hello.txt')
120 hello_key.set_canned_acl('public-read')
121 plans_key = bucket.get_key('secret_plans.txt')
122 plans_key.set_canned_acl('private')
125 Download an Object (to a file)
126 ------------------------------
128 This downloads the object ``perl_poetry.pdf`` and saves it in
129 ``/home/larry/documents/``
131 .. code-block:: python
133 key = bucket.get_key('perl_poetry.pdf')
134 key.get_contents_to_filename('/home/larry/documents/perl_poetry.pdf')
140 This deletes the object ``goodbye.txt``
142 .. code-block:: python
144 bucket.delete_key('goodbye.txt')
147 Generate Object Download URLs (signed and unsigned)
148 ---------------------------------------------------
150 This generates an unsigned download URL for ``hello.txt``. This works
151 because we made ``hello.txt`` public by setting the ACL above.
152 This then generates a signed download URL for ``secret_plans.txt`` that
153 will work for 1 hour. Signed download URLs will work for the time
154 period even if the object is private (when the time period is up, the
155 URL will stop working).
157 .. code-block:: python
159 hello_key = bucket.get_key('hello.txt')
160 hello_url = hello_key.generate_url(0, query_auth=False, force_http=True)
163 plans_key = bucket.get_key('secret_plans.txt')
164 plans_url = plans_key.generate_url(3600, query_auth=True, force_http=True)
167 The output of this will look something like::
169 http://objects.dreamhost.com/my-bucket-name/hello.txt
170 http://objects.dreamhost.com/my-bucket-name/secret_plans.txt?Signature=XXXXXXXXXXXXXXXXXXXXXXXXXXX&Expires=1316027075&AWSAccessKeyId=XXXXXXXXXXXXXXXXXXX