1
votes

Looking at the Azure Media Service REST API I can't see any way to limit the upload size of a file:

https://docs.microsoft.com/en-us/azure/media-services/media-services-rest-upload-files

For example I want a user to directly upload a file to azure but limit it to 50mb. I don't want to proxy the request via my servers first because that seems like unnecessary bandwidth usage.

Is it possible to do this with Azure? If it isn't possible to do it directly on Azure what is the best way to indirectly restrict the file upload size?

3

3 Answers

0
votes

From http://wely-lau.net/2012/02/26/uploading-big-files-in-windows-azure-blob-storage-with-putlistblock/, maybe something like this will work (maximum 64MB implicit):

protected void btnUpload_Click(object sender, EventArgs e)
{
    CloudBlobClient blobClient;
    var storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
    blobClient = storageAccount.CreateCloudBlobClient();

    CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");
    container.CreateIfNotExist();

    var permission = container.GetPermissions();
    permission.PublicAccess = BlobContainerPublicAccessType.Container;
    container.SetPermissions(permission);

    string name = fu.FileName;
    CloudBlockBlob blob = container.GetBlockBlobReference(name);

    blob.UploadFromStream(fu.FileContent);

    int maxSize = ....;

    if (fu.PostedFile.ContentLength > maxSize)
    {
        // Handle error: file too large
    } else {
        blob.UploadFromStream(fu.FileContent); 
    }
0
votes

If you don't want to proxy the upload you'll need to use browser-side validation. HTML5's File API has the answer.

From https://stackoverflow.com/a/4307882/4148708:

if (typeof FileReader !== "undefined") {
    var size = document.getElementById('myfile').files[0].size;
    // check file size
}

Of course this doesn't stop the user from using something like Postman to work around the limitation, but i mean, come on,

nobody got time for that

If you're more interested in something like the duration of the video rather than raw file size, you absolutely need to proxy the content bytes first – see this thread for more:
Verify duration of video which has been uploaded into azure blob

0
votes

If you are not using proxy solution, you are exposing to end users Azure storage Shared Access signature. There is no any size limitations associated with it. Only mitigationyou can take is restrict time during which saas signature is valid, so users have time limited time frame to do upload. Any validations on client will only partually mitigate the problem of uploading big files. As mentioned before users can take saas link from ui by debugging client side script and upload any content size to it within time frame link is valid.

Azure storage not exposing any apis to limit file upload size,so right now only bullet proof solution for your scenario is to use proxy.