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. */
19 /* Simple in-memory data store for test purposes. */
21 S3BucketContext bucket;
22 uint8_t encryption_key[CRYPTO_KEY_SIZE];
25 static gpointer s3store_new()
27 S3Store *store = g_new(S3Store, 1);
28 store->bucket.bucketName = "mvrable-bluesky";
29 store->bucket.protocol = S3ProtocolHTTP;
30 store->bucket.uriStyle = S3UriStylePath;
31 store->bucket.accessKeyId = getenv("AWS_ACCESS_KEY_ID");
32 store->bucket.secretAccessKey = getenv("AWS_SECRET_ACCESS_KEY");
34 const char *key = getenv("BLUESKY_KEY");
36 g_error("Encryption key not defined; please set BLUESKY_KEY environment variable");
40 bluesky_crypt_hash_key(key, store->encryption_key);
42 g_print("Initializing S3 with bucket %s, access key %s, encryption key %s\n",
43 store->bucket.bucketName, store->bucket.accessKeyId, key);
48 static void s3store_destroy(gpointer store)
58 static S3Status s3store_get_handler(int bufferSize, const char *buffer,
61 struct get_info *info = (struct get_info *)callbackData;
62 gint bytes = MIN(bufferSize, (int)(BLUESKY_BLOCK_SIZE - info->offset));
63 memcpy(info->buf + info->offset, buffer, bytes);
64 info->offset += bytes;
73 static int s3store_put_handler(int bufferSize, char *buffer,
76 struct put_info *info = (struct put_info *)callbackData;
77 gint bytes = MIN(bufferSize, (int)(info->val->len - info->offset));
78 memcpy(buffer, (char *)info->val->data + info->offset, bytes);
79 info->offset += bytes;
83 static S3Status s3store_properties_callback(const S3ResponseProperties *properties,
86 g_print("(Properties callback)\n");
90 static void s3store_response_callback(S3Status status,
91 const S3ErrorDetails *errorDetails,
94 g_print("S3 operation complete, status=%s, now=%ld\n",
95 S3_get_status_name(status), bluesky_now_hires());
96 if (errorDetails != NULL) {
97 g_print(" Error message: %s\n", errorDetails->message);
101 static BlueSkyRCStr *s3store_get(gpointer s, const gchar *key)
103 S3Store *store = (S3Store *)s;
105 struct get_info info;
106 info.buf = (char *)g_malloc0(BLUESKY_BLOCK_SIZE);
109 struct S3GetObjectHandler handler;
110 handler.responseHandler.propertiesCallback = s3store_properties_callback;
111 handler.responseHandler.completeCallback = s3store_response_callback;
112 handler.getObjectDataCallback = s3store_get_handler;
114 g_print("Starting fetch of %s from S3...\n", key);
115 S3_get_object(&store->bucket, key, NULL, 0, 0, NULL,
118 BlueSkyRCStr *raw, *decrypted;
119 raw = bluesky_string_new(info.buf, BLUESKY_BLOCK_SIZE);
120 decrypted = bluesky_crypt_decrypt(raw, store->encryption_key);
121 bluesky_string_unref(raw);
125 static void s3store_put(gpointer s, const gchar *key, BlueSkyRCStr *val)
127 S3Store *store = (S3Store *)s;
128 BlueSkyRCStr *encrypted = bluesky_crypt_encrypt(val, store->encryption_key);
130 struct put_info info;
131 info.val = encrypted;
134 struct S3PutObjectHandler handler;
135 handler.responseHandler.propertiesCallback = s3store_properties_callback;
136 handler.responseHandler.completeCallback = s3store_response_callback;
137 handler.putObjectDataCallback = s3store_put_handler;
139 g_print("Starting store of %s to S3 at %ld...\n", key, bluesky_now_hires());
140 S3_put_object(&store->bucket, key, encrypted->len, NULL, NULL,
143 /* TODO: unref encrypted */
146 static BlueSkyStoreImplementation store_impl = {
147 .create = s3store_new,
148 .destroy = s3store_destroy,
153 void bluesky_store_init_s3(void)
155 S3_initialize(NULL, S3_INIT_ALL);
156 bluesky_store_register(&store_impl, "s3");