#include #include #include #include #include #include #include #include #include #include #include "./libs3-2.0/inc/libs3.h" #include #include #include #include "s3_access.h" const char access_key[] = "LRDC5AOZ75ZVW4J9DIY7"; const char secret_key[] = "3nMbpA83GfL65jLPr712UMaH4c7/g+XHtQXyOlGx"; const char host[] = "svllabsg01-g1.eng.netapp.com:8082"; const char sample_bucket[] = "Bucket2"; const char sample_file[] = "/u/anubhavs/Workflow_Events"; static S3Protocol protocolG = S3ProtocolHTTPS; static S3UriStyle uriStyleG = S3UriStylePath; static S3Status statusG = S3StatusOK; static char errorDetailsG[4096] = { 0 }; static int retriesG = 5; S3BucketContext bucketContext = { host, sample_bucket, S3ProtocolHTTP, S3UriStylePath, access_key, secret_key }; S3Status responsePropertiesCallback( const S3ResponseProperties *properties, void *callbackData) { return S3StatusOK; } static void responseCompleteCallback( S3Status status, const S3ErrorDetails *error, void *callbackData) { return; } S3ResponseHandler responseHandler = { &responsePropertiesCallback, &responseCompleteCallback }; static S3Status listBucketCallback( int isTruncated, const char *nextMarker, int contentsCount, const S3ListBucketContent *contents, int commonPrefixesCount, const char **commonPrefixes, void *callbackData) { printf("%-22s", " Object Name"); printf(" %-5s %-20s", "Size", " Last Modified"); printf("\n"); printf("----------------------"); printf(" -----" " --------------------"); printf("\n"); for (int i = 0; i < contentsCount; i++) { char timebuf[256]; char sizebuf[16]; const S3ListBucketContent *content = &(contents[i]); time_t t = (time_t) content->lastModified; strftime(timebuf, sizeof(timebuf), "%Y-%m-%dT%H:%M:%SZ", gmtime(&t)); sprintf(sizebuf, "%5llu", (unsigned long long) content->size); printf("%-22s %s %s\n", content->key, sizebuf, timebuf); } return S3StatusOK; } S3ListBucketHandler listBucketHandler = { responseHandler, &listBucketCallback }; static void S3_init() { S3Status status; const char *hostname = getenv("S3_HOSTNAME"); if ((status = S3_initialize("s3", S3_INIT_ALL, hostname)) != S3StatusOK) { fprintf(stderr, "Failed to initialize libs3: %s\n", S3_get_status_name(status)); exit(-1); } } typedef struct list_service_data { int headerPrinted; int allDetails; } list_service_data; static void printListServiceHeader(int allDetails) { printf("%-56s %-20s", " Bucket", " Created"); if (allDetails) { printf(" %-64s %-12s", " Owner ID", "Display Name"); } printf("\n"); printf("-------------------------------------------------------- " "--------------------"); if (allDetails) { printf(" -------------------------------------------------" "--------------- ------------"); } printf("\n"); } static S3Status listServiceCallback(const char *ownerId, const char *ownerDisplayName, const char *bucketName, int64_t creationDate, void *callbackData) { list_service_data *data = (list_service_data *) callbackData; if (!data->headerPrinted) { data->headerPrinted = 1; printListServiceHeader(data->allDetails); } char timebuf[256]; if (creationDate >= 0) { time_t t = (time_t) creationDate; strftime(timebuf, sizeof(timebuf), "%Y-%m-%dT%H:%M:%SZ", gmtime(&t)); } else { timebuf[0] = 0; } printf("%-56s %-20s", bucketName, timebuf); if (data->allDetails) { printf(" %-64s %-12s", ownerId ? ownerId : "", ownerDisplayName ? ownerDisplayName : ""); } printf("\n"); return S3StatusOK; } #define SLEEP_UNITS_PER_SECOND 1 static int should_retry() { if (retriesG--) { // Sleep before next retry; start out with a 1 second sleep static int retrySleepInterval = 1 * SLEEP_UNITS_PER_SECOND; sleep(retrySleepInterval); // Next sleep 1 second longer retrySleepInterval++; return 1; } return 0; } static void printError() { if (statusG < S3StatusErrorAccessDenied) { fprintf(stderr, "\nERROR: %s\n", S3_get_status_name(statusG)); } else { fprintf(stderr, "\nERROR: %s\n", S3_get_status_name(statusG)); fprintf(stderr, "%s\n", errorDetailsG); } } static void list_service(int allDetails) { list_service_data data; data.headerPrinted = 0; data.allDetails = allDetails; S3_init(); S3ListServiceHandler listServiceHandler = { { &responsePropertiesCallback, &responseCompleteCallback }, &listServiceCallback }; do { S3_list_service(protocolG, access_key, secret_key, 0, 0, &listServiceHandler, &data); } while (S3_status_is_retryable(statusG) && should_retry()); if (statusG == S3StatusOK) { if (!data.headerPrinted) { printListServiceHeader(allDetails); } } else { printError(); } S3_deinitialize(); } #define BUFSIZE (128 * 1024) int main(void) { // S3_init(); // list_service(0); // S3_deinitialize(); S3Access *s3 = S3Access::instance(); std::string tempStr("Hello NetApp-987"); char buf[BUFSIZE]; if (NULL == s3){ std::cout<<"Unable to create s3_access object"<initialize(); s3->put_object("karthik987", tempStr.size(),(char *) tempStr.c_str(), NULL); int i; for (i=0; iput_object("/a/obj_128K", BUFSIZE, buf, NULL); s3->put_object("/a/obj_128K", BUFSIZE, buf, NULL); s3->put_object("/a/b/obj_128K", BUFSIZE, buf, NULL); s3->put_object("/c/b/obj_128K", BUFSIZE, buf, NULL); s3->put_object("/ab/c/obj_128K", BUFSIZE, buf, NULL); printf("\nlisting bucket all objects\n"); s3->list_bucket_objects(""); printf("\nlisting bucket objects /a\n"); s3->list_bucket_objects("/a"); printf("\nlisting bucket objects /a/\n"); s3->list_bucket_objects("/a/"); printf("\nGetting 20 bytes from the middle of an object\n"); s3->get_object_range("/a/obj_128K", 500, 20, NULL); printf("\nReading 30 bytes to see the modification\n"); s3->get_object_range("/a/obj_128K", 40, 30, NULL); printf("\nPutting 10 bytes into the middle of an object\n"); s3->put_object_range("/a/obj_128K", 50, 10, buf, NULL); printf("\nReading 30 bytes to see the modification\n"); s3->get_object_range("/a/obj_128K", 40, 30, NULL); s3->deinitialize(); delete(s3); }