-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.cpp
76 lines (67 loc) · 2.44 KB
/
client.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <argp.h>
#include <minioclient.h>
#include <iostream>
#include <string>
const char *argp_program_version = "minioclient-cpp";
const char *argp_program_bug_address = "<[email protected]>";
static char doc[] = "minioclient-cpp test";
static char args_doc[] = "";
static struct argp_option options[] = {
{"endpoint", 'm', "http[s]://<host>:<port>", 0, "Minio server endpoint"},
{"access", 'a', "ACCESSKEY", 0, "Minio access key"},
{"secret", 'k', "SECRETKEY", 0, "Minio secret key"},
{0}};
struct Arguments {
std::string minioendpoint, minioaccesskey, miniosecret;
};
//------------------------------------------------------------------------------
static error_t parse_opt(int key, char *arg, struct argp_state *state) {
Arguments *args = (Arguments *)state->input;
switch (key) {
case 'm':
args->minioendpoint = arg;
break;
case 'a':
args->minioaccesskey = arg;
break;
case 'k':
args->miniosecret = arg;
break;
default:
return ARGP_ERR_UNKNOWN;
};
return 0;
}
//------------------------------------------------------------------------------
int main(int argc, char **argv) {
Arguments args;
struct argp argp = {options, parse_opt, 0, doc};
argp_parse(&argp, argc, argv, 0, 0, &args);
if (args.minioendpoint.empty() || args.minioaccesskey.empty() ||
args.miniosecret.empty()) {
std::cerr << "All arguments required: -m http[s]://<host>:<port> -a "
"<ACCESSKEY> -k <SECRETKEY>"
<< std::endl;
return 1;
}
try {
MinioClient minioClient(args.minioendpoint, args.minioaccesskey,
args.miniosecret);
minioClient.traceOn(std::cout);
std::string bucket("asiatripy");
bool isExist = minioClient.bucketExists(bucket);
if (isExist) {
std::cout << "Bucket already exists." << std::endl;
} else {
minioClient.makeBucket(bucket);
}
std::string filepath = "client.cpp", remotename = "content.txt";
minioClient.putObject(bucket, remotename, filepath);
std::cout << filepath + " is successfully uploaded as '" + remotename +
"' to '" + bucket + "' bucket."
<< std::endl;
} catch (const std::exception &e) {
std::cerr << e.what() << std::endl;
}
return 0;
}