Skip to content

Commit 3e99126

Browse files
authored
Docs for a new release (#38)
* docs * fix for upload args * test fix * gitignore
1 parent 25d9c70 commit 3e99126

File tree

5 files changed

+67
-21
lines changed

5 files changed

+67
-21
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,5 @@ ENV/
101101

102102
# mypy
103103
.mypy_cache/
104+
105+
.vscode

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

7+
## [0.1.9] - 2018-06-24
8+
9+
### Added
10+
11+
- upload_args and download_args to constructor (Geoff Jukes)
12+
713
## [0.1.8] - 2018-03-29
814

915
### Changed

fs_s3fs/_s3fs.py

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,13 @@ class S3FS(FS):
221221
PyFilesystem specification exactly. Set to ``False`` to disable
222222
validation of destination paths which may speed up uploads /
223223
downloads.
224+
:param str cache_control: Sets the 'Cache-Control' header for uploads.
225+
:param str acl: Sets the Access Control List header for uploads.
226+
:param dict upload_args: A dictionary for additional upload arguments.
227+
See https://boto3.readthedocs.io/en/latest/reference/services/s3.html#S3.Object.put
228+
for details.
229+
:param dict download_args: Dictionary of extra arguments passed to
230+
the S3 client.
224231
225232
"""
226233

@@ -354,12 +361,16 @@ def _get_object(self, path, key):
354361
else:
355362
return obj
356363

357-
def _upload_args(self, key):
358-
if 'ContentType' not in self.upload_args:
359-
mimetype = mimetypes.guess_type(key)[0] or 'binary/octet-stream'
360-
return dict(ContentType=mimetype, **self.upload_args)
361-
else:
362-
return self.upload_args
364+
def _get_upload_args(self, key):
365+
upload_args = (
366+
self.upload_args.copy() if self.upload_args else {}
367+
)
368+
if 'ContentType' not in upload_args:
369+
mime_type, _encoding = mimetypes.guess_type(key)
370+
if six.PY2 and mime_type is not None:
371+
mime_type = mime_type.decode('utf-8', 'replace')
372+
upload_args['ContentType'] = mime_type or 'binary/octet-stream'
373+
return upload_args
363374

364375
@property
365376
def s3(self):
@@ -543,7 +554,8 @@ def makedir(self, path, permissions=None, recreate=False):
543554
else:
544555
raise errors.DirectoryExists(path)
545556
with s3errors(path):
546-
self.s3.Object(self._bucket_name, _key).put(**self._upload_args(_key))
557+
_obj = self.s3.Object(self._bucket_name, _key)
558+
_obj.put(**self._get_upload_args(_key))
547559
return SubFS(self, path)
548560

549561
def openbin(self, path, mode="r", buffering=-1, **options):
@@ -561,7 +573,10 @@ def on_close_create(s3file):
561573
s3file.raw.seek(0)
562574
with s3errors(path):
563575
self.client.upload_fileobj(
564-
s3file.raw, self._bucket_name, _key, ExtraArgs=self._upload_args(_key)
576+
s3file.raw,
577+
self._bucket_name,
578+
_key,
579+
ExtraArgs=self._get_upload_args(_key)
565580
)
566581
finally:
567582
s3file.raw.close()
@@ -589,7 +604,10 @@ def on_close_create(s3file):
589604
try:
590605
with s3errors(path):
591606
self.client.download_fileobj(
592-
self._bucket_name, _key, s3file.raw, ExtraArgs=self.download_args
607+
self._bucket_name,
608+
_key,
609+
s3file.raw,
610+
ExtraArgs=self.download_args
593611
)
594612
except errors.ResourceNotFound:
595613
pass
@@ -610,15 +628,21 @@ def on_close(s3file):
610628
s3file.raw.seek(0, os.SEEK_SET)
611629
with s3errors(path):
612630
self.client.upload_fileobj(
613-
s3file.raw, self._bucket_name, _key, ExtraArgs=self._upload_args(_key)
631+
s3file.raw,
632+
self._bucket_name,
633+
_key,
634+
ExtraArgs=self._get_upload_args(_key)
614635
)
615636
finally:
616637
s3file.raw.close()
617638

618639
s3file = S3File.factory(path, _mode, on_close=on_close)
619640
with s3errors(path):
620641
self.client.download_fileobj(
621-
self._bucket_name, _key, s3file.raw, ExtraArgs=self.download_args
642+
self._bucket_name,
643+
_key,
644+
s3file.raw,
645+
ExtraArgs=self.download_args
622646
)
623647
s3file.seek(0, os.SEEK_SET)
624648
return s3file
@@ -681,7 +705,10 @@ def getbytes(self, path):
681705
bytes_file = io.BytesIO()
682706
with s3errors(path):
683707
self.client.download_fileobj(
684-
self._bucket_name, _key, bytes_file, ExtraArgs=self.download_args
708+
self._bucket_name,
709+
_key,
710+
bytes_file,
711+
ExtraArgs=self.download_args
685712
)
686713
return bytes_file.getvalue()
687714

@@ -695,7 +722,10 @@ def getfile(self, path, file, chunk_size=None, **options):
695722
_key = self._path_to_key(_path)
696723
with s3errors(path):
697724
self.client.download_fileobj(
698-
self._bucket_name, _key, file, ExtraArgs=self.download_args
725+
self._bucket_name,
726+
_key,
727+
file,
728+
ExtraArgs=self.download_args
699729
)
700730

701731
def exists(self, path):
@@ -779,7 +809,10 @@ def setbytes(self, path, contents):
779809
bytes_file = io.BytesIO(contents)
780810
with s3errors(path):
781811
self.client.upload_fileobj(
782-
bytes_file, self._bucket_name, _key, ExtraArgs=self._upload_args(_key)
812+
bytes_file,
813+
self._bucket_name,
814+
_key,
815+
ExtraArgs=self._get_upload_args(_key)
783816
)
784817

785818
def setbinfile(self, path, file):
@@ -797,7 +830,12 @@ def setbinfile(self, path, file):
797830
pass
798831

799832
with s3errors(path):
800-
self.client.upload_fileobj(file, self._bucket_name, _key, self._upload_args(_key))
833+
self.client.upload_fileobj(
834+
file,
835+
self._bucket_name,
836+
_key,
837+
ExtraArgs=self._get_upload_args(_key)
838+
)
801839

802840
def copy(self, src_path, dst_path, overwrite=False):
803841
if not overwrite and self.exists(dst_path):

fs_s3fs/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.1.8"
1+
__version__ = "0.1.9a0"

fs_s3fs/tests/test_s3fs.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ def test_path_to_key_subdir(self):
7171

7272
def test_upload_args(self):
7373
s3 = S3FS('foo', acl='acl', cache_control='cc')
74-
self.assertDictEqual(s3._upload_args('test.jpg'),
74+
self.assertDictEqual(s3._get_upload_args('test.jpg'),
7575
{'ACL': 'acl', 'CacheControl': 'cc', 'ContentType': 'image/jpeg'})
76-
self.assertDictEqual(s3._upload_args('test.mp3'),
76+
self.assertDictEqual(s3._get_upload_args('test.mp3'),
7777
{'ACL': 'acl', 'CacheControl': 'cc', 'ContentType': 'audio/mpeg'})
78-
self.assertDictEqual(s3._upload_args('test.json'),
78+
self.assertDictEqual(s3._get_upload_args('test.json'),
7979
{'ACL': 'acl', 'CacheControl': 'cc', 'ContentType': 'application/json'})
80-
self.assertDictEqual(s3._upload_args('unknown.ext'),
81-
{'ACL': 'acl', 'CacheControl': 'cc', 'ContentType': 'binary/octet-stream'})
80+
self.assertDictEqual(s3._get_upload_args('unknown.unknown'),
81+
{'ACL': 'acl', 'CacheControl': 'cc', 'ContentType': 'binary/octet-stream'})

0 commit comments

Comments
 (0)