diff 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
line wrap: on
line diff
--- a/s3/tests/s3_uploader_test.c	Sat Feb 14 16:08:47 2026 -0800
+++ b/s3/tests/s3_uploader_test.c	Sat Feb 14 16:18:14 2026 -0800
@@ -1,47 +1,97 @@
 #include "s3/s3_uploader.h"
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 
-// Simple test for S3 uploader
-// Run with: bazel run //s3/tests:s3_uploader_test
-//
-// Set environment variables before running:
-//   export AWS_ACCESS_KEY_ID="your-access-key"
-//   export AWS_SECRET_ACCESS_KEY="your-secret-key"
-//   export AWS_REGION="us-east-1"
-//   export AWS_BUCKET="your-bucket"
+// Simple .env parser
+static char *parse_env_value(const char *env_content, const char *key)
+{
+  const char *pos = strstr(env_content, key);
+  if (!pos) return NULL;
+
+  pos += strlen(key);
+  if (*pos != '=') return NULL;
+  pos++;
+
+  const char *end = pos;
+  while (*end && *end != '\n' && *end != '\r') end++;
+
+  size_t len = end - pos;
+  char *value = malloc(len + 1);
+  memcpy(value, pos, len);
+  value[len] = '\0';
+  return value;
+}
+
+static char *load_file(const char *path)
+{
+  FILE *f = fopen(path, "r");
+  if (!f) return NULL;
+
+  fseek(f, 0, SEEK_END);
+  long size = ftell(f);
+  fseek(f, 0, SEEK_SET);
+
+  char *content = malloc(size + 1);
+  fread(content, 1, size, f);
+  content[size] = '\0';
+  fclose(f);
+  return content;
+}
 
 int main(int argc, char **argv)
 {
-  const char *access_key = getenv("AWS_ACCESS_KEY_ID");
-  const char *secret_key = getenv("AWS_SECRET_ACCESS_KEY");
-  const char *region = getenv("AWS_REGION");
-  const char *bucket = getenv("AWS_BUCKET");
+  // Try to load .env file - check multiple locations for bazel runfiles
+  char *env_content = load_file(".env");
+  if (!env_content)
+  {
+    env_content = load_file("zenbu/.env");  // bazel runfiles path
+  }
+  if (!env_content)
+  {
+    env_content = load_file("../.env");
+  }
+  if (!env_content)
+  {
+    env_content = load_file("../../.env");
+  }
 
-  if (!access_key || !secret_key || !region || !bucket)
+  char *access_key = NULL;
+  char *secret_key = NULL;
+
+  if (env_content)
   {
-    printf("Missing environment variables. Set:\n");
-    printf("  AWS_ACCESS_KEY_ID\n");
-    printf("  AWS_SECRET_ACCESS_KEY\n");
-    printf("  AWS_REGION\n");
-    printf("  AWS_BUCKET\n");
+    access_key = parse_env_value(env_content, "AWS_MRJUNEJUNE_ACCESS_KEY");
+    secret_key = parse_env_value(env_content, "AWS_MRJUNEJUNE_SECRET_ACCESS_KEY");
+    free(env_content);
+  }
+
+  // Fall back to environment variables
+  if (!access_key) access_key = getenv("AWS_ACCESS_KEY_ID") ? strdup(getenv("AWS_ACCESS_KEY_ID")) : NULL;
+  if (!secret_key) secret_key = getenv("AWS_SECRET_ACCESS_KEY") ? strdup(getenv("AWS_SECRET_ACCESS_KEY")) : NULL;
+
+  if (!access_key || !secret_key)
+  {
+    printf("Missing credentials. Set in .env file or environment:\n");
+    printf("  AWS_MRJUNEJUNE_ACCESS_KEY / AWS_ACCESS_KEY_ID\n");
+    printf("  AWS_MRJUNEJUNE_SECRET_ACCESS_KEY / AWS_SECRET_ACCESS_KEY\n");
     return 1;
   }
 
   S3_Config config = {
     .access_key_id     = access_key,
     .secret_access_key = secret_key,
-    .region            = region,
-    .bucket            = bucket,
+    .region            = "us-west-2",
+    .bucket            = "mrjunejune",
     .endpoint          = NULL,
     .use_path_style    = FALSE
   };
 
   printf("S3 Uploader Test\n");
   printf("================\n");
-  printf("Region: %s\n", region);
-  printf("Bucket: %s\n", bucket);
+  printf("Region: %s\n", config.region);
+  printf("Bucket: %s\n", config.bucket);
   printf("\n");
 
   // Test 1: Upload string data
@@ -135,5 +185,8 @@
   }
 
   printf("\nDone!\n");
+
+  free(access_key);
+  free(secret_key);
   return 0;
 }