Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2Amazon S3 backend storage upload via AJAX.
4Install
5-------
7The ``boto`` package is required: ``pip install boto``
9Setup
10-----
12Add the settings to your ``settings.py`` file:
14.. code-block:: python
16 AWS_ACCESS_KEY_ID = "example"
17 AWS_BUCKET_NAME = "example"
18 AWS_SECRET_KEY = "example"
20Usage
21-----
23Specify the backend for the AJAX upload:
25.. code-block:: python
27 # views.py
28 from superdjango.ajax.views import AjaxFileUploader
29 from superdjango.storage.backends.ajax.couch import S3AjaxStorage
31 ajax_uploader = AjaxFileUploader(S3AjaxStorage)
33"""
34# Imports
36from django.conf import settings
37from io import StringIO
38from multiprocessing import Pool
39from .base import BaseAjaxStorage
40from .compat import boto
42# Exports
44__all__ = (
45 "S3AjaxStorage",
46)
48# Constants
50ACCESS_KEY_ID = settings.AWS_ACCESS_KEY_ID
51BUCKET_NAME = settings.AWS_BUCKET_NAME
52SECRET_KEY = settings.AWS_SECRET_KEY
54# Classes
57class S3AjaxStorage(BaseAjaxStorage):
58 NUM_PARALLEL_PROCESSES = 4
60 def upload_chunk(self, chunk, *args, **kwargs):
61 self._counter += 1
62 buffer = StringIO()
63 buffer.write(chunk)
64 self._pool.apply_async(self._mp.upload_part_from_file(buffer, self._counter))
65 buffer.close()
67 def setup(self, filename, *args, **kwargs):
68 # noinspection PyAttributeOutsideInit
69 self._bucket = boto.connect_s3(ACCESS_KEY_ID, SECRET_KEY).lookup(BUCKET_NAME)
71 # noinspection PyAttributeOutsideInit
72 self._mp = self._bucket.initiate_multipart_upload(filename)
74 # noinspection PyAttributeOutsideInit
75 self._pool = Pool(processes=self.NUM_PARALLEL_PROCESSES)
77 # noinspection PyAttributeOutsideInit
78 self._counter = 0
80 def upload_complete(self, request, filename, *args, **kwargs):
81 # Tie up loose ends, and finish the upload
82 self._pool.close()
83 self._pool.join()
84 self._mp.complete_upload()