When working with the AWS S3 service in .NET, generating a pre-signed URL for file uploads is a common task. However, while the AWS SDK for .NET provides functionality to generate pre-signed URLs using the GetPreSignedURL
method, there seems to be a lack of documentation or examples demonstrating how to include a POST policy with the request.
The .Net SDK documentation shows an example ( GetPreSignedURL sample 4 ) to generate a pre signed URL but not to include a Post Policy and neither the GetPreSignedUrlRequest object has that option.
// Create a client
AmazonS3Client client = new AmazonS3Client();
// Create a CopyObject request
GetPreSignedUrlRequest request = new GetPreSignedUrlRequest
{
Expires = DateTime.UtcNow.AddMinutes(5)
};
// Get path for request
string path = client.GetPreSignedURL(request);
// Retrieve buckets
string allBuckets = GetContents(path);
For example, the NodeJS SDK allows to pass a Conditions parameter to create the Post Policy https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-s3-presigned-post/
const Conditions = [{ acl: "bucket-owner-full-control" }, { bucket: "johnsmith" }, ["starts-with", "$key", "user/eric/"]];
const client = new S3Client({ region: "us-west-2" });
const Bucket = "johnsmith";
const Key = "user/eric/1";
const Fields = {
acl: "bucket-owner-full-control",
};
const { url, fields } = await createPresignedPost(client, {
Bucket,
Key,
Conditions,
Fields,
Expires: 600, //Seconds before the presigned post expires. 3600 by default.
});
However, translating this functionality to the .NET SDK remains unclear.
How can a POST policy be attached to create a pre-signed URL for uploading files to S3 using the .NET SDK? Any insights, examples, or pointers would be greatly appreciated. Thank you!
I tried to pass the conditions as the Parameters to the GetPreSignedUrlRequest object, but that created the URL as x-Policy not Policy, and the v4 signature generated is incorrect
1