basic example

0 views
Skip to first unread message

.M.

unread,
Dec 6, 2006, 1:38:20 AM12/6/06
to Python Amazon
Hi there

I'm really keen to try this module out but a bit stumpted as to the
basic syntax. Can you clarify how to do use this in relation to the
Amazone S3 get started guide? Using Getting Started Guide (API Version
2006-03-01)

e.g.

<code>
# all future examples assume this:
conn = S3.AWSAuthConnection("[aws-access-key-id]",
"[aws-secret-access-key]")

response = conn.create_bucket("[bucket-name]")
if response.http_response.status == 200:
# bucket was created
else:
# something bad happened
</code>

I tried

<code>
>>> import s3
>>> connection = s3.S3Connection('<amazon user id>', '<secret key>')
>>> connection
<s3.connection.S3Connection object at 0xb78b3fcc>
>>> response = connection.create_bucket("mynewbucket")
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "s3/connection.py", line 128, in f
raise parseError(r.read())
s3.errors.S3Error: S3Error: NoSuchBucket - The specified bucket does
not exist
</code>

Looking at the docs that came with the module, not clear what this last
error means.

Any help appreciated.

Thanks
.M.

Viktor Kerkez

unread,
Dec 6, 2006, 9:22:34 AM12/6/06
to python...@googlegroups.com
The goal of the project was to make a Object oriented approach to the
Amazon S3 service, so it has a little bit different approach then the
one from the AmazonS3 specification.
The simplest code example is this:


$ python
>>> import s3
>>> service = s3.Service(self, pub_key, priv_key)
>>> bucket = service.create(name='MyBucket')
>>> service.list()
>>> object = s3.S3Object(key='myKey', data='my data', metadata={})
>>> bucket.save(object)
>>> bucket.list()

.M.

unread,
Dec 6, 2006, 5:41:55 PM12/6/06
to Python Amazon
Thanks Viktor. Got it working.

> $ python
> >>> import s3
> >>> service = s3.Service(self, pub_key, priv_key)

This didn't work but this did

service = s3.Service(pub_key, priv_key)

> >>> bucket = service.create(name='MyBucket')
> >>> service.list()
> >>> object = s3.S3Object(key='myKey', data='my data', metadata={})

how is metadata supposed to be expressed? Standard dictionary syntax
doesn't appear to work.

>>> object = s3.S3Object(key='testing2', data='my data', metadata={'user_id':1})
>>> bucket.save(object)Traceback (most recent call last):


File "<stdin>", line 1, in ?

File "s3/objects.py", line 161, in save
self._request('PUT', s3object.key, send_io=data, headers=headers)
File "s3/objects.py", line 66, in _request
params=params, headers=headers,
File "s3/connection.py", line 97, in f
headers = self._headers(method, path, length=length,
headers=headers)
File "s3/connection.py", line 62, in _headers
headers["Authorization"] = self._auth_header_value(method, path,
headers)
File "s3/connection.py", line 48, in _auth_header_value
auth_parts.extend([k + ":" + headers[k].strip() for k in xamzs])
AttributeError: 'int' object has no attribute 'strip'

Viktor

unread,
Dec 7, 2006, 8:05:32 PM12/7/06
to python...@googlegroups.com
Yes, my mistake, I just copy pasted the code, and the self parameter was
sufficent.

> This didn't work but this did
>
> service = s3.Service(pub_key, priv_key)


Metadata should be a dictionary of strings. So both the key and the
value must be a string.

Reply all
Reply to author
Forward
0 new messages