int __s3fs_remove_object(const char *bucketName, const char *key) {
    S3_init();
    S3BucketContext bucketContext =
    {
        0,
        bucketName,
        protocolG,
        uriStyleG,
        accessKeyIdG,
        secretAccessKeyG
    };

    S3ResponseHandler responseHandler =
    { 
        0,
        &responseCompleteCallback
    };

    do {
        S3_delete_object(&bucketContext, key, 0, &responseHandler, 0);
    } while (S3_status_is_retryable(statusG) && should_retry());

    int result = statusG == S3StatusOK ? 0 : -1;

    if ((statusG != S3StatusOK) &&
        (statusG != S3StatusErrorPreconditionFailed)) {
        printError();
    }

    S3_deinitialize();

    return result;    
}
ssize_t __s3fs_get_object(const char *bucketName, const char *key, uint8_t **buf, 
                        ssize_t start_byte, ssize_t byte_count) {

    int64_t ifModifiedSince = -1, ifNotModifiedSince = -1;
    const char *ifMatch = 0, *ifNotMatch = 0;
    uint64_t startByte = start_byte, byteCount = byte_count;

    S3_init();

    struct get_callback_data get_context;
    get_context.buf = NULL;
    get_context.bytes_read = 0;
    
    S3BucketContext bucketContext =
    {
        0,
        bucketName,
        protocolG,
        uriStyleG,
        accessKeyIdG,
        secretAccessKeyG
    };

    S3GetConditions getConditions =
    {
        ifModifiedSince,
        ifNotModifiedSince,
        ifMatch,
        ifNotMatch
    };

    S3GetObjectHandler getObjectHandler =
    {
        { &responsePropertiesCallback, &responseCompleteCallback },
        &getObjectDataCallback
    };

    do {
        S3_get_object(&bucketContext, key, &getConditions, startByte,
                      byteCount, 0, &getObjectHandler, &get_context);
    } while (S3_status_is_retryable(statusG) && should_retry());

    ssize_t status = get_context.bytes_read;
    if (statusG != S3StatusOK) {
        status = -1;
        if (get_context.buf) {
            free (get_context.buf);
        }
        printError();
    } else {
        *buf = get_context.buf; 
    }

    S3_deinitialize();

    return status;
}
示例#3
0
int main(int argc, char** argv)
{
    if (2 > argc){
       usage();
       return 1;
    }

    char *slash = argv[1];

    while (*slash && (*slash != '/')) {
        slash++;
    }
    *slash++ = 0;

    const char *bucketName = argv[1];
    const char *key = slash;


    S3Protocol protocolG = S3ProtocolHTTP;
    const char* accessKeyIdG = getenv("S3_ACCESS_KEY_ID");
    const char* secretAccessKeyG = getenv("S3_SECRET_ACCESS_KEY");

    S3Status status;
    const char* hostname = getenv("S3_HOSTNAME");    
    if ((status = S3_initialize("s3", S3_INIT_ALL, hostname)) != S3StatusOK)
    {
        fprintf(stdout, "Failed to initialize libs3: %s\n", S3_get_status_name(status));
        exit(-1);
    }

    S3BucketContext bucketContext =
    {
        0,
        bucketName,
        protocolG,
        S3UriStylePath,
        accessKeyIdG,
        secretAccessKeyG,
        0
    };

    S3ResponseHandler responseHandler =
    { 
        0,
        &responseCompleteCallback
    };


    //----------------------create bucket-----------------//
    S3_delete_object(&bucketContext, key, 0, &responseHandler, 0);
    S3_deinitialize();

    return 0;
}
static int ct_s3_cleanup(void)
{
    int rc = 0;

    rc = ct_cleanup();
    if (rc == 0) {
        S3_deinitialize();
    }

    return rc;
}
示例#5
0
文件: lister.cpp 项目: ephemerr/s3bar
void list_bucket(const char *bucketName, const char *prefix,
                        const char *marker, const char *delimiter,
                        int maxkeys, const char* ak, const char* sk)
{

    S3_init();
    savedCommonPrefixes.clear();
    savedContents.clear();
    savedKeys.clear();

    S3BucketContext bucketContext =
    {
        0,
        bucketName,
        S3ProtocolHTTPS,
        S3UriStyleVirtualHost,
        ak,
        sk
    };

    S3ListBucketHandler listBucketHandler =
    {
        { &responsePropertiesCallback, &responseCompleteCallback },
        &listBucketCallback
    };

    list_bucket_callback_data data;

    snprintf(data.nextMarker, sizeof(data.nextMarker), "%s", marker);
    data.keyCount = 0;
    data.allDetails = 0;

    do {
        data.isTruncated = 0;
        do {
            S3_list_bucket(&bucketContext, prefix, data.nextMarker,
                           delimiter, maxkeys, 0, &listBucketHandler, &data);
        } while (S3_status_is_retryable((S3Status)statusG) && should_retry());
        if (statusG != S3StatusOK) {
            break;
        }
    } while (data.isTruncated && (!maxkeys || (data.keyCount < maxkeys)));

    if (statusG == S3StatusOK) {
//        if (!data.keyCount) {
//            printListBucketHeader(allDetails);
//        }
    }
    else {
        printError();
    }

    S3_deinitialize();
}
int main(int argc, char** args)
{
    if (3 > argc){
       usage();
       return 1;
    }

    const char* bucketName = args[1];
    const char* key = args[2];

    S3Protocol protocolG = S3ProtocolHTTP;
    const char* accessKeyIdG = getenv("S3_ACCESS_KEY_ID");
    const char* secretAccessKeyG = getenv("S3_SECRET_ACCESS_KEY");

    S3Status status;
    const char* hostname = getenv("S3_HOSTNAME");    
    if ((status = S3_initialize("s3", S3_INIT_ALL, hostname)) != S3StatusOK)
    {
        fprintf(stdout, "Failed to initialize libs3: %s\n", S3_get_status_name(status));
        exit(-1);
    }

    S3BucketContext bucketContext = 
    {
        0,
        bucketName,
        protocolG,
        S3UriStylePath,
        accessKeyIdG,
        secretAccessKeyG,
        0
    };

    S3ResponseHandler responseHandler = { &responsePropertiesCallback,
                                          &responseCompleteCallback
                                        };

    //----------------------check whether object is exist-----------------//
    S3_head_object(&bucketContext, key, 0, &responseHandler, 0);

    S3_deinitialize();

    return 0;
}
int __s3fs_test_bucket(const char *bucketName)
{
    S3_init();

    S3ResponseHandler responseHandler =
    {
        &responsePropertiesCallback, &responseCompleteCallback
    };

    char locationConstraint[64];
    do {
        S3_test_bucket(protocolG, uriStyleG, accessKeyIdG, secretAccessKeyG,
                       0, bucketName, sizeof(locationConstraint),
                       locationConstraint, 0, &responseHandler, 0);
    } while (S3_status_is_retryable(statusG) && should_retry());

    const char *reason = "Unknown";
    int result = statusG == S3StatusOK ? 1 : 0;

    switch (statusG) {
    case S3StatusOK:
        // bucket exists
        reason = locationConstraint[0] ? locationConstraint : "USA";
        break;
    case S3StatusErrorNoSuchBucket:
        reason = "Does Not Exist";
        break;
    case S3StatusErrorAccessDenied:
        reason = "Access Denied";
        break;
    default:
        break;
    }

    fprintf(stderr, "S3 test_bucket: %s\n", reason);

    S3_deinitialize();

    return result;
}
示例#8
0
文件: wcd.c 项目: flsafe/Webdir
static int bucket_exists(char *bname){

    s3_init();

    if (! load_settings()){
			printf("Configure your ~/.webdir-settings file!\n");
			exit(1);
		}

    char skey[STR];
    get_secret_key(skey);
    char akey[STR];
    get_access_key(akey);
		char hostname[STR];
		get_host(hostname);

    S3ResponseHandler res_handler = {
        &res_properties, &res_complete/* Can properties be null?*/
    };

    char loc_constraint[STR];
    do {
        S3_test_bucket(S3ProtocolHTTPS, 
                       S3UriStylePath, 
                       akey, 
                       skey,
                       hostname, 
                       bname, 
                       sizeof(loc_constraint),
                       loc_constraint, 
                       0, 
                       &res_handler, 
                       0);
    } while (S3_status_is_retryable(RequestStatus) && should_retry());


    S3_deinitialize();

		return RequestStatus == S3StatusOK; // Bucket exists
}
ssize_t __s3fs_put_object(const char *bucketName, const char *key, const uint8_t *buf, ssize_t contentLength)
{
    const char *cacheControl = 0, *contentType = 0, *md5 = 0;
    const char *contentDispositionFilename = 0, *contentEncoding = 0;
    int64_t expires = -1;
    S3CannedAcl cannedAcl = S3CannedAclPrivate;
    int metaPropertiesCount = 0;
    S3NameValue metaProperties[S3_MAX_METADATA_COUNT];
    int noStatus = 0;

    put_object_callback_data data;
    memset(&data, 0, sizeof(put_object_callback_data));
    data.data = buf;
    // data.gb = 0;
    data.noStatus = noStatus;

    data.contentLength = data.originalContentLength = contentLength;

    S3_init();
    
    S3BucketContext bucketContext =
    {
        0,
        bucketName,
        protocolG,
        uriStyleG,
        accessKeyIdG,
        secretAccessKeyG
    };

    S3PutProperties putProperties =
    {
        contentType,
        md5,
        cacheControl,
        contentDispositionFilename,
        contentEncoding,
        expires,
        cannedAcl,
        metaPropertiesCount,
        metaProperties
    };

    S3PutObjectHandler putObjectHandler =
    {
        { &responsePropertiesCallback, &responseCompleteCallback },
        &putObjectDataCallback
    };

    do {
        S3_put_object(&bucketContext, key, contentLength, &putProperties, 0,
                      &putObjectHandler, &data);
    } while (S3_status_is_retryable(statusG) && should_retry());

    int result = data.written;

    if (statusG != S3StatusOK) {
        printError();
        result = -1;
    }
    else if (data.contentLength) {
        fprintf(stderr, "\nERROR: Failed to read remaining %llu bytes from "
                "input\n", (unsigned long long) data.contentLength);
    }

    S3_deinitialize();
    return result;
}
int __s3fs_clear_bucket(const char *bucketName) {
    S3_init();

    const char *prefix = 0, *marker = 0, *delimiter = 0;
    int maxkeys = 0, allDetails = 0;
    
    S3BucketContext bucketContext =
    {
        0,
        bucketName,
        protocolG,
        uriStyleG,
        accessKeyIdG,
        secretAccessKeyG
    };

    S3ListBucketHandler listBucketHandler =
    {
        { &responsePropertiesCallback, &responseCompleteCallback },
        &traverseBucketCallback
    };

    traverse_bucket_callback_data data;

    snprintf(data.nextMarker, sizeof(data.nextMarker), "%s", marker);
    data.keyCount = 0;
    data.keylist = NULL;
    data.allDetails = allDetails;

    do {
        data.isTruncated = 0;
        do {
            S3_list_bucket(&bucketContext, prefix, data.nextMarker,
                           delimiter, maxkeys, 0, &listBucketHandler, &data);
        } while (S3_status_is_retryable(statusG) && should_retry());
        if (statusG != S3StatusOK) {
            break;
        }
    } while (data.isTruncated && (!maxkeys || (data.keyCount < maxkeys)));

    int rv = statusG == S3StatusOK ? 0 : -1;

    S3_deinitialize();

    struct node *klist = data.keylist;

    // try to remove objects
    if (rv == 0) {
        while (klist) {
            struct node *el = klist;
            int thisrv = __s3fs_remove_object(bucketName, el->key);
            if (thisrv < 0) {
                rv = -1;
            }
            klist = klist->next;
        }
    }

    // free keylist
    klist = data.keylist;
    while (klist) {
        struct node *el = klist;
        klist = klist->next;
        free(el->key);
        free(el);
    }

    return rv;
}
示例#11
0
int putFileIntoS3(char *fileName, char *s3ObjName)
{

  S3Status status;
  char *key;
  struct stat statBuf;
  uint64_t fileSize;
  FILE *fd;
  char *accessKeyId;
  char *secretAccessKey;
  put_object_callback_data data;


  accessKeyId = getenv("S3_ACCESS_KEY_ID");
  if (accessKeyId == NULL) {
    printf("S3_ACCESS_KEY_ID environment variable is undefined");
    return(-1);
  }

  secretAccessKey = getenv("S3_SECRET_ACCESS_KEY");
  if (secretAccessKey == NULL) {
    printf("S3_SECRET_ACCESS_KEY environment variable is undefined");
    return(-1);
  }

  key = (char *) strchr(s3ObjName, '/');
  if (key == NULL) {
    printf("S3 Key for the Object Not defined\n");
    return(-1);
  }
  *key = '\0';
  key++;
  if (stat(fileName, &statBuf) == -1) {
    printf("Unknown input file");
    return(-1);
  }
  fileSize = statBuf.st_size;

  fd = fopen(fileName, "r" );
  if (fd == NULL) {
    printf("Unable to open input file");
    return(-1);
  }
  data.infile = fd;


  S3BucketContext bucketContext =
    {s3ObjName,  1, 0, accessKeyId, secretAccessKey};
  S3PutObjectHandler putObjectHandler =
    {
      { &responsePropertiesCallback, &responseCompleteCallback },
      &putObjectDataCallback
    };


  if ((status = S3_initialize("s3", S3_INIT_ALL))
      != S3StatusOK) {
    printf("Failed to initialize libs3: %s\n",S3_get_status_name(status));
    return(-1);
  }

  S3_put_object(&bucketContext, key, fileSize, NULL, 0,
		&putObjectHandler, &data);
  if (statusG != S3StatusOK) {
    printf("Put failed: %i\n", statusG);
    S3_deinitialize();
    return(-1);
  }
  S3_deinitialize();

  fclose(fd);
  return(0);
}
void cloud_destroy() {
  S3_deinitialize();
}
示例#13
0
int main(int argc, char** argv)
{
    if (3 > argc){
       usage();
       return 1;
    }

    char *slash = argv[1];
    while (*slash && (*slash != '/')) {
        slash++;
    }
    if (!*slash || !*(slash + 1)) {
        fprintf(stderr, "\nERROR: Invalid bucket/key name: %s\n",
                argv[1]);
       usage();
       exit(-1);
    }
    *slash++ = 0;

    const char* bucketName = argv[1];
    const char *key = slash;
    const char *uploadId = 0;
    const char *filename = argv[2];
    uint64_t contentLength = 0;
    const char *cacheControl = 0, *contentType = 0, *md5 = 0;
    const char *contentDispositionFilename = 0, *contentEncoding = 0;
    int64_t expires = -1;
    S3CannedAcl cannedAcl = S3CannedAclPrivate;
    int metaPropertiesCount = 0;
    S3NameValue metaProperties[S3_MAX_METADATA_COUNT];
    char useServerSideEncryption = 0;
    int noStatus = 0;

    put_object_callback_data data;

    data.infile = 0;
    data.gb = 0;
    data.noStatus = noStatus;

    if (filename) {
        if (!contentLength) {
            struct stat statbuf;
            // Stat the file to get its length
            if (stat(filename, &statbuf) == -1) {
                fprintf(stderr, "\nERROR: Failed to stat file %s: ",
                        filename);
                perror(0);
                exit(-1);
            }
            contentLength = statbuf.st_size;
        }
        // Open the file
        if (!(data.infile = fopen(filename, "r" FOPEN_EXTRA_FLAGS))) {
            fprintf(stderr, "\nERROR: Failed to open input file %s: ",
                    filename);
            perror(0);
            exit(-1);
        }
    }
    else{
        usage();
    }
    data.contentLength = data.originalContentLength = contentLength;

    S3Protocol protocolG = S3ProtocolHTTP;
    const char* accessKeyIdG = getenv("S3_ACCESS_KEY_ID");
    const char* secretAccessKeyG = getenv("S3_SECRET_ACCESS_KEY");

    S3Status status;
    const char* hostname = getenv("S3_HOSTNAME");    
    if ((status = S3_initialize("s3", S3_INIT_ALL, hostname)) != S3StatusOK)
    {
        fprintf(stdout, "Failed to initialize libs3: %s\n", S3_get_status_name(status));
        exit(-1);
    }

    S3BucketContext bucketContext =
    {
        0,
        bucketName,
        protocolG,
        S3UriStylePath,
        accessKeyIdG,
        secretAccessKeyG,
        0
    };

    S3PutProperties putProperties =
    {
        contentType,
        md5,
        cacheControl,
        contentDispositionFilename,
        contentEncoding,
        expires,
        cannedAcl,
        metaPropertiesCount,
        metaProperties,
        useServerSideEncryption
    };

    S3PutObjectHandler putObjectHandler =
    {
        { &responsePropertiesCallback, &responseCompleteCallback },
        &putObjectDataCallback
    };
    //----------------------create bucket-----------------//
    S3_put_object(&bucketContext, key, contentLength, &putProperties, 0,
                          &putObjectHandler, &data);
    S3_deinitialize();

    return 0;
}