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 'ddcu'
to your INSTALLED_APPS
setting :
INSTALLED_APPS = [
# ...
'django.contrib.staticfiles',
# ...
'ddcu',
]
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 ddcu
urlpatterns = [
# ...
path('ddcu/', include(ddcu.urlpatterns)),
]
Note: It is not mandatory to choose ddcu/
as the path, you are free to set any path.
Usage
Create a Bucket
first :
from google.cloud import storage
client = storage.Client()
gcs_bucket = client.get_bucket('bucket-id-here')
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 ddcu import CloudFileWidget
class EbookForm(forms.ModelForm):
class Meta:
model = Ebook
fields = ['title', 'pdf_file']
widgets = {
'pdf_file': CloudFileWidget(
widget_identifier = "ebook_pdf_file",
bucket = gcs_bucket,
path_prefix = "ebook_pdf/",
)
}
CloudFileWidget
has required parameters:
widget_identifier
(string): identifier for the widget, should be unique in your project. Only use uppercase and lowercase characters, numbers, underscores and hyphens.bucket
: GCSBucket
object where the files will be uploaded.
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
.object_identifier
(str) : Identifier (not necessarily unique) that will be added to the path.timeout
(int) : Timeout (in seconds) for the signed URL. Defaults to 3600.authorize_func
: Function that should raise addcu.AuthenticationError
if the user is not allowed to upload a file using this widget. The function will receive three parameters: The request object, the Content Type of the file being uploaded and the object identifier (if set).allow_anonymous
(bool) : Allow users that aren't logged in to upload files. Defaults toFalse
.
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.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 64699557e3afb6d415cd70e6611af3aae8d3721ce521a0693bb9a495f856dd8c |
|
MD5 | b1e5d8981e809683c2989bcfd66d4261 |
|
BLAKE2b-256 | 50afa1b4dc833f74987458b70c28cb1e3bb24616ffe7d07191b88be99bedefc0 |