Add pluggable support for multiple storage backends.
[bluesky.git] / bluesky / s3store.c
1 /* Blue Sky: File Systems in the Cloud
2  *
3  * Copyright (C) 2009  The Regents of the University of California
4  * Written by Michael Vrable <mvrable@cs.ucsd.edu>
5  *
6  * TODO: Licensing
7  */
8
9 #include <stdint.h>
10 #include <stdlib.h>
11 #include <glib.h>
12 #include <string.h>
13
14 #include "bluesky.h"
15 #include "libs3.h"
16
17 /* Interface to Amazon S3 storage. */
18
19 /* Simple in-memory data store for test purposes. */
20 typedef struct {
21     S3BucketContext bucket;
22 } S3Store;
23
24 static gpointer s3store_new()
25 {
26     S3Store *store = g_new(S3Store, 1);
27     store->bucket.bucketName = "mvrable-bluesky";
28     store->bucket.protocol = S3ProtocolHTTP;
29     store->bucket.uriStyle = S3UriStylePath;
30     store->bucket.accessKeyId = getenv("AWS_ACCESS_KEY_ID");
31     store->bucket.secretAccessKey = getenv("AWS_SECRET_ACCESS_KEY");
32
33     g_print("Initializing S3 with bucket %s, access key %s\n",
34             store->bucket.bucketName, store->bucket.accessKeyId);
35
36     return store;
37 }
38
39 static void s3store_destroy(gpointer store)
40 {
41     g_free(store);
42 }
43
44 struct get_info {
45     gchar *buf;
46     gint offset;
47 };
48
49 static S3Status s3store_get_handler(int bufferSize, const char *buffer,
50                                     void *callbackData)
51 {
52     struct get_info *info = (struct get_info *)callbackData;
53     gint bytes = MIN(bufferSize, (int)(BLUESKY_BLOCK_SIZE - info->offset));
54     memcpy(info->buf + info->offset, buffer, bytes);
55     info->offset += bytes;
56     return S3StatusOK;
57 }
58
59 struct put_info {
60     BlueSkyRCStr *val;
61     gint offset;
62 };
63
64 static int s3store_put_handler(int bufferSize, char *buffer,
65                                void *callbackData)
66 {
67     struct put_info *info = (struct put_info *)callbackData;
68     gint bytes = MIN(bufferSize, (int)(info->val->len - info->offset));
69     memcpy(buffer, (char *)info->val->data + info->offset, bytes);
70     info->offset += bytes;
71     return bytes;
72 }
73
74 static S3Status s3store_properties_callback(const S3ResponseProperties *properties,
75                                      void *callbackData)
76 {
77     g_print("(Properties callback)\n");
78     return S3StatusOK;
79 }
80
81 static void s3store_response_callback(S3Status status,
82                                const S3ErrorDetails *errorDetails,
83                                void *callbackData)
84 {
85     g_print("S3 operation complete, status=%s\n",
86             S3_get_status_name(status));
87     if (errorDetails != NULL) {
88         g_print("  Error message: %s\n", errorDetails->message);
89     }
90 }
91
92 static BlueSkyRCStr *s3store_get(gpointer s, const gchar *key)
93 {
94     S3Store *store = (S3Store *)s;
95
96     struct get_info info;
97     info.buf = (char *)g_malloc0(BLUESKY_BLOCK_SIZE);
98     info.offset = 0;
99
100     struct S3GetObjectHandler handler;
101     handler.responseHandler.propertiesCallback = s3store_properties_callback;
102     handler.responseHandler.completeCallback = s3store_response_callback;
103     handler.getObjectDataCallback = s3store_get_handler;
104
105     g_print("Starting fetch of %s from S3...\n", key);
106     S3_get_object(&store->bucket, key, NULL, 0, 0, NULL,
107                   &handler, &info);
108
109     return bluesky_string_new(info.buf, BLUESKY_BLOCK_SIZE);
110 }
111
112 static void s3store_put(gpointer s, const gchar *key, BlueSkyRCStr *val)
113 {
114     S3Store *store = (S3Store *)s;
115
116     struct put_info info;
117     info.val = val;
118     info.offset = 0;
119
120     struct S3PutObjectHandler handler;
121     handler.responseHandler.propertiesCallback = s3store_properties_callback;
122     handler.responseHandler.completeCallback = s3store_response_callback;
123     handler.putObjectDataCallback = s3store_put_handler;
124
125     g_print("Starting store of %s to S3...\n", key);
126     S3_put_object(&store->bucket, key, val->len, NULL, NULL,
127                   &handler, &info);
128 }
129
130 static BlueSkyStoreImplementation store_impl = {
131     .create = s3store_new,
132     .destroy = s3store_destroy,
133     .get = s3store_get,
134     .put = s3store_put,
135 };
136
137 void bluesky_store_init_s3(void)
138 {
139     S3_initialize(NULL, S3_INIT_ALL);
140     bluesky_store_register(&store_impl, "s3");
141 }