|
| 1 | +#include <cstdlib> |
| 2 | +#include <cstring> |
| 3 | +#include <s3api.h> |
| 4 | +#include <s3cpp/s3.h> |
| 5 | + |
| 6 | +extern "C" { |
| 7 | + |
| 8 | +s3_client* s3_client_create(const char* access_key, const char* secret_key) { |
| 9 | + return reinterpret_cast<s3_client*>(new S3Client(access_key, secret_key)); |
| 10 | +} |
| 11 | + |
| 12 | +s3_client* s3_client_create_with_region(const char* access_key, const char* secret_key, const char* region) { |
| 13 | + return reinterpret_cast<s3_client*>(new S3Client(access_key, secret_key, region)); |
| 14 | +} |
| 15 | + |
| 16 | +s3_client* s3_client_create_with_endpoint(const char* access_key, const char* secret_key, const char* endpoint, s3_addressing_style style) { |
| 17 | + auto cpp_style = static_cast<S3AddressingStyle>(style); |
| 18 | + return reinterpret_cast<s3_client*>(new S3Client(access_key, secret_key, endpoint, cpp_style)); |
| 19 | +} |
| 20 | + |
| 21 | +void s3_client_destroy(s3_client* client) { |
| 22 | + delete reinterpret_cast<S3Client*>(client); |
| 23 | +} |
| 24 | + |
| 25 | +s3_list_buckets_response list_buckets(s3_client* client, const s3_list_buckets_options* options) { |
| 26 | + S3Client* s3_client = reinterpret_cast<S3Client*>(client); |
| 27 | + |
| 28 | + ListBucketsInput input; |
| 29 | + if (options) { |
| 30 | + if (options->bucket_region) |
| 31 | + input.BucketRegion = options->bucket_region; |
| 32 | + if (options->continuation_token) |
| 33 | + input.ContinuationToken = options->continuation_token; |
| 34 | + if (options->max_buckets) |
| 35 | + input.MaxBuckets = *options->max_buckets; |
| 36 | + if (options->prefix) |
| 37 | + input.Prefix = options->prefix; |
| 38 | + } |
| 39 | + |
| 40 | + std::expected<ListAllMyBucketsResult, Error> result = s3_client->ListBuckets(input); |
| 41 | + |
| 42 | + s3_list_buckets_response response {}; |
| 43 | + if (result.has_value()) { |
| 44 | + response.is_error = false; |
| 45 | + const auto& cpp_result = result.value(); |
| 46 | + |
| 47 | + response.data.result.bucket_count = cpp_result.Buckets.size(); |
| 48 | + response.data.result.buckets = static_cast<s3_bucket*>(malloc(sizeof(s3_bucket) * cpp_result.Buckets.size())); |
| 49 | + |
| 50 | + for (size_t i = 0; i < cpp_result.Buckets.size(); ++i) { |
| 51 | + const auto& cpp_bucket = cpp_result.Buckets[i]; |
| 52 | + response.data.result.buckets[i].bucket_arn = strdup(cpp_bucket.BucketARN.c_str()); |
| 53 | + response.data.result.buckets[i].bucket_region = strdup(cpp_bucket.BucketRegion.c_str()); |
| 54 | + response.data.result.buckets[i].creation_date = strdup(cpp_bucket.CreationDate.c_str()); |
| 55 | + response.data.result.buckets[i].name = strdup(cpp_bucket.Name.c_str()); |
| 56 | + } |
| 57 | + |
| 58 | + response.data.result.owner.display_name = strdup(cpp_result.Owner.DisplayName.c_str()); |
| 59 | + response.data.result.owner.id = strdup(cpp_result.Owner.ID.c_str()); |
| 60 | + response.data.result.continuation_token = cpp_result.ContinuationToken.empty() ? nullptr : strdup(cpp_result.ContinuationToken.c_str()); |
| 61 | + response.data.result.prefix = cpp_result.Prefix.empty() ? nullptr : strdup(cpp_result.Prefix.c_str()); |
| 62 | + } else { |
| 63 | + response.is_error = true; |
| 64 | + const auto& cpp_error = result.error(); |
| 65 | + response.data.error.code = strdup(cpp_error.Code.c_str()); |
| 66 | + response.data.error.message = strdup(cpp_error.Message.c_str()); |
| 67 | + response.data.error.resource = strdup(cpp_error.Resource.c_str()); |
| 68 | + response.data.error.request_id = cpp_error.RequestId; |
| 69 | + } |
| 70 | + |
| 71 | + return response; |
| 72 | +} |
| 73 | + |
| 74 | +void s3_free_string(char* str) { |
| 75 | + if (str) { |
| 76 | + free(str); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +void s3_free_error(s3_error* error) { |
| 81 | + if (!error) |
| 82 | + return; |
| 83 | + s3_free_string(error->code); |
| 84 | + s3_free_string(error->message); |
| 85 | + s3_free_string(error->resource); |
| 86 | +} |
| 87 | + |
| 88 | +void s3_free_list_buckets_result(s3_list_buckets_result* result) { |
| 89 | + if (!result) |
| 90 | + return; |
| 91 | + |
| 92 | + for (size_t i = 0; i < result->bucket_count; ++i) { |
| 93 | + s3_free_string(result->buckets[i].bucket_arn); |
| 94 | + s3_free_string(result->buckets[i].bucket_region); |
| 95 | + s3_free_string(result->buckets[i].creation_date); |
| 96 | + s3_free_string(result->buckets[i].name); |
| 97 | + } |
| 98 | + free(result->buckets); |
| 99 | + |
| 100 | + s3_free_string(result->owner.display_name); |
| 101 | + s3_free_string(result->owner.id); |
| 102 | + s3_free_string(result->continuation_token); |
| 103 | + s3_free_string(result->prefix); |
| 104 | +} |
| 105 | + |
| 106 | +void s3_free_list_buckets_response(s3_list_buckets_response* response) { |
| 107 | + if (!response) |
| 108 | + return; |
| 109 | + if (response->is_error) { |
| 110 | + s3_free_error(&response->data.error); |
| 111 | + } else { |
| 112 | + s3_free_list_buckets_result(&response->data.result); |
| 113 | + } |
| 114 | +} |
| 115 | +} |
0 commit comments