简体   繁体   中英

How i use amazon s3 in python to use REST examples

I am reading this link http://docs.aws.amazon.com/AmazonS3/2006-03-01/API/RESTBucketGET.html

The example i want to use is this

The following GET request specifies the delimiter parameter with value "/", and the prefix parameter with value photos/2006/.

The response is

GET /?prefix=photos/2006/&delimiter=/ HTTP/1.1
Host: example-bucket.s3.amazonaws.com
Date: Wed, 01 Mar  2009 12:00:00 GMT
Authorization: AWS AKIAIOSFODNN7EXAMPLE:xQE0diMbLRepdf3YB+FIEXAMPLE=

The returned resposne is

<ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
  <Name>example-bucket</Name>
  <Prefix>photos/2006/</Prefix>
  <Marker></Marker>
  <MaxKeys>1000</MaxKeys>
  <Delimiter>/</Delimiter>
  <IsTruncated>false</IsTruncated>

  <CommonPrefixes>
    <Prefix>photos/2006/feb/</Prefix>
  </CommonPrefixes>
  <CommonPrefixes>
    <Prefix>photos/2006/jan/</Prefix>
  </CommonPrefixes>
</ListBucketResult>

I get that but i am not sure how can i implement that in django/pyhton using boto.

Also Is there any way to get JOSN instead of xml.

In boto, you could create a similar request like this:

import boto

s3 = boto.connect_s3()
bucket = s3.lookup('example-bucket')
for key in bucket.list(prefix='photos/2006/', delimiter='/'):
    print k.name

What boto does is parse the XML output from S3 and turn it into Python objects. So, listing a bucket will return a bunch of Key objects, each object representing an object in S3.

There isn't anything in boto or in S3 that would automatically turn the data into JSON but you could certainly write some Python to translate the information in the Key objects into JSON.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM