Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions gcloud/storage/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,17 +180,24 @@ def delete_key(self, key):
self.connection.api_request(method='DELETE', path=key.path)
return key

def delete_keys(self, keys):
def delete_keys(self, keys, on_error=None):
"""Deletes a list of keys from the current bucket.

Uses :func:`Bucket.delete_key` to delete each individual key.

:type keys: list of string or :class:`gcloud.storage.key.Key`
:param key: A list of key names or Key objects to delete.
:param keys: A list of key names or Key objects to delete.

:type on_error: a callable taking (key)
:param on_error: If not ``None``, called once for each key which
raises a ``NotFoundError``.
"""
# NOTE: boto returns a MultiDeleteResult instance.
for key in keys:
self.delete_key(key)
try:

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

self.delete_key(key)
except exceptions.NotFoundError:
if on_error is not None:
on_error(key)

This comment was marked as spam.

This comment was marked as spam.


def copy_key(self, key, destination_bucket, new_name=None):
"""Copy the given key to the given bucket, optionally with a new name.
Expand Down
19 changes: 17 additions & 2 deletions gcloud/storage/test_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,13 +233,28 @@ def test_delete_keys_hit(self):
self.assertEqual(kw[0]['path'], '/b/%s/o/%s' % (NAME, KEY))

def test_delete_keys_miss(self):
from gcloud.storage.exceptions import NotFoundError
NAME = 'name'
KEY = 'key'
NONESUCH = 'nonesuch'
connection = _Connection({})
bucket = self._makeOne(connection, NAME)
self.assertRaises(NotFoundError, bucket.delete_keys, [KEY, NONESUCH])
bucket.delete_keys([KEY, NONESUCH])
kw = connection._requested
self.assertEqual(len(kw), 2)
self.assertEqual(kw[0]['method'], 'DELETE')
self.assertEqual(kw[0]['path'], '/b/%s/o/%s' % (NAME, KEY))
self.assertEqual(kw[1]['method'], 'DELETE')
self.assertEqual(kw[1]['path'], '/b/%s/o/%s' % (NAME, NONESUCH))

def test_delete_keys_miss_w_on_error(self):
NAME = 'name'
KEY = 'key'
NONESUCH = 'nonesuch'
connection = _Connection({})
bucket = self._makeOne(connection, NAME)
errors = []
bucket.delete_keys([KEY, NONESUCH], errors.append)
self.assertEqual(errors, [NONESUCH])
kw = connection._requested
self.assertEqual(len(kw), 2)
self.assertEqual(kw[0]['method'], 'DELETE')
Expand Down