comparison s3/tests/s3_uploader_test.c @ 198:008ca7780c8a

S3 upload got it to work.
author MrJuneJune <me@mrjunejune.com>
date Sat, 14 Feb 2026 16:18:14 -0800
parents 83f16548ba41
children
comparison
equal deleted inserted replaced
197:0106cb67d958 198:008ca7780c8a
1 #include "s3/s3_uploader.h" 1 #include "s3/s3_uploader.h"
2 2
3 #include <stdio.h> 3 #include <stdio.h>
4 #include <stdlib.h>
4 #include <string.h> 5 #include <string.h>
5 6
6 // Simple test for S3 uploader 7 // Simple .env parser
7 // Run with: bazel run //s3/tests:s3_uploader_test 8 static char *parse_env_value(const char *env_content, const char *key)
8 // 9 {
9 // Set environment variables before running: 10 const char *pos = strstr(env_content, key);
10 // export AWS_ACCESS_KEY_ID="your-access-key" 11 if (!pos) return NULL;
11 // export AWS_SECRET_ACCESS_KEY="your-secret-key" 12
12 // export AWS_REGION="us-east-1" 13 pos += strlen(key);
13 // export AWS_BUCKET="your-bucket" 14 if (*pos != '=') return NULL;
15 pos++;
16
17 const char *end = pos;
18 while (*end && *end != '\n' && *end != '\r') end++;
19
20 size_t len = end - pos;
21 char *value = malloc(len + 1);
22 memcpy(value, pos, len);
23 value[len] = '\0';
24 return value;
25 }
26
27 static char *load_file(const char *path)
28 {
29 FILE *f = fopen(path, "r");
30 if (!f) return NULL;
31
32 fseek(f, 0, SEEK_END);
33 long size = ftell(f);
34 fseek(f, 0, SEEK_SET);
35
36 char *content = malloc(size + 1);
37 fread(content, 1, size, f);
38 content[size] = '\0';
39 fclose(f);
40 return content;
41 }
14 42
15 int main(int argc, char **argv) 43 int main(int argc, char **argv)
16 { 44 {
17 const char *access_key = getenv("AWS_ACCESS_KEY_ID"); 45 // Try to load .env file - check multiple locations for bazel runfiles
18 const char *secret_key = getenv("AWS_SECRET_ACCESS_KEY"); 46 char *env_content = load_file(".env");
19 const char *region = getenv("AWS_REGION"); 47 if (!env_content)
20 const char *bucket = getenv("AWS_BUCKET"); 48 {
49 env_content = load_file("zenbu/.env"); // bazel runfiles path
50 }
51 if (!env_content)
52 {
53 env_content = load_file("../.env");
54 }
55 if (!env_content)
56 {
57 env_content = load_file("../../.env");
58 }
21 59
22 if (!access_key || !secret_key || !region || !bucket) 60 char *access_key = NULL;
61 char *secret_key = NULL;
62
63 if (env_content)
23 { 64 {
24 printf("Missing environment variables. Set:\n"); 65 access_key = parse_env_value(env_content, "AWS_MRJUNEJUNE_ACCESS_KEY");
25 printf(" AWS_ACCESS_KEY_ID\n"); 66 secret_key = parse_env_value(env_content, "AWS_MRJUNEJUNE_SECRET_ACCESS_KEY");
26 printf(" AWS_SECRET_ACCESS_KEY\n"); 67 free(env_content);
27 printf(" AWS_REGION\n"); 68 }
28 printf(" AWS_BUCKET\n"); 69
70 // Fall back to environment variables
71 if (!access_key) access_key = getenv("AWS_ACCESS_KEY_ID") ? strdup(getenv("AWS_ACCESS_KEY_ID")) : NULL;
72 if (!secret_key) secret_key = getenv("AWS_SECRET_ACCESS_KEY") ? strdup(getenv("AWS_SECRET_ACCESS_KEY")) : NULL;
73
74 if (!access_key || !secret_key)
75 {
76 printf("Missing credentials. Set in .env file or environment:\n");
77 printf(" AWS_MRJUNEJUNE_ACCESS_KEY / AWS_ACCESS_KEY_ID\n");
78 printf(" AWS_MRJUNEJUNE_SECRET_ACCESS_KEY / AWS_SECRET_ACCESS_KEY\n");
29 return 1; 79 return 1;
30 } 80 }
31 81
32 S3_Config config = { 82 S3_Config config = {
33 .access_key_id = access_key, 83 .access_key_id = access_key,
34 .secret_access_key = secret_key, 84 .secret_access_key = secret_key,
35 .region = region, 85 .region = "us-west-2",
36 .bucket = bucket, 86 .bucket = "mrjunejune",
37 .endpoint = NULL, 87 .endpoint = NULL,
38 .use_path_style = FALSE 88 .use_path_style = FALSE
39 }; 89 };
40 90
41 printf("S3 Uploader Test\n"); 91 printf("S3 Uploader Test\n");
42 printf("================\n"); 92 printf("================\n");
43 printf("Region: %s\n", region); 93 printf("Region: %s\n", config.region);
44 printf("Bucket: %s\n", bucket); 94 printf("Bucket: %s\n", config.bucket);
45 printf("\n"); 95 printf("\n");
46 96
47 // Test 1: Upload string data 97 // Test 1: Upload string data
48 printf("Test 1: Uploading string data...\n"); 98 printf("Test 1: Uploading string data...\n");
49 const char *test_data = "Hello from S3 uploader test!"; 99 const char *test_data = "Hello from S3 uploader test!";
133 printf("\nTest 5: Skipped (no file path provided)\n"); 183 printf("\nTest 5: Skipped (no file path provided)\n");
134 printf(" Usage: %s <local_file> <s3_key>\n", argv[0]); 184 printf(" Usage: %s <local_file> <s3_key>\n", argv[0]);
135 } 185 }
136 186
137 printf("\nDone!\n"); 187 printf("\nDone!\n");
188
189 free(access_key);
190 free(secret_key);
138 return 0; 191 return 0;
139 } 192 }