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; }
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 __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; }
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; }