views:

77

answers:

2

What can I do to limit the size of a file that can be uploaded? I know I can limit it client side with SWFUpload, but how can I limit it server side?

How do I protect against someone uploading a 1GB file and eating up my quota?

A: 

Put the code below in serlet where you are recieving data, before calling datastore API.

// Determine the HTTP method
long maxSize = 1024*1024 * 50; //Limit 50 MB
boolean isPostMethod = request.getMethod().equals("POST");

// Verify the content length
int contentLength = request.getContentLength();
if (isPostMethod && (contentLength < 0 || contentLength > maxSize))
  //posted data size is not in acceptable range
else {
    // Process Data
}
Manjoor
As the OP said, this is using the App Engine blobstore API, so this doesn't apply.
Nick Johnson
And from where he'll call blobstore API? from within a servlet? Do you tell me which line of code is not applicable in App Engine?
Manjoor
if you read the docs on the blobstore (which is different than the datastore) you'll see that none of the lines above are applicable - your servlet doesn't accept the data from the user, the blobstore does. the form the user submits goes directly to the blobstore, not your servlet.http://code.google.com/appengine/docs/java/blobstore/overview.html
Peter Recore
+2  A: 

You can't prohibit people from uploading files that are too large (though this would make a good feature request). What you can do is check the size of the uploaded blob, and immediately delete it if it's too big.

Nick Johnson
I filed a feature request - http://code.google.com/p/googleappengine/issues/detail?id=3554
Kyle