Package s3 :: Module service
[hide private]
[frames] | no frames]

Source Code for Module s3.service

  1  from s3.connection import S3Connection 
  2  from s3.objects import S3Bucket 
  3  from s3.parsers import parseListBuckets, parseGetBucket, parseGetBucketNames 
  4   
5 -class S3Service(object):
6 """ 7 S3 Service class. 8 9 Behaves like a dictionary of buckets, with some additional functions. 10 """ 11
12 - def __init__(self, pub_key, priv_key):
13 self._s3_conn = S3Connection(pub_key, priv_key)
14
15 - def get(self, name, default=None):
16 """ 17 Get bucket with the exact name. 18 19 @param name: The name of the bucket 20 @type name: string 21 @return: Bucket if exists, else None 22 @rtype: S3Bucket or None 23 """ 24 response = self._s3_conn.clone().get() 25 return parseGetBucket(name, response.read(), self._s3_conn, default)
26
27 - def list(self):
28 """ 29 List all buckets. 30 31 @return: List of buckets associated with the authenticated user 32 @rtype: list 33 """ 34 response = self._s3_conn.clone().get() 35 return parseListBuckets(response.read(), self._s3_conn)
36
37 - def create(self, name):
38 """ 39 Create a bucket. 40 41 @param name: Name for the new bucket 42 @type name: string 43 @return: Returns the newly created bucket 44 @rtype: S3Bucket 45 """ 46 self._s3_conn.clone().put(name) 47 return S3Bucket(name, self._s3_conn)
48 49
50 - def delete(self, name):
51 """ 52 Deletes a bucket. 53 54 @param name: Name of the queue that should be deleted 55 @type name: string 56 """ 57 self._s3_conn.clone().delete(name)
58 59
60 - def keys(self):
61 """ 62 Returns a flat list of bucket names. 63 64 @return: List of bucket names 65 @rtype: list 66 """ 67 response = self._s3_conn.clone().get() 68 return parseGetBucketNames(response.read())
69 70 values = list 71
72 - def items(self):
73 """ 74 Returns (key, value) pairs, where, keys are bucket names, and values are S3Buckets. 75 76 @return: List of (bucket name, bucket) pairs 77 @rtype: list 78 """ 79 return zip(self.keys(), self.list())
80 81
82 - def has_key(self, key):
83 """ 84 Does bucket exist. 85 """ 86 return key in self.keys()
87 88
89 - def __getitem__(self, key):
90 """ 91 Get a bucket instance. 92 """ 93 return self.get(key)
94 95
96 - def __delitem__(self, key):
97 """ 98 Deletes a bucket 99 """ 100 self.delete(key)
101