Upload file to S3 and register in database
(
ProcedureContext ctx,
string filename,
string contentType,
List<byte> data,
string s3Bucket,
string s3Region)
| 241 | |
| 242 | // Upload file to S3 and register in database |
| 243 | [SpacetimeDB.Procedure] |
| 244 | public static string UploadToS3( |
| 245 | ProcedureContext ctx, |
| 246 | string filename, |
| 247 | string contentType, |
| 248 | List<byte> data, |
| 249 | string s3Bucket, |
| 250 | string s3Region) |
| 251 | { |
| 252 | // Generate a unique S3 key |
| 253 | var timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); |
| 254 | var s3Key = $"uploads/{timestamp}-{filename}"; |
| 255 | var url = $"https://{s3Bucket}.s3.{s3Region}.amazonaws.com/{s3Key}"; |
| 256 | |
| 257 | // Build the S3 PUT request (simplified - add AWS4 signature in production) |
| 258 | var request = new HttpRequest |
| 259 | { |
| 260 | Uri = url, |
| 261 | Method = SpacetimeDB.HttpMethod.Put, |
| 262 | Headers = new List<HttpHeader> |
| 263 | { |
| 264 | new HttpHeader("Content-Type", contentType), |
| 265 | new HttpHeader("x-amz-content-sha256", "UNSIGNED-PAYLOAD"), |
| 266 | // Add Authorization header with AWS4 signature |
| 267 | }, |
| 268 | Body = new HttpBody(data.ToArray()), |
| 269 | }; |
| 270 | |
| 271 | // Upload to S3 |
| 272 | var response = ctx.Http.Send(request).UnwrapOrThrow(); |
| 273 | |
| 274 | if (response.StatusCode != 200) |
| 275 | { |
| 276 | throw new Exception($"S3 upload failed with status: {response.StatusCode}"); |
| 277 | } |
| 278 | |
| 279 | // Store metadata in database |
| 280 | ctx.WithTx(txCtx => |
| 281 | { |
| 282 | txCtx.Db.Document.Insert(new Document |
| 283 | { |
| 284 | Id = 0, |
| 285 | OwnerId = txCtx.Sender, |
| 286 | Filename = filename, |
| 287 | S3Key = s3Key, |
| 288 | UploadedAt = txCtx.Timestamp, |
| 289 | }); |
| 290 | return 0; |
| 291 | }); |
| 292 | |
| 293 | return s3Key; |
| 294 | } |
| 295 | |
| 296 | // === Snippet 10: Pre-signed URL Flow === |
| 297 | [SpacetimeDB.Type] |
nothing calls this directly
no test coverage detected