Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google app engine - limit file size upload

I would like to limit the size during a file upload according to the next requirements:

1- Client side ( this is almost impossible unless using client plugins such as Flash or Applets ) so I discard this one

2- On the server side, can I know the size of a file / image / video before storing it in the database?

Thanks!

like image 720
Mc- Avatar asked Dec 28 '22 19:12

Mc-


2 Answers

With the Blobstore service, currently there is no way to limit the file size upload (open issue here).
Storing your data using a simple BlobProperty, you could check the size of the blob after the upload with len(uploaded_blob).

EDIT:
this is now fixed

like image 101
systempuntoout Avatar answered Jan 16 '23 04:01

systempuntoout


Since release 1.5.4 create_upload_url takes additional optional arguments that can limit the upload size

create_upload_url(success_path, max_bytes_per_blob=None, max_bytes_total=None, rpc=None, gs_bucket_name=None)

As I get it max_bytes_per_blob limits the size of each file being uploaded, while max_bytes_total limits the total size of all files uploaded in one request. For details see https://developers.google.com/appengine/docs/python/blobstore/functions

For example to limit the upload size to 5MB, call it like this:

upload_url = blobstore.create_upload_url('/upload', max_bytes_total=5000000)

If the upload size is larger than the limit, HTTP status 413 (Request Entity Too Large) is returned. If you need, you can intercept it in jQuery like this:

    $("#file_form").ajaxForm({
        success : function(response) {
            ...
        },
        error : function(jqXHR, textStatus, errorThrown) {
            if (jqXHR.status == 413) {
                $("#error_message").text("Uploaded files should not be larger than 5MB");
            }
        }
    });
like image 32
Peter Dotchev Avatar answered Jan 16 '23 04:01

Peter Dotchev