Mercurial
comparison s3/tests/s3_uploader_test.c @ 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 | 008ca7780c8a |
comparison
equal
deleted
inserted
replaced
| 195:f8f5004a920a | 196:83f16548ba41 |
|---|---|
| 1 #include "s3/s3_uploader.h" | |
| 2 | |
| 3 #include <stdio.h> | |
| 4 #include <string.h> | |
| 5 | |
| 6 // Simple test for S3 uploader | |
| 7 // Run with: bazel run //s3/tests:s3_uploader_test | |
| 8 // | |
| 9 // Set environment variables before running: | |
| 10 // export AWS_ACCESS_KEY_ID="your-access-key" | |
| 11 // export AWS_SECRET_ACCESS_KEY="your-secret-key" | |
| 12 // export AWS_REGION="us-east-1" | |
| 13 // export AWS_BUCKET="your-bucket" | |
| 14 | |
| 15 int main(int argc, char **argv) | |
| 16 { | |
| 17 const char *access_key = getenv("AWS_ACCESS_KEY_ID"); | |
| 18 const char *secret_key = getenv("AWS_SECRET_ACCESS_KEY"); | |
| 19 const char *region = getenv("AWS_REGION"); | |
| 20 const char *bucket = getenv("AWS_BUCKET"); | |
| 21 | |
| 22 if (!access_key || !secret_key || !region || !bucket) | |
| 23 { | |
| 24 printf("Missing environment variables. Set:\n"); | |
| 25 printf(" AWS_ACCESS_KEY_ID\n"); | |
| 26 printf(" AWS_SECRET_ACCESS_KEY\n"); | |
| 27 printf(" AWS_REGION\n"); | |
| 28 printf(" AWS_BUCKET\n"); | |
| 29 return 1; | |
| 30 } | |
| 31 | |
| 32 S3_Config config = { | |
| 33 .access_key_id = access_key, | |
| 34 .secret_access_key = secret_key, | |
| 35 .region = region, | |
| 36 .bucket = bucket, | |
| 37 .endpoint = NULL, | |
| 38 .use_path_style = FALSE | |
| 39 }; | |
| 40 | |
| 41 printf("S3 Uploader Test\n"); | |
| 42 printf("================\n"); | |
| 43 printf("Region: %s\n", region); | |
| 44 printf("Bucket: %s\n", bucket); | |
| 45 printf("\n"); | |
| 46 | |
| 47 // Test 1: Upload string data | |
| 48 printf("Test 1: Uploading string data...\n"); | |
| 49 const char *test_data = "Hello from S3 uploader test!"; | |
| 50 S3_Result result = S3_Upload_Data(&config, | |
| 51 (const uint8 *)test_data, | |
| 52 strlen(test_data), | |
| 53 "test/hello.txt", | |
| 54 "text/plain"); | |
| 55 if (result.success) | |
| 56 { | |
| 57 printf(" SUCCESS! ETag: %s\n", result.etag ? result.etag : "(none)"); | |
| 58 } | |
| 59 else | |
| 60 { | |
| 61 printf(" FAILED! Status: %d, Error: %s\n", | |
| 62 result.status_code, | |
| 63 result.error_message ? result.error_message : "(unknown)"); | |
| 64 } | |
| 65 S3_Result_Destroy(&result); | |
| 66 | |
| 67 // Test 2: Content type detection | |
| 68 printf("\nTest 2: Content type detection...\n"); | |
| 69 printf(" .html -> %s\n", S3_Guess_Content_Type("page.html")); | |
| 70 printf(" .png -> %s\n", S3_Guess_Content_Type("image.png")); | |
| 71 printf(" .json -> %s\n", S3_Guess_Content_Type("data.json")); | |
| 72 printf(" .xyz -> %s\n", S3_Guess_Content_Type("unknown.xyz")); | |
| 73 | |
| 74 // Test 3: Generate presigned PUT URL | |
| 75 printf("\nTest 3: Generating presigned PUT URL...\n"); | |
| 76 S3_Presigned_URL put_url = S3_Presign_Put(&config, | |
| 77 "uploads/client-upload.png", | |
| 78 "image/png", | |
| 79 3600); // 1 hour | |
| 80 if (put_url.success) | |
| 81 { | |
| 82 printf(" SUCCESS!\n"); | |
| 83 printf(" URL: %s\n", put_url.url); | |
| 84 printf("\n Client can upload with:\n"); | |
| 85 printf(" curl -X PUT -H \"Content-Type: image/png\" --data-binary @file.png \"$URL\"\n"); | |
| 86 } | |
| 87 else | |
| 88 { | |
| 89 printf(" FAILED! Error: %s\n", put_url.error_message ? put_url.error_message : "(unknown)"); | |
| 90 } | |
| 91 S3_Presigned_URL_Destroy(&put_url); | |
| 92 | |
| 93 // Test 4: Generate presigned GET URL | |
| 94 printf("\nTest 4: Generating presigned GET URL...\n"); | |
| 95 S3_Presigned_URL get_url = S3_Presign_Get(&config, | |
| 96 "uploads/client-upload.png", | |
| 97 3600); // 1 hour | |
| 98 if (get_url.success) | |
| 99 { | |
| 100 printf(" SUCCESS!\n"); | |
| 101 printf(" URL: %s\n", get_url.url); | |
| 102 printf("\n Client can download with:\n"); | |
| 103 printf(" curl \"$URL\" -o file.png\n"); | |
| 104 } | |
| 105 else | |
| 106 { | |
| 107 printf(" FAILED! Error: %s\n", get_url.error_message ? get_url.error_message : "(unknown)"); | |
| 108 } | |
| 109 S3_Presigned_URL_Destroy(&get_url); | |
| 110 | |
| 111 // Test 5: Upload file (if provided as argument) | |
| 112 if (argc >= 3) | |
| 113 { | |
| 114 printf("\nTest 5: Uploading file...\n"); | |
| 115 printf(" Local: %s\n", argv[1]); | |
| 116 printf(" S3 Key: %s\n", argv[2]); | |
| 117 | |
| 118 result = S3_Upload_File(&config, argv[1], argv[2]); | |
| 119 if (result.success) | |
| 120 { | |
| 121 printf(" SUCCESS! ETag: %s\n", result.etag ? result.etag : "(none)"); | |
| 122 } | |
| 123 else | |
| 124 { | |
| 125 printf(" FAILED! Status: %d, Error: %s\n", | |
| 126 result.status_code, | |
| 127 result.error_message ? result.error_message : "(unknown)"); | |
| 128 } | |
| 129 S3_Result_Destroy(&result); | |
| 130 } | |
| 131 else | |
| 132 { | |
| 133 printf("\nTest 5: Skipped (no file path provided)\n"); | |
| 134 printf(" Usage: %s <local_file> <s3_key>\n", argv[0]); | |
| 135 } | |
| 136 | |
| 137 printf("\nDone!\n"); | |
| 138 return 0; | |
| 139 } |