1 /* Blue Sky: File Systems in the Cloud
3 * Copyright (C) 2009 The Regents of the University of California
4 * Written by Michael Vrable <mvrable@cs.ucsd.edu>
14 #include "bluesky-private.h"
17 /* Interface to Amazon S3 storage. */
20 GThreadPool *thread_pool;
21 S3BucketContext bucket;
22 uint8_t encryption_key[CRYPTO_KEY_SIZE];
26 enum { S3_GET, S3_PUT } op;
41 static S3Status s3store_get_handler(int bufferSize, const char *buffer,
44 struct get_info *info = (struct get_info *)callbackData;
45 g_string_append_len(info->buf, buffer, bufferSize);
49 static int s3store_put_handler(int bufferSize, char *buffer,
52 struct put_info *info = (struct put_info *)callbackData;
53 gint bytes = MIN(bufferSize, (int)(info->val->len - info->offset));
54 memcpy(buffer, (char *)info->val->data + info->offset, bytes);
55 info->offset += bytes;
59 static S3Status s3store_properties_callback(const S3ResponseProperties *properties,
62 g_print("(Properties callback)\n");
66 static void s3store_response_callback(S3Status status,
67 const S3ErrorDetails *errorDetails,
70 struct get_info *info = (struct get_info *)callbackData;
72 g_print("S3 operation complete, status=%s, now=%ld\n",
73 S3_get_status_name(status), bluesky_now_hires());
79 if (errorDetails != NULL) {
80 g_print(" Error message: %s\n", errorDetails->message);
84 static BlueSkyRCStr *s3store_get(gpointer s, const gchar *key)
86 S3Store *store = (S3Store *)s;
89 info.buf = g_string_new("");
92 struct S3GetObjectHandler handler;
93 handler.responseHandler.propertiesCallback = s3store_properties_callback;
94 handler.responseHandler.completeCallback = s3store_response_callback;
95 handler.getObjectDataCallback = s3store_get_handler;
97 g_print("Starting fetch of %s from S3...\n", key);
98 S3_get_object(&store->bucket, key, NULL, 0, 0, NULL,
102 g_string_free(info.buf, TRUE);
106 BlueSkyRCStr *raw, *decrypted;
107 raw = bluesky_string_new_from_gstring(info.buf);
108 decrypted = bluesky_crypt_decrypt(raw, store->encryption_key);
109 bluesky_string_unref(raw);
113 static void s3store_put(gpointer s, const gchar *key, BlueSkyRCStr *val)
115 S3Store *store = (S3Store *)s;
117 S3Op *op = g_new(S3Op, 1);
119 op->key = g_strdup(key);
120 bluesky_string_ref(val);
123 g_thread_pool_push(store->thread_pool, op, NULL);
126 static void s3store_task(gpointer o, gpointer s)
128 S3Store *store = (S3Store *)s;
129 S3Op *op = (S3Op *)o;
131 g_print("Start task [key=%s]...\n", op->key);
133 if (op->op == S3_PUT) {
134 BlueSkyRCStr *encrypted = bluesky_crypt_encrypt(op->data,
135 store->encryption_key);
137 struct put_info info;
138 info.val = encrypted;
141 struct S3PutObjectHandler handler;
142 handler.responseHandler.propertiesCallback
143 = s3store_properties_callback;
144 handler.responseHandler.completeCallback = s3store_response_callback;
145 handler.putObjectDataCallback = s3store_put_handler;
147 g_print("Starting store of %s to S3 at %ld...\n",
148 op->key, bluesky_now_hires());
149 S3_put_object(&store->bucket, op->key, encrypted->len, NULL, NULL,
152 bluesky_string_unref(encrypted);
155 bluesky_string_unref(op->data);
159 g_print("Finish task...\n");
162 static gpointer s3store_new()
164 S3Store *store = g_new(S3Store, 1);
165 store->thread_pool = g_thread_pool_new(s3store_task, store, -1, FALSE,
167 store->bucket.bucketName = "mvrable-bluesky";
168 store->bucket.protocol = S3ProtocolHTTP;
169 store->bucket.uriStyle = S3UriStylePath;
170 store->bucket.accessKeyId = getenv("AWS_ACCESS_KEY_ID");
171 store->bucket.secretAccessKey = getenv("AWS_SECRET_ACCESS_KEY");
173 const char *key = getenv("BLUESKY_KEY");
175 g_error("Encryption key not defined; please set BLUESKY_KEY environment variable");
179 bluesky_crypt_hash_key(key, store->encryption_key);
181 g_print("Initializing S3 with bucket %s, access key %s, encryption key %s\n",
182 store->bucket.bucketName, store->bucket.accessKeyId, key);
187 static void s3store_destroy(gpointer store)
192 static BlueSkyStoreImplementation store_impl = {
193 .create = s3store_new,
194 .destroy = s3store_destroy,
199 void bluesky_store_init_s3(void)
201 S3_initialize(NULL, S3_INIT_ALL);
202 bluesky_store_register(&store_impl, "s3");