Widget for uploading files from the client directly to a cloud storage bucket.
Project description
django-direct-cloud-upload (DDCU)
Widget for uploading files from the client directly to a cloud storage bucket. Currently only supports Google Cloud Storage.
Works with Internet Explorer 11, Google Chrome, Mozilla Firefox and Apple Safari.
Installation and setup
Install DDCU using pip :
pip install django-direct-cloud-upload
Make sure that 'django.contrib.staticfiles'
is set up properly and add 'direct_cloud_upload'
to your INSTALLED_APPS
setting :
INSTALLED_APPS = [
# ...
'django.contrib.staticfiles',
# ...
'direct_cloud_upload',
]
STATIC_URL = '/static/'
DDCU provides a view that will generate a 'signed URL' for uploading a file. To enable this view, include DDCU's urlpatterns
to your project's URLconf :
import direct_cloud_upload
urlpatterns = [
# ...
path('direct_cloud_upload/', include(direct_cloud_upload.urlpatterns)),
]
Note: It is not mandatory to choose direct_cloud_upload/
as the path, you are free to set any path.
Configuring the GCS bucket
If you don't have a GCS bucket yet, create one first.
Configure CORS on your bucket to allow uploads from your website. Configuring CORS for GCS buckets is done by creating a JSON-file describing the CORS policy and uploading the JSON file with gsutil
.
Example JSON file containing a CORS policy:
[
{
"origin": [
"https://example-app.com",
"https://www.example-app.com",
"http://localhost:8000"
],
"responseHeader": ["Content-Type"],
"method": ["GET", "HEAD", "PUT"],
"maxAgeSeconds": 3600
}
]
Upload the JSON file with the gsutil cors set
command:
gsutil cors set cors-json-file.json gs://example-bucket
Usage
Create a Bucket
instance first, and register it with DDCU :
import google.cloud.storage
import direct_cloud_upload
client = google.cloud.storage.Client()
gcs_bucket = client.get_bucket('bucket-id-here')
ddcu_bucket_identifier = direct_cloud_upload.register_gcs_bucket(gcs_bucket)
Creating the Client
can be a bit more complicated if you need to authenticate, see the GCS Python client documentation.
Now you can use the CloudFileWidget
for any django.forms.CharField
in a Form :
from direct_cloud_upload import CloudFileWidget
class EbookForm(forms.ModelForm):
class Meta:
model = Ebook
fields = ['title', 'pdf_file']
widgets = {
'pdf_file': CloudFileWidget(
bucket_identifier = ddcu_bucket_identifier,
path_prefix = "ebook_pdf/",
)
}
CloudFileWidget
has one required:
bucket_identifier
(string): identifier retrieved when registering the bucket with DDCU.
CloudFileWidget
has some optional parameters:
path_prefix
(string): Will be prepended automatically to the path of each file. This is useful for collecting all files uploaded via this widget into one directory.include_timestamp
(bool) : Determines if a timestamp will be added to the path. Defaults toTrue
.submit_timeout
(int) : Timeout (in seconds) for uploading the form. Defaults to 129600 (36 hours).
When the form is being submitted, the field will contain the path in the bucket where the file has been uploaded to.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Hashes for django-direct-cloud-upload-0.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | fcb6fba7197fc464abfdeb733a4de0b908e790edd74e917599032ed8c3676ece |
|
MD5 | d1e19b4dadc7d9897613970b13622359 |
|
BLAKE2b-256 | dc9fc6ea32b936e1a3dd88a42a15603ff691f296c2d5a2291babc138bee49cbd |