I'm trying to create a signed policy for users to upload their files on Google Cloud Storage.
The Issue i'm facing here is with the filename, i want the user to provide the filename at the time of the upload, as mentioned in the official GCS documentation, you can provide filename as ${filename} if you want it from the user, this doesn't work as i m getting the following error :
<?xml version='1.0' encoding='UTF-8'?>
<Error>
<Code>InvalidPolicyDocument</Code>
<Message>The content of the form does not meet the conditions specified in the policy document.</Message>
<Details>Failed condition: {"key":"${filename}"}</Details>
</Error>
I've tried doing the same with S3's createPresignedPost method and it works fine.
Reference : https://cloud.google.com/storage/docs/xml-api/post-object-forms
Any help would be appreciated.
My Node.js Code for generating policy from GCS :
const { Storage } = require('@google-cloud/storage');
const storage = new Storage();
generateSignedPolicy = () => {
const bucket = 'some-bucket';
const file = storage.bucket(bucket).file("someFolder/${filename}");
const options = {
expires: Date.now() + (1000 * 300),
conditions : [
{ bucket : bucket },
]
};
return new Promise((resolve, reject) => {
file.generateSignedPostPolicyV4(options)
.then(([res]) => resolve(res))
.catch(error => reject(error))
})
}
Policy generated with above code :
{
"url": "https://storage.googleapis.com/some-bucket/",
"fields": {
"key": "someFolder/${filename}",
"x-goog-date": "20221015T212358Z",
"x-goog-credential": "credential",
"x-goog-algorithm": "GOOG4-RSA-SHA256",
"policy": "policy",
"x-goog-signature": "signature"
}
}