Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise ValueError if s3 key does not exist #245

Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion smart_open/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ class SeekableRawReader(object):

def __init__(self, s3_object):
self._object = s3_object
self._content_length = self._object.content_length
try:
self._content_length = self._object.content_length
except botocore.client.ClientError:
raise ValueError('the s3 key %r does not exist, or is forbidden for access' % s3_object.key)
self.seek(0)

def seek(self, position):
Expand Down
5 changes: 5 additions & 0 deletions smart_open/tests/test_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,11 @@ def test_nonexisting_bucket(self):
with smart_open.s3.open('thisbucketdoesntexist', 'mykey', 'wb') as fout:
fout.write(expected)

def test_read_nonexisting_key(self):
with self.assertRaises(ValueError):
with smart_open.s3.open(BUCKET_NAME, 'my_nonexisting_key', 'rb') as fin:
fin.read()

def test_double_close(self):
create_bucket_and_key()

Expand Down