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,
65 static void s3store_response_callback(S3Status status,
66 const S3ErrorDetails *errorDetails,
69 struct get_info *info = (struct get_info *)callbackData;
75 if (errorDetails != NULL && errorDetails->message != NULL) {
76 g_print(" Error message: %s\n", errorDetails->message);
80 static void s3store_task(gpointer a, gpointer s)
82 BlueSkyStoreAsync *async = (BlueSkyStoreAsync *)a;
83 S3Store *store = (S3Store *)s;
85 async->status = ASYNC_RUNNING;
86 async->exec_time = bluesky_now_hires();
88 if (async->op == STORE_OP_GET) {
90 info.buf = g_string_new("");
93 struct S3GetObjectHandler handler;
94 handler.responseHandler.propertiesCallback = s3store_properties_callback;
95 handler.responseHandler.completeCallback = s3store_response_callback;
96 handler.getObjectDataCallback = s3store_get_handler;
98 S3_get_object(&store->bucket, async->key, NULL, 0, 0, NULL,
102 BlueSkyRCStr *raw, *decrypted;
103 raw = bluesky_string_new_from_gstring(info.buf);
104 decrypted = bluesky_crypt_decrypt(raw, store->encryption_key);
105 bluesky_string_unref(raw);
106 async->data = decrypted;
109 g_string_free(info.buf, TRUE);
112 } else if (async->op == STORE_OP_PUT) {
113 BlueSkyRCStr *encrypted = bluesky_crypt_encrypt(async->data,
114 store->encryption_key);
116 struct put_info info;
117 info.val = encrypted;
120 struct S3PutObjectHandler handler;
121 handler.responseHandler.propertiesCallback
122 = s3store_properties_callback;
123 handler.responseHandler.completeCallback = s3store_response_callback;
124 handler.putObjectDataCallback = s3store_put_handler;
126 S3_put_object(&store->bucket, async->key, encrypted->len, NULL, NULL,
129 bluesky_string_unref(encrypted);
134 bluesky_store_async_mark_complete(async);
135 bluesky_store_async_unref(async);
138 static gpointer s3store_new(const gchar *path)
140 S3Store *store = g_new(S3Store, 1);
141 store->thread_pool = g_thread_pool_new(s3store_task, store, -1, FALSE,
143 store->bucket.bucketName = "mvrable-bluesky";
144 store->bucket.protocol = S3ProtocolHTTP;
145 store->bucket.uriStyle = S3UriStylePath;
146 store->bucket.accessKeyId = getenv("AWS_ACCESS_KEY_ID");
147 store->bucket.secretAccessKey = getenv("AWS_SECRET_ACCESS_KEY");
149 const char *key = getenv("BLUESKY_KEY");
151 g_error("Encryption key not defined; please set BLUESKY_KEY environment variable");
155 bluesky_crypt_hash_key(key, store->encryption_key);
157 g_print("Initializing S3 with bucket %s, access key %s, encryption key %s\n",
158 store->bucket.bucketName, store->bucket.accessKeyId, key);
163 static void s3store_destroy(gpointer store)
168 static void s3store_submit(gpointer s, BlueSkyStoreAsync *async)
170 S3Store *store = (S3Store *)s;
171 g_return_if_fail(async->status == ASYNC_NEW);
172 g_return_if_fail(async->op != STORE_OP_NONE);
177 async->status = ASYNC_PENDING;
178 bluesky_store_async_ref(async);
179 g_thread_pool_push(store->thread_pool, async, NULL);
183 g_warning("Uknown operation type for S3Store: %d\n", async->op);
184 bluesky_store_async_mark_complete(async);
189 static void s3store_cleanup(gpointer store, BlueSkyStoreAsync *async)
191 GString *buf = (GString *)async->store_private;
194 g_string_free(buf, TRUE);
195 async->store_private = NULL;
199 static BlueSkyStoreImplementation store_impl = {
200 .create = s3store_new,
201 .destroy = s3store_destroy,
202 .submit = s3store_submit,
203 .cleanup = s3store_cleanup,
206 void bluesky_store_init_s3(void)
208 S3_initialize(NULL, S3_INIT_ALL);
209 bluesky_store_register(&store_impl, "s3");