Mercurial
comparison s3/s3_uploader.h @ 196:83f16548ba41
[AI] Adding s3 bucket uploader code using Seobeo.
| author | MrJuneJune <me@mrjunejune.com> |
|---|---|
| date | Sat, 14 Feb 2026 16:08:15 -0800 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 195:f8f5004a920a | 196:83f16548ba41 |
|---|---|
| 1 #ifndef S3_UPLOADER_H | |
| 2 #define S3_UPLOADER_H | |
| 3 | |
| 4 /** | |
| 5 * S3 Uploader | |
| 6 * ----------- | |
| 7 * | |
| 8 * Generic S3 bucket uploader using AWS Signature Version 4. | |
| 9 * Built on top of seobeo HTTP client. | |
| 10 * | |
| 11 * ## Examples | |
| 12 * | |
| 13 * ### 1. Upload a file from disk | |
| 14 * | |
| 15 * ```c | |
| 16 * S3_Config config = { | |
| 17 * .access_key_id = "AKIAIOSFODNN7EXAMPLE", | |
| 18 * .secret_access_key = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", | |
| 19 * .region = "us-east-1", | |
| 20 * .bucket = "my-bucket" | |
| 21 * }; | |
| 22 * | |
| 23 * S3_Result result = S3_Upload_File(&config, "/path/to/local/file.txt", "uploads/file.txt"); | |
| 24 * if (result.success) | |
| 25 * { | |
| 26 * printf("Uploaded successfully!\n"); | |
| 27 * } | |
| 28 * else | |
| 29 * { | |
| 30 * printf("Error: %s\n", result.error_message); | |
| 31 * } | |
| 32 * S3_Result_Destroy(&result); | |
| 33 * ``` | |
| 34 * | |
| 35 * ### 2. Upload raw data from memory | |
| 36 * | |
| 37 * ```c | |
| 38 * const char *data = "Hello, S3!"; | |
| 39 * S3_Result result = S3_Upload_Data(&config, (const uint8 *)data, strlen(data), | |
| 40 * "hello.txt", "text/plain"); | |
| 41 * S3_Result_Destroy(&result); | |
| 42 * ``` | |
| 43 * | |
| 44 * ### 3. Upload with custom content type | |
| 45 * | |
| 46 * ```c | |
| 47 * S3_Result result = S3_Upload_File_With_Content_Type(&config, | |
| 48 * "/path/to/image.png", | |
| 49 * "images/photo.png", | |
| 50 * "image/png"); | |
| 51 * S3_Result_Destroy(&result); | |
| 52 * ``` | |
| 53 */ | |
| 54 | |
| 55 #include <stddef.h> | |
| 56 | |
| 57 #include "dowa/dowa.h" | |
| 58 | |
| 59 // S3 configuration | |
| 60 typedef struct { | |
| 61 const char *access_key_id; // AWS access key ID | |
| 62 const char *secret_access_key; // AWS secret access key | |
| 63 const char *region; // AWS region (e.g., "us-east-1") | |
| 64 const char *bucket; // S3 bucket name | |
| 65 const char *endpoint; // Optional custom endpoint (NULL for default AWS) | |
| 66 boolean use_path_style; // Use path-style URLs (bucket in path, not subdomain) | |
| 67 } S3_Config; | |
| 68 | |
| 69 // Upload result | |
| 70 typedef struct { | |
| 71 boolean success; // TRUE if upload succeeded | |
| 72 int32 status_code; // HTTP status code | |
| 73 char *error_message; // Error message (NULL on success) | |
| 74 char *etag; // ETag of uploaded object (on success) | |
| 75 Dowa_Arena *p_arena; // Internal arena (do not modify) | |
| 76 } S3_Result; | |
| 77 | |
| 78 // --- Core API --- // | |
| 79 | |
| 80 /* Upload file from disk to S3. Content-Type is auto-detected. */ | |
| 81 extern S3_Result S3_Upload_File(const S3_Config *p_config, | |
| 82 const char *local_path, | |
| 83 const char *s3_key); | |
| 84 | |
| 85 /* Upload file with explicit content type. */ | |
| 86 extern S3_Result S3_Upload_File_With_Content_Type(const S3_Config *p_config, | |
| 87 const char *local_path, | |
| 88 const char *s3_key, | |
| 89 const char *content_type); | |
| 90 | |
| 91 /* Upload raw data from memory to S3. */ | |
| 92 extern S3_Result S3_Upload_Data(const S3_Config *p_config, | |
| 93 const uint8 *data, | |
| 94 size_t data_length, | |
| 95 const char *s3_key, | |
| 96 const char *content_type); | |
| 97 | |
| 98 /* Free resources associated with result. */ | |
| 99 extern void S3_Result_Destroy(S3_Result *p_result); | |
| 100 | |
| 101 // --- Presigned URLs --- // | |
| 102 | |
| 103 // Presigned URL result | |
| 104 typedef struct { | |
| 105 boolean success; // TRUE if generation succeeded | |
| 106 char *url; // The presigned URL (on success) | |
| 107 char *error_message; // Error message (on failure) | |
| 108 Dowa_Arena *p_arena; // Internal arena (do not modify) | |
| 109 } S3_Presigned_URL; | |
| 110 | |
| 111 /* Generate a presigned URL for uploading (PUT) a file. | |
| 112 * | |
| 113 * The client can use this URL to upload directly to S3 without credentials. | |
| 114 * | |
| 115 * Example: | |
| 116 * S3_Presigned_URL url = S3_Presign_Put(&config, "uploads/file.txt", "image/png", 3600); | |
| 117 * if (url.success) { | |
| 118 * printf("Upload URL: %s\n", url.url); | |
| 119 * // Client does: curl -X PUT -H "Content-Type: image/png" --data-binary @file.png "$URL" | |
| 120 * } | |
| 121 * S3_Presigned_URL_Destroy(&url); | |
| 122 */ | |
| 123 extern S3_Presigned_URL S3_Presign_Put(const S3_Config *p_config, | |
| 124 const char *s3_key, | |
| 125 const char *content_type, | |
| 126 int32 expires_seconds); | |
| 127 | |
| 128 /* Generate a presigned URL for downloading (GET) a file. | |
| 129 * | |
| 130 * Example: | |
| 131 * S3_Presigned_URL url = S3_Presign_Get(&config, "uploads/file.txt", 3600); | |
| 132 * if (url.success) { | |
| 133 * printf("Download URL: %s\n", url.url); | |
| 134 * // Client does: curl "$URL" -o file.txt | |
| 135 * } | |
| 136 * S3_Presigned_URL_Destroy(&url); | |
| 137 */ | |
| 138 extern S3_Presigned_URL S3_Presign_Get(const S3_Config *p_config, | |
| 139 const char *s3_key, | |
| 140 int32 expires_seconds); | |
| 141 | |
| 142 /* Free resources associated with presigned URL result. */ | |
| 143 extern void S3_Presigned_URL_Destroy(S3_Presigned_URL *p_url); | |
| 144 | |
| 145 // --- Utility Functions --- // | |
| 146 | |
| 147 /* Guess content type from file extension. Returns "application/octet-stream" if unknown. */ | |
| 148 extern const char *S3_Guess_Content_Type(const char *filename); | |
| 149 | |
| 150 #endif |