// TODO Copyright #ifndef _S3_ACCESS_H_ #define _S3_ACCESS_H_ #include #include #include #include #include #include "uuid/uuid.h" #include "smagent/sma_interface.h" #include "./libs3-2.0/inc/libs3.h" #include #include #include #include #define MAX_VOLID_SIZE 256 #define MAX_SNAPID_SIZE sizeof(uint64_t) #define UUID_SIZE (sizeof(uuid_t)) #define S3_PRINT(fmt, ...) \ do { \ fprintf(stdout, "%s():%d: " fmt, \ __func__, __LINE__, \ ##__VA_ARGS__); \ fflush(stdout); \ } while(0) #define S3_ERR_PRINT(fmt, ...) \ do { \ fprintf(stderr, "%s():%d: " fmt, \ __func__, __LINE__, \ ##__VA_ARGS__); \ fflush(stderr); \ } while(0) #ifdef DEBUG #define S3_DEBUG_PRINT(fmt, ...) \ do { \ fprintf(stdout, "%s():%d: " fmt, \ __func__, __LINE__, \ ##__VA_ARGS__); \ fflush(stdout); \ } while(0) #else /* DEBUG */ #define S3_DEBUG_PRINT(fmt, ...) #endif /* DEBUG */ class S3Access { private: // singleton Btrfs Mgr instance static S3Access *instance_; static const std::string access_key; static const std::string host; static const std::string bucket_name; static const std::string secret_key; static const S3Protocol protocolG; static const S3UriStyle uriStyleG; S3Status statusG; char errorDetailsG[4096]; int retriesG; bool header_printed; typedef struct list_service_data { int headerPrinted; int allDetails; } list_service_data; struct put_object_callback_data { char *buff; int64_t totalContentLength, pendingContentLength; }; struct get_object_callback_data { char *buff; uint64_t totalContentLength; }; typedef struct list_bucket_callback_data { int isTruncated; char nextMarker[1024]; int keyCount; int allDetails; } list_bucket_callback_data; struct growbuffer { // The total number of bytes, and the start byte int size; // The start byte int start; // The blocks char data[64 * 1024]; struct growbuffer *prev, *next; }; S3BucketContext bucketContext; S3ResponseHandler responseHandler; S3ListServiceHandler listServiceHandler; S3ListBucketHandler listBucketHandler; S3PutObjectHandler putObjectHandler; S3ResponseHandler deleteResponseHandler; S3GetObjectHandler getObjectHandler; // make default constructors private S3Access(); S3Access(S3Access const&); int should_retry(); void resetRetryCount(); void printError(); void list_bucket(const char *bucketName, const char *prefix, const char *marker, const char *delimiter, int maxkeys, int allDetails); void printListBucketHeader(int allDetails); static void printListServiceHeader(int allDetails); static uint64_t convertInt(const char *str, const char *paramName); // Call Back Functions static S3Status responsePropertiesCallback( const S3ResponseProperties *properties, void *callbackData); static void responseCompleteCallback( S3Status status, const S3ErrorDetails *error, void *callbackData); static S3Status listServiceCallback( const char *ownerId, const char *ownerDisplayName, const char *bucketName, int64_t creationDate, void *callbackData); static S3Status listBucketCallback( int isTruncated, const char *nextMarker, int contentsCount, const S3ListBucketContent *contents, int commonPrefixesCount, const char **commonPrefixes, void *callbackData); static int putObjectDataCallback(int bufferSize, char *buffer, void *callbackData); static S3Status getObjectDataCallback(int bufferSize, const char *buffer, void *callbackData); public: static S3Access *instance() { if (!instance_) { instance_ = new S3Access(); printf("s3 instance created\n"); } return instance_; } static void free_instance() { if (instance_) { delete instance_; instance_ = NULL; } } ~S3Access() {} static void initialize(); static void deinitialize(); void list_all_buckets(int allDetails); // CREATING AN OBJECT std::string put_object_range(std::string key, uint64_t offset, uint64_t contentLength, char *data, void *response_func); std::string put_object(std::string key, uint64_t contentLength, char *data, void *response_func); // DOWNLOAD AN OBJECT int get_object(std::string key, char * object_buffer, uint64_t *size); int get_object_range(std::string key, uint64_t offset, uint64_t length, char * object_buffer); //LISTING A BUCKETS CONTENT std::vector list_bucket_objects(std::string prefix); //GET SIZE OF OBJECT uint64_t get_object_size(std::string key); // DELETE AN OBJECT bool delete_object(std::string key); }; // class S3Access #endif // _S3_ACCESS_H_