Google app engine - limit file size upload

2019-05-07 00:13发布

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!

3条回答
相关推荐>>
2楼-- · 2019-05-07 00:49

If you're using the Google Blobstore (which you probably will be on App Engine) then I think you're in a little bit of difficulty. As this post states your option is to immediately check the File Size and delete it if too large. Perhaps architecturally it is difficult for Google to offer validation on the BlobStore before the data is stored. I don't know.

Perhaps you want to star the issue on the Google App Engine Issue Tracker. It's unclear if you are using Java or Python, but if you look at the relevant Google API Docs you can determine how to check the size of the Blob entries.

查看更多
霸刀☆藐视天下
3楼-- · 2019-05-07 01:10

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

查看更多
疯言疯语
4楼-- · 2019-05-07 01:12

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");
            }
        }
    });
查看更多
登录 后发表回答