Browse code

object_batch_delete() gets full remote_list (consistent with _do_deletes())

Ubuntu authored on 2014/02/19 18:31:10
Showing 2 changed files
... ...
@@ -487,8 +487,8 @@ class S3(object):
487 487
         response = self.recv_file(request, stream, labels, start_position)
488 488
         return response
489 489
 
490
-    def object_batch_delete(self, uri, batch):
491
-        def compose_batch_del_xml(key_list):
490
+    def object_batch_delete(self, remote_list):
491
+        def compose_batch_del_xml(bucket, key_list):
492 492
             body = "<Delete>"
493 493
             for key in key_list:
494 494
                 uri = S3Uri(key)
... ...
@@ -496,18 +496,22 @@ class S3(object):
496 496
                     raise ValueError("Excpected URI type 's3', got '%s'" % uri.type)
497 497
                 if not uri.has_object():
498 498
                     raise ValueError("URI '%s' has no object" % key)
499
+                if uri.bucket() != bucket:
500
+                    raise ValueError("The batch should contain keys from the same bucket")
499 501
                 object = self.urlencode_string(uri.object())
500 502
                 body += "<Object><Key>%s</Key></Object>" % object
501 503
             body += "</Delete>"
502 504
             return body
503 505
 
504
-        if batch == None or len(batch) == 0:
506
+        batch = [remote_list[item]['object_uri_str'] for item in remote_list]
507
+        if len(batch) == 0:
505 508
             raise ValueError("Key list is empty")
506
-        request_body = compose_batch_del_xml(batch)
509
+        bucket = S3Uri(batch[0]).bucket()
510
+        request_body = compose_batch_del_xml(bucket, batch)
507 511
         md5_hash = md5()
508 512
         md5_hash.update(request_body)
509 513
         headers = {'content-md5': base64.b64encode(md5_hash.digest())}
510
-        request = self.create_request("BATCH_DELETE", uri = uri, extra = '?delete', headers = headers)
514
+        request = self.create_request("BATCH_DELETE", bucket = bucket, extra = '?delete', headers = headers)
511 515
         response = self.send_request(request, request_body)
512 516
         return response
513 517
 
... ...
@@ -534,11 +534,12 @@ def subcmd_object_del_uri(uri_str, recursive = None, batch_mode = False):
534 534
         return
535 535
 
536 536
     if batch_mode:
537
-        batch = [remote_list[item]['object_uri_str'] for item in remote_list]
538
-        response = s3.object_batch_delete(S3Uri(uri_str), batch)
539
-        output(u"Number of deleted keys: %d" % len(batch))
540
-        output(u"From: \t%s" % batch[0] or "None")
541
-        output(u"To: \t%s" % batch[-1] or "None")
537
+        response = s3.object_batch_delete(remote_list)
538
+        first_key = S3Uri(remote_list[remote_list.keys()[0]]['object_uri_str']) if remote_list else "None"
539
+        last_key = S3Uri(remote_list[remote_list.keys()[-1]]['object_uri_str']) if remote_list else "None"
540
+        output(u"Number of deleted keys: %d" % len(remote_list))
541
+        output(u"From: \t%s" % first_key)
542
+        output(u"To: \t%s" % last_key)
542 543
         remote_list = []
543 544
 
544 545
     for key in remote_list: