@@ -39,10 +39,40 @@ public static async Task Main(string[] args)
39
39
40
40
// Create a key prefix for this example
41
41
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
+ } ;
42
66
43
67
// Generate the presigned POST URL with combined features
44
68
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" ) ;
46
76
47
77
// Display the URL and fields
48
78
Console . WriteLine ( "\n Presigned POST URL with combined features created successfully:" ) ;
@@ -66,58 +96,6 @@ public static async Task Main(string[] args)
66
96
Console . WriteLine ( $ "Error: { ex . Message } ") ;
67
97
}
68
98
}
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]
121
99
122
100
/// <summary>
123
101
/// Create a presigned POST URL with conditions.
@@ -130,7 +108,6 @@ public static async Task<CreatePresignedPostResponse> CreateWithFilenameAndCondi
130
108
/// <param name="fields">Dictionary of fields to add to the form.</param>
131
109
/// <param name="conditions">List of conditions to apply.</param>
132
110
/// <returns>A CreatePresignedPostResponse object with URL and form fields.</returns>
133
- // snippet-start:[S3.dotnetv4.CreatePresignedPostAsync]
134
111
public static async Task < CreatePresignedPostResponse > CreatePresignedPostAsync (
135
112
IAmazonS3 s3Client ,
136
113
ILogger logger ,
@@ -167,13 +144,11 @@ public static async Task<CreatePresignedPostResponse> CreatePresignedPostAsync(
167
144
168
145
return await s3Client . CreatePresignedPostAsync ( request ) ;
169
146
}
170
- // snippet-end:[S3.dotnetv4.CreatePresignedPostAsync]
171
147
172
148
/// <summary>
173
149
/// Display the fields from a presigned POST response.
174
150
/// </summary>
175
151
/// <param name="response">The CreatePresignedPostResponse to display.</param>
176
- // snippet-start:[S3.dotnetv4.DisplayPresignedPostFields]
177
152
public static void DisplayPresignedPostFields ( CreatePresignedPostResponse response )
178
153
{
179
154
Console . WriteLine ( $ "Presigned POST URL: { response . Url } ") ;
@@ -184,6 +159,5 @@ public static void DisplayPresignedPostFields(CreatePresignedPostResponse respon
184
159
Console . WriteLine ( $ " { field . Key } : { field . Value } ") ;
185
160
}
186
161
}
187
- // snippet-end:[S3.dotnetv4.DisplayPresignedPostFields]
188
162
}
189
163
// snippet-end:[S3.dotnetv4.CreatePresignedPost]
0 commit comments