@@ -5,41 +5,63 @@ namespace S3Actions;
5
5
6
6
// snippet-start:[S3.dotnetv4.CreatePresignedPost]
7
7
/// <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 .
10
10
/// </summary>
11
11
public class CreatePresignedPost
12
12
{
13
13
/// <summary>
14
- /// Create a basic presigned POST URL.
14
+ /// Create a presigned POST URL with both filename variable and conditions .
15
15
/// </summary>
16
16
/// <param name="s3Wrapper">The S3Wrapper instance to use.</param>
17
17
/// <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>
20
20
/// <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 )
26
26
{
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
+ } ;
34
51
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 ) ;
36
57
58
+ logger . LogInformation ( "Successfully created presigned POST URL with filename variable and conditions" ) ;
59
+
37
60
return response ;
38
61
}
39
62
40
-
41
63
/// <summary>
42
- /// Main method that demonstrates the CreatePresignedPost functionality .
64
+ /// Main method that demonstrates creating and using presigned POST URLs with combined features .
43
65
/// </summary>
44
66
/// <param name="args">Command line arguments. Not used in this example.</param>
45
67
/// <returns>Async task.</returns>
@@ -61,24 +83,32 @@ public static async Task Main(string[] args)
61
83
// Create the wrapper instance
62
84
var s3Wrapper = new S3Wrapper ( s3Client , loggerFactory . CreateLogger < S3Wrapper > ( ) ) ;
63
85
64
- Console . WriteLine ( "Amazon S3 CreatePresignedPost Basic Example" ) ;
65
- Console . WriteLine ( "========================================== " ) ;
86
+ Console . WriteLine ( "Amazon S3 CreatePresignedPost Example" ) ;
87
+ Console . WriteLine ( "===================================" ) ;
66
88
67
89
try
68
90
{
69
91
const string bucketName = "amzn-s3-demo-bucket" ;
70
92
Console . WriteLine ( $ "Using bucket: { bucketName } ") ;
71
93
Console . WriteLine ( "Note: You must have an existing bucket with this name or create one first." ) ;
72
94
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/ " ;
75
97
76
- // Generate the presigned POST URL
77
- Console . WriteLine ( "\n Creating 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 ( "\n Creating a presigned POST URL with both filename preservation and upload restrictions ..." ) ;
100
+ var response = await CreateWithFilenameAndConditions ( s3Wrapper , logger , bucketName , keyPrefix ) ;
79
101
80
- // Display the URL and fields that would be needed in an HTML form
102
+ // Display the URL and fields
103
+ Console . WriteLine ( "\n Presigned POST URL with combined features created successfully:" ) ;
81
104
PresignedPostUtils . DisplayPresignedPostFields ( response ) ;
105
+
106
+ Console . WriteLine ( "\n This 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" ) ;
82
112
83
113
Console . WriteLine ( "\n Example completed successfully." ) ;
84
114
}
0 commit comments