comparison core/s3.py @ 718:bf5340705d0c

Completed view to delete user photos. Still need to modify the admin to delete not just the model instance but the S3 bucket keys.
author Brian Neal <bgneal@gmail.com>
date Wed, 18 Sep 2013 21:34:05 -0500
parents e888d627928f
children e4f2d6a4b401
comparison
equal deleted inserted replaced
717:846cf9a06a04 718:bf5340705d0c
7 from boto.s3.key import Key 7 from boto.s3.key import Key
8 8
9 9
10 class S3Bucket(object): 10 class S3Bucket(object):
11 """This class abstracts an Amazon S3 bucket. 11 """This class abstracts an Amazon S3 bucket.
12
13 We currently only support upload functionality.
14 12
15 """ 13 """
16 def __init__(self, access_key, secret_key, base_url, bucket_name): 14 def __init__(self, access_key, secret_key, base_url, bucket_name):
17 self.conn = S3Connection(access_key, secret_key) 15 self.conn = S3Connection(access_key, secret_key)
18 self.bucket = self.conn.get_bucket(bucket_name, validate=False) 16 self.bucket = self.conn.get_bucket(bucket_name, validate=False)
69 key.set_contents_from_string(content) 67 key.set_contents_from_string(content)
70 if public: 68 if public:
71 key.make_public() 69 key.make_public()
72 return '{}{}/{}'.format(self.base_url, self.name, key_name) 70 return '{}{}/{}'.format(self.base_url, self.name, key_name)
73 71
72 def delete_keys(self, key_urls):
73 """Deletes a set of keys, specified as a list of URLs. The URLs could
74 have been returned by one or more of the upload_* methods.
75
76 Returns the number of keys that were successfully deleted.
77
78 """
79 if len(key_urls) == 0:
80 return 0
81
82 prefix = '{}{}/'.format(self.base_url, self.name)
83 prefix_len = len(prefix)
84
85 keys = []
86 for url in key_urls:
87 if url.startswith(prefix):
88 key = url[prefix_len:]
89 keys.append(key)
90
91 response = self.bucket.delete_keys(keys, quiet=True)
92 return len(key_urls) - len(response.errors)
93
74 def _make_key(self, key_name, metadata): 94 def _make_key(self, key_name, metadata):
75 """Private method to create a key and optionally apply metadata to 95 """Private method to create a key and optionally apply metadata to
76 it. 96 it.
77 97
78 """ 98 """