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;
47 static S3Status s3store_get_handler(int bufferSize, const char *buffer,
50 struct get_info *info = (struct get_info *)callbackData;
51 g_string_append_len(info->buf, buffer, bufferSize);
55 static int s3store_put_handler(int bufferSize, char *buffer,
58 struct put_info *info = (struct put_info *)callbackData;
59 gint bytes = MIN(bufferSize, (int)(info->val->len - info->offset));
60 memcpy(buffer, (char *)info->val->data + info->offset, bytes);
61 info->offset += bytes;
65 static S3Status s3store_properties_callback(const S3ResponseProperties *properties,
71 static void s3store_response_callback(S3Status status,
72 const S3ErrorDetails *errorDetails,
75 struct get_info *info = (struct get_info *)callbackData;
81 if (errorDetails != NULL && errorDetails->message != NULL) {
82 g_print(" Error message: %s\n", errorDetails->message);
86 static void s3store_task(gpointer a, gpointer s)
88 BlueSkyStoreAsync *async = (BlueSkyStoreAsync *)a;
89 S3Store *store = (S3Store *)s;
91 async->status = ASYNC_RUNNING;
92 async->exec_time = bluesky_now_hires();
94 if (async->op == STORE_OP_GET) {
96 info.buf = g_string_new("");
99 struct S3GetObjectHandler handler;
100 handler.responseHandler.propertiesCallback = s3store_properties_callback;
101 handler.responseHandler.completeCallback = s3store_response_callback;
102 handler.getObjectDataCallback = s3store_get_handler;
104 S3_get_object(&store->bucket, async->key, NULL, 0, 0, NULL,
108 async->data = bluesky_string_new_from_gstring(info.buf);
111 g_string_free(info.buf, TRUE);
114 } else if (async->op == STORE_OP_PUT) {
115 struct put_info info;
116 info.val = async->data;
119 struct S3PutObjectHandler handler;
120 handler.responseHandler.propertiesCallback
121 = s3store_properties_callback;
122 handler.responseHandler.completeCallback = s3store_response_callback;
123 handler.putObjectDataCallback = s3store_put_handler;
125 S3_put_object(&store->bucket, async->key, async->data->len, NULL, NULL,
131 bluesky_store_async_mark_complete(async);
132 bluesky_store_async_unref(async);
135 static S3Status s3store_list_handler(int isTruncated,
136 const char *nextMarker,
138 const S3ListBucketContent *contents,
139 int commonPrefixesCount,
140 const char **commonPrefixes,
143 struct list_info *info = (struct list_info *)callbackData;
144 if (contentsCount > 0) {
145 g_free(info->last_entry);
146 info->last_entry = g_strdup(contents[contentsCount - 1].key);
151 static char *s3store_lookup_last(gpointer s, const char *prefix)
153 S3Store *store = (S3Store *)s;
154 struct list_info info = {0, NULL};
156 struct S3ListBucketHandler handler;
157 handler.responseHandler.propertiesCallback
158 = s3store_properties_callback;
159 handler.responseHandler.completeCallback = s3store_response_callback;
160 handler.listBucketCallback = s3store_list_handler;
164 S3_list_bucket(&store->bucket, prefix, marker, NULL, 1024, NULL, &handler, &info);
166 g_print("Last key: %s\n", info.last_entry);
168 return info.last_entry;
171 static gpointer s3store_new(const gchar *path)
173 S3Store *store = g_new(S3Store, 1);
174 store->thread_pool = g_thread_pool_new(s3store_task, store, -1, FALSE,
176 store->bucket.bucketName = "mvrable-bluesky";
177 store->bucket.protocol = S3ProtocolHTTP;
178 store->bucket.uriStyle = S3UriStylePath;
179 store->bucket.accessKeyId = getenv("AWS_ACCESS_KEY_ID");
180 store->bucket.secretAccessKey = getenv("AWS_SECRET_ACCESS_KEY");
182 const char *key = getenv("BLUESKY_KEY");
184 g_error("Encryption key not defined; please set BLUESKY_KEY environment variable");
188 bluesky_crypt_hash_key(key, store->encryption_key);
190 g_print("Initializing S3 with bucket %s, access key %s, encryption key %s\n",
191 store->bucket.bucketName, store->bucket.accessKeyId, key);
196 static void s3store_destroy(gpointer store)
201 static void s3store_submit(gpointer s, BlueSkyStoreAsync *async)
203 S3Store *store = (S3Store *)s;
204 g_return_if_fail(async->status == ASYNC_NEW);
205 g_return_if_fail(async->op != STORE_OP_NONE);
210 async->status = ASYNC_PENDING;
211 bluesky_store_async_ref(async);
212 g_thread_pool_push(store->thread_pool, async, NULL);
216 g_warning("Uknown operation type for S3Store: %d\n", async->op);
217 bluesky_store_async_mark_complete(async);
222 static void s3store_cleanup(gpointer store, BlueSkyStoreAsync *async)
224 GString *buf = (GString *)async->store_private;
227 g_string_free(buf, TRUE);
228 async->store_private = NULL;
232 static BlueSkyStoreImplementation store_impl = {
233 .create = s3store_new,
234 .destroy = s3store_destroy,
235 .submit = s3store_submit,
236 .cleanup = s3store_cleanup,
237 .lookup_last = s3store_lookup_last,
240 void bluesky_store_init_s3(void)
242 S3_initialize(NULL, S3_INIT_ALL);
243 bluesky_store_register(&store_impl, "s3");