opener.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. # coding: utf-8
  2. """Defines the S3FS Opener."""
  3. from __future__ import absolute_import
  4. from __future__ import print_function
  5. from __future__ import unicode_literals
  6. __all__ = ["S3FSOpener"]
  7. from fs.opener import Opener
  8. from fs.opener.errors import OpenerError
  9. from ._s3fs import S3FS
  10. class S3FSOpener(Opener):
  11. protocols = ["s3"]
  12. def open_fs(self, fs_url, parse_result, writeable, create, cwd):
  13. bucket_name, _, dir_path = parse_result.resource.partition("/")
  14. if not bucket_name:
  15. raise OpenerError("invalid bucket name in '{}'".format(fs_url))
  16. strict = (
  17. parse_result.params["strict"] == "1"
  18. if "strict" in parse_result.params
  19. else True
  20. )
  21. s3fs = S3FS(
  22. bucket_name,
  23. dir_path=dir_path or "/",
  24. aws_access_key_id=parse_result.username or None,
  25. aws_secret_access_key=parse_result.password or None,
  26. endpoint_url=parse_result.params.get("endpoint_url", None),
  27. acl=parse_result.params.get("acl", None),
  28. cache_control=parse_result.params.get("cache_control", None),
  29. strict=strict,
  30. )
  31. return s3fs