README.rst 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. S3FS
  2. ====
  3. S3FS is a `PyFilesystem <https://www.pyfilesystem.org/>`__ interface to
  4. Amazon S3 cloud storage.
  5. As a PyFilesystem concrete class,
  6. `S3FS <http://fs-s3fs.readthedocs.io/en/latest/>`__ allows you to work
  7. with S3 in the same way as any other supported filesystem.
  8. Installing
  9. ----------
  10. You can install S3FS from pip as follows:
  11. ::
  12. pip install fs-s3fs
  13. Opening a S3FS
  14. --------------
  15. Open an S3FS by explicitly using the constructor:
  16. .. code:: python
  17. from fs_s3fs import S3FS
  18. s3fs = S3FS('mybucket')
  19. Or with a FS URL:
  20. .. code:: python
  21. from fs import open_fs
  22. s3fs = open_fs('s3://mybucket')
  23. Downloading Files
  24. -----------------
  25. To *download* files from an S3 bucket, open a file on the S3 filesystem
  26. for reading, then write the data to a file on the local filesystem.
  27. Here's an example that copies a file ``example.mov`` from S3 to your HD:
  28. .. code:: python
  29. from fs.tools import copy_file_data
  30. with s3fs.open('example.mov', 'rb') as remote_file:
  31. with open('example.mov', 'wb') as local_file:
  32. copy_file_data(remote_file, local_file)
  33. Although it is preferable to use the higher-level functionality in the
  34. ``fs.copy`` module. Here's an example:
  35. .. code:: python
  36. from fs.copy import copy_file
  37. copy_file(s3fs, 'example.mov', './', 'example.mov')
  38. Uploading Files
  39. ---------------
  40. You can *upload* files in the same way. Simply copy a file from a source
  41. filesystem to the S3 filesystem. See `Moving and
  42. Copying <https://docs.pyfilesystem.org/en/latest/guide.html#moving-and-copying>`__
  43. for more information.
  44. ExtraArgs
  45. ---------
  46. S3 objects have additional properties, beyond a traditional filesystem.
  47. These options can be set using the ``upload_args`` and ``download_args``
  48. properties. which are handed to upload and download methods, as
  49. appropriate, for the lifetime of the filesystem instance.
  50. For example, to set the ``cache-control`` header of all objects uploaded
  51. to a bucket:
  52. .. code:: python
  53. import fs, fs.mirror
  54. s3fs = S3FS('example', upload_args={"CacheControl": "max-age=2592000", "ACL": "public-read"})
  55. fs.mirror.mirror('/path/to/mirror', s3fs)
  56. see `the Boto3
  57. docs <https://boto3.readthedocs.io/en/latest/reference/customizations/s3.html#boto3.s3.transfer.S3Transfer.ALLOWED_UPLOAD_ARGS>`__
  58. for more information.
  59. ``acl`` and ``cache_control`` are exposed explicitly for convenience,
  60. and can be used in URLs. It is important to URL-Escape the
  61. ``cache_control`` value in a URL, as it may contain special characters.
  62. .. code:: python
  63. import fs, fs.mirror
  64. with open fs.open_fs('s3://example?acl=public-read&cache_control=max-age%3D2592000%2Cpublic') as s3fs
  65. fs.mirror.mirror('/path/to/mirror', s3fs)
  66. S3 URLs
  67. -------
  68. You can get a public URL to a file on a S3 bucket as follows:
  69. .. code:: python
  70. movie_url = s3fs.geturl('example.mov')
  71. Documentation
  72. -------------
  73. - `PyFilesystem Wiki <https://www.pyfilesystem.org>`__
  74. - `S3FS Reference <http://fs-s3fs.readthedocs.io/en/latest/>`__
  75. - `PyFilesystem
  76. Reference <https://docs.pyfilesystem.org/en/latest/reference/base.html>`__