@@ -39,10 +39,40 @@ public static async Task Main(string[] args)
3939
4040 // Create a key prefix for this example
4141 string keyPrefix = "uploads/" ;
42+ var expiration = DateTime . UtcNow . AddHours ( 1 ) ;
43+
44+ // Using "${filename}" placeholder in the key lets the browser replace it with the actual filename
45+ string objectKey = keyPrefix + "${filename}" ;
46+
47+ // Add custom metadata and fields
48+ var fields = new Dictionary < string , string >
49+ {
50+ // Add a custom metadata field
51+ { "x-amz-meta-uploaded-by" , "dotnet-sdk-example" } ,
52+
53+ // Return HTTP 201 on successful upload
54+ { "success_action_status" , "201" } ,
55+
56+ // Set the content type
57+ { "Content-Type" , "text/plain" }
58+ } ;
59+
60+ // Add policy conditions
61+ var conditions = new List < S3PostCondition >
62+ {
63+ // File size must be between 1 byte and 1 MB
64+ S3PostCondition . ContentLengthRange ( 1 , 1048576 )
65+ } ;
4266
4367 // Generate the presigned POST URL with combined features
4468 Console . WriteLine ( "\n Creating a presigned POST URL with both filename preservation and upload restrictions..." ) ;
45- var response = await CreateWithFilenameAndConditions ( s3Client , logger , bucketName , keyPrefix ) ;
69+ logger . LogInformation ( "Creating presigned POST URL with filename variable and conditions for {bucket}/{key}" ,
70+ bucketName , objectKey ) ;
71+
72+ var response = await CreatePresignedPostAsync (
73+ s3Client , logger , bucketName , objectKey , expiration , fields , conditions ) ;
74+
75+ logger . LogInformation ( "Successfully created presigned POST URL with filename variable and conditions" ) ;
4676
4777 // Display the URL and fields
4878 Console . WriteLine ( "\n Presigned POST URL with combined features created successfully:" ) ;
@@ -66,58 +96,6 @@ public static async Task Main(string[] args)
6696 Console . WriteLine ( $ "Error: { ex . Message } ") ;
6797 }
6898 }
69-
70- /// <summary>
71- /// Create a presigned POST URL with both filename variable and conditions.
72- /// </summary>
73- /// <param name="s3Client">The Amazon S3 client.</param>
74- /// <param name="logger">The logger to use.</param>
75- /// <param name="bucketName">The name of the bucket.</param>
76- /// <param name="keyPrefix">The prefix for the key, final key will be prefix + actual filename.</param>
77- /// <returns>A CreatePresignedPostResponse containing the URL and form fields.</returns>
78- // snippet-start:[S3.dotnetv4.CreateWithFilenameAndConditions]
79- public static async Task < CreatePresignedPostResponse > CreateWithFilenameAndConditions (
80- IAmazonS3 s3Client ,
81- ILogger logger ,
82- string bucketName ,
83- string keyPrefix )
84- {
85- var expiration = DateTime . UtcNow . AddHours ( 1 ) ;
86-
87- // Using "${filename}" placeholder in the key lets the browser replace it with the actual filename
88- string objectKey = keyPrefix + "${filename}" ;
89-
90- // Add custom metadata and fields
91- var fields = new Dictionary < string , string >
92- {
93- // Add a custom metadata field
94- { "x-amz-meta-uploaded-by" , "dotnet-sdk-example" } ,
95-
96- // Return HTTP 201 on successful upload
97- { "success_action_status" , "201" } ,
98-
99- // Set the content type
100- { "Content-Type" , "text/plain" }
101- } ;
102-
103- // Add policy conditions
104- var conditions = new List < S3PostCondition >
105- {
106- // File size must be between 1 byte and 1 MB
107- S3PostCondition . ContentLengthRange ( 1 , 1048576 )
108- } ;
109-
110- logger . LogInformation ( "Creating presigned POST URL with filename variable and conditions for {bucket}/{key}" ,
111- bucketName , objectKey ) ;
112-
113- var response = await CreatePresignedPostAsync (
114- s3Client , logger , bucketName , objectKey , expiration , fields , conditions ) ;
115-
116- logger . LogInformation ( "Successfully created presigned POST URL with filename variable and conditions" ) ;
117-
118- return response ;
119- }
120- // snippet-end:[S3.dotnetv4.CreateWithFilenameAndConditions]
12199
122100 /// <summary>
123101 /// Create a presigned POST URL with conditions.
@@ -130,7 +108,6 @@ public static async Task<CreatePresignedPostResponse> CreateWithFilenameAndCondi
130108 /// <param name="fields">Dictionary of fields to add to the form.</param>
131109 /// <param name="conditions">List of conditions to apply.</param>
132110 /// <returns>A CreatePresignedPostResponse object with URL and form fields.</returns>
133- // snippet-start:[S3.dotnetv4.CreatePresignedPostAsync]
134111 public static async Task < CreatePresignedPostResponse > CreatePresignedPostAsync (
135112 IAmazonS3 s3Client ,
136113 ILogger logger ,
@@ -167,13 +144,11 @@ public static async Task<CreatePresignedPostResponse> CreatePresignedPostAsync(
167144
168145 return await s3Client . CreatePresignedPostAsync ( request ) ;
169146 }
170- // snippet-end:[S3.dotnetv4.CreatePresignedPostAsync]
171147
172148 /// <summary>
173149 /// Display the fields from a presigned POST response.
174150 /// </summary>
175151 /// <param name="response">The CreatePresignedPostResponse to display.</param>
176- // snippet-start:[S3.dotnetv4.DisplayPresignedPostFields]
177152 public static void DisplayPresignedPostFields ( CreatePresignedPostResponse response )
178153 {
179154 Console . WriteLine ( $ "Presigned POST URL: { response . Url } ") ;
@@ -184,6 +159,5 @@ public static void DisplayPresignedPostFields(CreatePresignedPostResponse respon
184159 Console . WriteLine ( $ " { field . Key } : { field . Value } ") ;
185160 }
186161 }
187- // snippet-end:[S3.dotnetv4.DisplayPresignedPostFields]
188162}
189163// snippet-end:[S3.dotnetv4.CreatePresignedPost]
0 commit comments