README.rst 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. Django S3 Sync
  2. ==============
  3. Keep your static and user-uploaded media in sync between your local machine and Amazon S3 buckets. Features:
  4. * Use separate buckets for user-uploaded media VS static media
  5. * Run a management command periodically to keep your user-uploaded media in sync.
  6. * Fall back to local ``MEDIA_URL`` links for files that have not yet been uploaded to S3 (uses any cache backend to store this list).
  7. * Automatically link to files on S3 when they have been uploaded.
  8. * Easy to set up cron jobs, management commands to keep your media in sync
  9. * Efficient sync of pending uploaded and deleted files
  10. * Optionally deletes files from S3 once they have been deleted locally.
  11. This project is inspired by `django-extensions's sync_media_s3 <https://github.com/django-extensions/django-extensions/blob/master/django_extensions/management/commands/sync_media_s3.py>`_ management command.
  12. Limitations
  13. -----------
  14. * This app only works with Django's ``FileSystemStorage`` backend. You are welcome to file a pull request to fix this limitation, feel free to ask me for help :)
  15. * Using symlinks (``ln -s``) in your static media has not been tested, and is assumed not to work.
  16. Installation
  17. ------------
  18. #. Step 1::
  19. pip install -e git://github.com/pcraciunoiu/django-s3sync#egg=django-s3sync
  20. #. Add ``s3sync`` to your installed apps::
  21. INSTALLED_APPS = (
  22. # ...
  23. 's3sync',
  24. # ...
  25. )
  26. #. Set s3sync's ``S3PendingStorage`` backend as the default::
  27. DEFAULT_FILE_STORAGE = 's3sync.storage.S3PendingStorage'
  28. #. Provide these settings, all required::
  29. MEDIA_ROOT = '/full/path/to/your/media'
  30. AWS_ACCESS_KEY_ID = 'your-amazon-access-key-id'
  31. AWS_SECRET_ACCESS_KEY = 'your-amazon-secret-access-key'
  32. BUCKET_UPLOADS = 's3-2.sowink.net'
  33. BUCKET_UPLOADS_URL = '//s3-2.sowink.net/media/'
  34. # For your production site, link to the S3 uploads bucket.
  35. # This setting is optional for development.
  36. PRODUCTION = True
  37. #. Run this on a cron::
  38. # Be sure to use your media prefix here.
  39. # --remove-missing ensures deleting files locally propagates to S3
  40. python manage.py s3sync_pending --prefix=media --remove-missing
  41. #. To sync your static media, see `cron.py <https://github.com/pcraciunoiu/django-s3sync/tree/master/example/cron.py>`_
  42. Already using a custom File storage backend?
  43. --------------------------------------------
  44. If you're already using your own File storage backend, extend s3sync's storage::
  45. from s3sync.storage import S3PendingStorage
  46. class YourCustomStorage(S3PendingStorage):
  47. # Override with your storage methods here.
  48. pass
  49. Tips and Tricks
  50. ---------------
  51. Alias your bucket URL
  52. ~~~~~~~~~~~~~~~~~~~~~
  53. Make your bucket URL nice and clean and hide the URL to amazonaws.com.
  54. * Make sure to name your bucket ``something.yourdomain.com``
  55. * Using your DNS service (e.g. Route 53), create a CNAME record named ``something.yourdomain.com`` with a value of (pointing to) the `website endpoint <http://docs.amazonwebservices.com/AmazonS3/latest/dev/WebsiteEndpoints.html>`_ for your bucket's region, e.g. ``s3-website-us-west-1.amazonaws.com``
  56. Multiple web servers?
  57. ~~~~~~~~~~~~~~~~~~~~~
  58. If you need to access the files from multiple web servers before they get uploadd to S3, you can use a dedicated EC2 instance or 3rd party server to mount as a partition on all of your machines. Going the EC2 instance route is probably your best bet to minimize latency.
  59. Usage
  60. -----
  61. python manage.py s3sync_media
  62. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  63. Command options are::
  64. -b BUCKET, --bucket=BUCKET
  65. The name of the Amazon bucket you are uploading to.
  66. -p PREFIX, --prefix=PREFIX
  67. The prefix to prepend to the path on S3.
  68. -d DIRECTORY, --dir=DIRECTORY
  69. The root directory to use instead of your MEDIA_ROOT
  70. --gzip Enables gzipping CSS and Javascript files.
  71. --expires Enables setting a far future expires header.
  72. --force Skip the file mtime check to force upload of all
  73. files.
  74. --remove-missing
  75. Remove any existing keys from the bucket that are not
  76. present in your local. DANGEROUS!
  77. --exclude-list Override default directory and file exclusion
  78. filters. (enter as comma separated line)
  79. --dry-run
  80. Do A dry-run to show what files would be affected.
  81. python manage.py s3sync_pending
  82. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  83. Required settings: ``BUCKET_UPLOADS``
  84. Command options are::
  85. -p PREFIX, --prefix=PREFIX
  86. The prefix to prepend to the path on S3.
  87. -d DIRECTORY, --dir=DIRECTORY
  88. The root directory to use instead of your MEDIA_ROOT
  89. --remove-missing
  90. Remove any existing keys from the bucket that are not
  91. present in your local. DANGEROUS!
  92. --dry-run
  93. Do a dry-run to show what files would be affected.
  94. s3sync.storage.S3PendingStorage
  95. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  96. Required settings: ``BUCKET_UPLOADS_URL``, ``PRODUCTION``
  97. Full List of Settings
  98. ~~~~~~~~~~~~~~~~~~~~~
  99. ``AWS_ACCESS_KEY_ID``, ``AWS_SECRET_ACCESS_KEY``
  100. *Required.* Your API keys from Amazon.
  101. ``BUCKET_UPLOADS``
  102. Name of your upload bucket. Usually 'something.yourdomain.com'
  103. ``BUCKET_UPLOADS_URL``
  104. URL to your bucket, including the prefix.
  105. ``BUCKET_UPLOADS_CACHE_ALIAS``
  106. Which cache backend to use from `settings.CACHES <https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-CACHES>`_
  107. ``BUCKET_UPLOADS_PENDING_KEY``
  108. Cache key to use for storing the list of pending files to be uploaded to S3.
  109. ``BUCKET_UPLOADS_PENDING_DELETE_KEY``
  110. Cache key to use for storing the list of pending files to be removed from S3.
  111. ``PRODUCTION``
  112. Set this to True for the storage backend to use ``BUCKET_UPLOADS_URL``.
  113. Contributing
  114. ============
  115. If you'd like to fix a bug, add a feature, etc
  116. #. Start by opening an issue.
  117. Be explicit so that project collaborators can understand and reproduce the
  118. issue, or decide whether the feature falls within the project's goals.
  119. Code examples can be useful, too.
  120. #. File a pull request.
  121. You may write a prototype or suggested fix.
  122. #. Check your code for errors, complaints.
  123. Use `check.py <https://github.com/jbalogh/check>`_
  124. #. Write and run tests.
  125. Write your own test showing the issue has been resolved, or the feature
  126. works as intended.
  127. Running Tests
  128. =============
  129. *TODO*: write tests.
  130. To run the tests::
  131. python manage.py test s3sync