Skip to content

Commit 0adf5b6

Browse files
committed
update examples
1 parent 7d360d5 commit 0adf5b6

File tree

9 files changed

+87
-522
lines changed

9 files changed

+87
-522
lines changed

.doc_gen/metadata/s3_metadata.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3705,3 +3705,31 @@ s3_Scenario_DoesBucketExist:
37053705
- s3.java2.does-bucket-exist-main
37063706
services:
37073707
s3: {GetBucketAcl}
3708+
3709+
s3_CreatePresignedPost:
3710+
languages:
3711+
.NET:
3712+
versions:
3713+
- sdk_version: 4
3714+
github: dotnetv4/S3
3715+
excerpts:
3716+
- description:
3717+
snippet_tags:
3718+
- S3.dotnetv4.CreatePresignedPost
3719+
services:
3720+
s3: {CreatePresignedPost}
3721+
3722+
s3_Scenario_CreatePresignedPost:
3723+
title: Create and use presigned POST URLs
3724+
category: Scenarios
3725+
languages:
3726+
.NET:
3727+
versions:
3728+
- sdk_version: 4
3729+
github: dotnetv4/S3/Scenarios/S3_CreatePresignedPost
3730+
excerpts:
3731+
- description:
3732+
snippet_tags:
3733+
- S3.dotnetv4.CreatePresignedPostScenario
3734+
services:
3735+
s3: {CreateBucket, CreatePresignedPost, GetObjectMetadata, DeleteBucket}

dotnetv4/S3/Actions/CreatePresignedPost.cs

Lines changed: 58 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,63 @@ namespace S3Actions;
55

