Implement flushing of file blocks to Amazon S3.
[bluesky.git] / s3store.cc
diff --git a/s3store.cc b/s3store.cc
new file mode 100644 (file)
index 0000000..aff9080
--- /dev/null
@@ -0,0 +1,91 @@
+/* Blue Sky: File Systems in the Cloud
+ *
+ * Copyright (C) 2009  The Regents of the University of California
+ * Written by Michael Vrable <mvrable@cs.ucsd.edu>
+ *
+ * TODO: Licensing
+ */
+
+#include <stdint.h>
+#include <stdlib.h>
+#include <glib.h>
+#include <string.h>
+
+#include "bluesky.h"
+#include "libs3.h"
+
+/* Interface to Amazon S3 storage. */
+
+/* Simple in-memory data store for test purposes. */
+struct S3Store {
+    S3BucketContext bucket;
+};
+
+S3Store *s3store_new()
+{
+    S3Store *store = g_new(S3Store, 1);
+    store->bucket.bucketName = "mvrable-bluesky";
+    store->bucket.protocol = S3ProtocolHTTP;
+    store->bucket.uriStyle = S3UriStylePath;
+    store->bucket.accessKeyId = getenv("AWS_ACCESS_KEY_ID");
+    store->bucket.secretAccessKey = getenv("AWS_SECRET_ACCESS_KEY");
+
+    g_print("Initializing S3 with bucket %s, access key %s\n",
+            store->bucket.bucketName, store->bucket.accessKeyId);
+
+    return store;
+}
+
+BlueSkyRCStr *s3store_get(S3Store *store, const gchar *key)
+{
+    return NULL;
+}
+
+struct put_info {
+    BlueSkyRCStr *val;
+    gint offset;
+};
+
+static int s3store_put_handler(int bufferSize, char *buffer,
+                               void *callbackData)
+{
+    struct put_info *info = (struct put_info *)callbackData;
+    gint bytes = MIN(bufferSize, (int)(info->val->len - info->offset));
+    memcpy(buffer, (char *)info->val->data + info->offset, bytes);
+    info->offset += bytes;
+    return bytes;
+}
+
+static S3Status s3store_properties_callback(const S3ResponseProperties *properties,
+                                     void *callbackData)
+{
+    g_print("(Properties callback)\n");
+    return S3StatusOK;
+}
+
+void s3store_response_callback(S3Status status,
+                               const S3ErrorDetails *errorDetails,
+                               void *callbackData)
+{
+    g_print("S3 operation complete, status=%s\n",
+            S3_get_status_name(status));
+    if (errorDetails != NULL) {
+        g_print("  Error message: %s\n", errorDetails->message);
+    }
+}
+
+void s3store_put(S3Store *store, const gchar *key, BlueSkyRCStr *val)
+{
+    struct put_info info;
+    info.val = val;
+    info.offset = 0;
+
+    struct S3PutObjectHandler handler;
+    handler.responseHandler.propertiesCallback = s3store_properties_callback;
+    handler.responseHandler.completeCallback = s3store_response_callback;
+    handler.putObjectDataCallback = s3store_put_handler;
+
+    g_print("Starting store of %s to S3...\n", key);
+    S3_put_object(&store->bucket, key, val->len, NULL, NULL,
+                  &handler, &info);
+}