diff 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
line wrap: on
line diff
--- a/core/s3.py	Wed Sep 18 18:33:52 2013 -0500
+++ b/core/s3.py	Wed Sep 18 21:34:05 2013 -0500
@@ -10,8 +10,6 @@
 class S3Bucket(object):
     """This class abstracts an Amazon S3 bucket.
 
-    We currently only support upload functionality.
-
     """
     def __init__(self, access_key, secret_key, base_url, bucket_name):
         self.conn = S3Connection(access_key, secret_key)
@@ -71,6 +69,28 @@
             key.make_public()
         return '{}{}/{}'.format(self.base_url, self.name, key_name)
 
+    def delete_keys(self, key_urls):
+        """Deletes a set of keys, specified as a list of URLs. The URLs could
+        have been returned by one or more of the upload_* methods.
+
+        Returns the number of keys that were successfully deleted.
+
+        """
+        if len(key_urls) == 0:
+            return 0
+
+        prefix = '{}{}/'.format(self.base_url, self.name)
+        prefix_len = len(prefix)
+
+        keys = []
+        for url in key_urls:
+            if url.startswith(prefix):
+                key = url[prefix_len:]
+                keys.append(key)
+
+        response = self.bucket.delete_keys(keys, quiet=True)
+        return len(key_urls) - len(response.errors)
+
     def _make_key(self, key_name, metadata):
         """Private method to create a key and optionally apply metadata to
         it.