66
// snippet-start:[S3.dotnetv4.CreatePresignedPost]
77
/// <summary>
8-
/// Demonstrates how to use Amazon Simple Storage Service (Amazon S3)
9-
/// CreatePresignedPost functionality to generate a pre-signed URL for browser-based uploads.
8+
/// Demonstrates how to create Amazon S3 presigned POST URLs with both conditions and filename variables.
9+
/// This example shows how to add restrictions to uploads and preserve original filenames.
1010
/// </summary>
1111
public class CreatePresignedPost
1212
{
1313
/// <summary>
14-
/// Create a basic presigned POST URL.
14+
/// Create a presigned POST URL with both filename variable and conditions.
1515
/// </summary>
1616
/// <param name="s3Wrapper">The S3Wrapper instance to use.</param>
1717
/// <param name="logger">The logger to use.</param>
18-
/// <param name="bucketName">The name of the bucket where the file will be uploaded.</param>
19-
/// <param name="objectKey">The object key (path) where the file will be stored.</param>
18+
/// <param name="bucketName">The name of the bucket.</param>
19+
/// <param name="keyPrefix">The prefix for the key, final key will be prefix + actual filename.</param>
2020
/// <returns>A CreatePresignedPostResponse containing the URL and form fields.</returns>
21-
public static async Task<CreatePresignedPostResponse> CreateBasicPresignedPost(
22-
S3Wrapper s3Wrapper,
23-
ILogger logger,
24-
string bucketName,
25-
string objectKey)
21+
public static async Task<CreatePresignedPostResponse> CreateWithFilenameAndConditions(
22+
S3Wrapper s3Wrapper,
23+
ILogger logger,
24+
string bucketName,
25+
string keyPrefix)
2626
{
27-
// Set expiration time (maximum is 7 days from now)
28-
var expiration = DateTime.UtcNow.AddHours(1); // 1 hour expiration
29-
30-
logger.LogInformation("Creating presigned POST URL for {bucket}/{key} with expiration {expiration}",
31-
bucketName, objectKey, expiration);
32-
33-
var response = await s3Wrapper.CreatePresignedPostAsync(bucketName, objectKey, expiration);
27+
var expiration = DateTime.UtcNow.AddHours(1);
28+
29+
// Using "${filename}" placeholder in the key lets the browser replace it with the actual filename
30+
string objectKey = keyPrefix + "${filename}";
31+
32+
// Add custom metadata and fields
33+
var fields = new Dictionary<string, string>
34+
{
35+
// Add a custom metadata field
36+
{ "x-amz-meta-uploaded-by", "dotnet-sdk-example" },
37+
38+
// Return HTTP 201 on successful upload
39+
{ "success_action_status", "201" },
40+
41+
// Set the content type
42+
{ "Content-Type", "text/plain" }
43+
};
44+
45+
// Add policy conditions
46+
var conditions = new List<S3PostCondition>
47+
{
48+
// File size must be between 1 byte and 1 MB
49+
S3PostCondition.ContentLengthRange(1, 1048576)
50+
};
3451

35-
logger.LogInformation("Successfully created presigned POST URL: {url}", response.Url);
52+
logger.LogInformation("Creating presigned POST URL with filename variable and conditions for {bucket}/{key}",
53+
bucketName, objectKey);
54+
55+
var response = await s3Wrapper.CreatePresignedPostWithConditionsAsync(
56+
bucketName, objectKey, expiration, fields, conditions);
3657

58+
logger.LogInformation("Successfully created presigned POST URL with filename variable and conditions");
59+
3760
return response;
3861
}
3962

40-
4163
/// <summary>
42-
/// Main method that demonstrates the CreatePresignedPost functionality.
64+
/// Main method that demonstrates creating and using presigned POST URLs with combined features.
4365
/// </summary>
4466
/// <param name="args">Command line arguments. Not used in this example.</param>
4567
/// <returns>Async task.</returns>
@@ -61,24 +83,32 @@ public static async Task Main(string[] args)
6183
// Create the wrapper instance
6284
var s3Wrapper = new S3Wrapper(s3Client, loggerFactory.CreateLogger<S3Wrapper>());
6385

64-
Console.WriteLine("Amazon S3 CreatePresignedPost Basic Example");
65-
Console.WriteLine("==========================================");
86+
Console.WriteLine("Amazon S3 CreatePresignedPost Example");
87+
Console.WriteLine("===================================");
6688

6789
try
6890
{
6991
const string bucketName = "amzn-s3-demo-bucket";
7092
Console.WriteLine($"Using bucket: {bucketName}");
7193
Console.WriteLine("Note: You must have an existing bucket with this name or create one first.");
7294

73-
// Create a simple example object key
74-
string objectKey = "example-upload.txt";
95+
// Create a key prefix for this example
96+
string keyPrefix = "uploads/";
7597

76-
// Generate the presigned POST URL
77-
Console.WriteLine("\nCreating a presigned POST URL...");
78-
var response = await CreateBasicPresignedPost(s3Wrapper, logger, bucketName, objectKey);
98+
// Generate the presigned POST URL with combined features
99+
Console.WriteLine("\nCreating a presigned POST URL with both filename preservation and upload restrictions...");
100+
var response = await CreateWithFilenameAndConditions(s3Wrapper, logger, bucketName, keyPrefix);
79101

80-
// Display the URL and fields that would be needed in an HTML form
102+
// Display the URL and fields
103+
Console.WriteLine("\nPresigned POST URL with combined features created successfully:");
81104
PresignedPostUtils.DisplayPresignedPostFields(response);
105+
106+
Console.WriteLine("\nThis example combines multiple features:");
107+
Console.WriteLine(" • Uses ${filename} to preserve the original filename in the 'uploads/' prefix");
108+
Console.WriteLine(" • Adds custom metadata (x-amz-meta-uploaded-by)");
109+
Console.WriteLine(" • Sets success_action_status to return HTTP 201 on success");
110+
Console.WriteLine(" • Restricts content type to text/plain");
111+
Console.WriteLine(" • Limits file size to between 1 byte and 1 MB");
82112

83113
Console.WriteLine("\nExample completed successfully.");
84114
}

dotnetv4/S3/Actions/CreatePresignedPostWithConditions.cs

Lines changed: 0 additions & 113 deletions
This file was deleted.

dotnetv4/S3/Actions/CreatePresignedPostWithFilename.cs

Lines changed: 0 additions & 104 deletions
This file was deleted.

0 commit comments

Comments
 (0)