Limiting file-size and Content using GCS SignedURL v4


I was looking into GCS SignedURLs yesterday for ways to limit the amount of data a SignedURL could upload.

I wasn’t particularly optimistic since its I understood SignedURLs to just say “ok, if you have this URL, you are free to upload whatever you want to this file destination”. Meaning, if you give someone a signedURL, they can upload 5MB or 5GB or whatever.

While Signed Policy Documents do allow some ways to set the size and type of files to upload, that mechanism isn’t generally encouraged and (IMO) there just to support/parity with other storage providers for browser-form uploads.

The better way to is to use the well-known Signed URL.

I was mostly sort-of familiar with the v2 signing process and just couldn’t think of a way to limit the file that was actually uploaded thinking that was out of the control of the issuer.

I began trying stuff out and noticed the v4Signer in go had the ability to set Header values.

So..i thought these headers must get honored when you use a PUT request. If thats possible, then i can sort of control the size of the file.

From there a bit of fiddling and there it is…i found out you can generate a SignedURL, limit the total size…and this shows how to do that…

but wait, this’ll also show how to also limit the upload to a known content! yeah, i can create a signed url, set its limit or set the hash of the file that will ultimately get uploaded.

image from:

So..while I started writing about this, i looked around a bit more and found out Nacho Coloma already did something around this in Limit the size of files uploaded with Signed URLs on Google Cloud Storage

note to self: very few thing you do is really ever that unique

However, I think there’s a minor bit to correct in that article above: you can actually sign a PUT url and set the Content-Length it will use. The article above states you can’t but..i happened to stumble on it earlier.

So, this article is about the same but with a spin on golang and addresses a couple of things:

  1. Generate a Signed URL that ONLY allows for 10bytes AND the file that has content MD5hash of eB5eJF1ptWaXm4bijSPyxw==

    Once the signedURL is created, upload it with a PUT. (and yes, i didn’t need to set the size limit if i could just set the hash..)

  2. Generate a Resumable Signed URL that only allows for 10bytes.

    Once the signedURL is created, upload the file to the Location URL that is provided.

for other background reading for signedURLs



Create a bucket, service account:

export PROJECT_ID=`gcloud config get-value core/project`
export PROJECT_NUMBER=`gcloud projects describe $PROJECT_ID --format='value(projectNumber)'`

gcloud iam service-accounts create urlsigner --display-name="GCS URL Signer" --project=${PROJECT_ID}
gcloud iam service-accounts keys  create service_account.json --iam-account=urlsigner@${PROJECT_ID}
gsutil mb gs://$PROJECT_ID-urlsigner

gsutil iam ch  serviceAccount:urlsigner@${PROJECT_NAME} gs://$PROJECT_ID-urlsigner

Now, what the demo shows is:

  • given file contents denoted by their byte count and md5hash

    const (
        fiveBytes        = "01234"   // md5hash QQDE1E2pF3JH5EpfwVRneA==
        tenBytes         = "0123456789"  // md5hash eB5eJF1ptWaXm4bijSPyxw==
        tenBytesReversed = "9876543210"  // md5hash 44jBxd9JM/oB9tqfkllViQ==
        twentyBytes      = "01234567890123456789"  // md5hash vkl8IWjjdPQUo1HEk3nAGg==
  • generate a non-resumable SignedURL that allows PUT for a file with Content-Length: 10 AND whose md5=eB5eJF1ptWaXm4bijSPyxw==

    which means only the tenBytes file can be uploaded

  • attempt uploading each of the files and see only tenBytes succeed

  • then generate a resumable SignedURL that allows only files of Content-Length: 10

  • exchange the signedURL for the upload Location.

  • attempting to use that signedURL to upload a file of any other content size will fail to issue the Location

  • attempt uploaded each of the files and see only tenBytes and tenBytesReversed succeed.

To see the program run:

export GOOGLE_APPLICATION_CREDENTIALS=`pwd`/service_account.json

go run main.go --bucketName $PROJECT_ID-urlsigner

______________________ upload _________________
UploadStatus for file of size [5] with hash [QQDE1E2pF3JH5EpfwVRneA==]  403 Forbidden
UploadStatus for file of size [10] with hash [eB5eJF1ptWaXm4bijSPyxw==]  200 OK
UploadStatus for file of size [10] with hash [44jBxd9JM/oB9tqfkllViQ==]  403 Forbidden
UploadStatus for file of size [20] with hash [vkl8IWjjdPQUo1HEk3nAGg==]  403 Forbidden

______________________ resumable _________________
Could not generate resumable location for size [5]  403 Forbidden
UploadStatus for file of size [10]  200 OK
UploadStatus for file of size [10]  200 OK
Could not generate resumable location for size [20]  403 Forbidden

What the above shows is files on the upload only tenBytes succeeded (since the file size and hash need to match)

The resumable section shows both tenBytes and tenBytesReversed succeeded since they both share the same size

So…i just happened to notice this capability and I do know customers time to time ask about how to limit sizes during uploads with SignedURL

This site supports webmentions. Send me a mention via this form.