Skip to content

Commit

Permalink
fix s3 delete_many (#38)
Browse files Browse the repository at this point in the history
* fix s3 delete_many

Couldn't find any documentation around this issue, but I empirically discovered that `delete_objects` with an empty array causes the request to fail. Also, this is a valid optimization anyway.

* add tests and comments

* add comment

* use mock

* install mock unconditionally
  • Loading branch information
itajaja authored Jul 14, 2017
1 parent a9b400b commit b8c6aec
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
11 changes: 8 additions & 3 deletions flask_annex/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,16 @@ def delete(self, key):
self._client.delete_object(Bucket=self._bucket_name, Key=key)

def delete_many(self, keys):
# casting to tuple because keys might be iterable
objects = tuple({'Key': key} for key in keys)
if not objects:
# this is not just an optimization, boto fails if the array
# is empty
return

self._client.delete_objects(
Bucket=self._bucket_name,
Delete={
'Objects': tuple({'Key': key} for key in keys),
},
Delete={'Objects': objects},
)

def get_file(self, key, out_file):
Expand Down
9 changes: 9 additions & 0 deletions tests/test_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from io import BytesIO
import json

from mock import Mock
import pytest

from flask_annex import Annex
Expand Down Expand Up @@ -107,6 +108,14 @@ def test_get_upload_info_max_content_length(self, app, client):
def assert_app_config_content_length_range(self, conditions):
assert get_condition(conditions, 'content-length-range') == [0, 100]

def test_delete_many_empty_list(self, annex, monkeypatch):
mock = Mock()
monkeypatch.setattr(annex._client, 'delete_objects', mock)

annex.delete_many(tuple())

mock.assert_not_called()


class TestS3AnnexFromEnv(TestS3Annex):
@pytest.fixture
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ usedevelop = True

deps =
flake8
mock
pytest
pytest-cov

Expand Down

0 comments on commit b8c6aec

Please sign in to comment.