]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | .. _cpp: |
2 | ||
3 | C++ S3 Examples | |
4 | =============== | |
5 | ||
6 | Setup | |
7 | ----- | |
8 | ||
9 | The following contains includes and globals that will be used in later examples: | |
10 | ||
11 | .. code-block:: cpp | |
12 | ||
13 | #include "libs3.h" | |
14 | #include <stdlib.h> | |
15 | #include <iostream> | |
16 | #include <fstream> | |
17 | ||
18 | const char access_key[] = "ACCESS_KEY"; | |
19 | const char secret_key[] = "SECRET_KEY"; | |
20 | const char host[] = "HOST"; | |
21 | const char sample_bucket[] = "sample_bucket"; | |
22 | const char sample_key[] = "hello.txt"; | |
23 | const char sample_file[] = "resource/hello.txt"; | |
11fdf7f2 TL |
24 | const char *security_token = NULL; |
25 | const char *auth_region = NULL; | |
7c673cae FG |
26 | |
27 | S3BucketContext bucketContext = | |
28 | { | |
29 | host, | |
30 | sample_bucket, | |
31 | S3ProtocolHTTP, | |
32 | S3UriStylePath, | |
33 | access_key, | |
11fdf7f2 TL |
34 | secret_key, |
35 | security_token, | |
36 | auth_region | |
7c673cae FG |
37 | }; |
38 | ||
39 | S3Status responsePropertiesCallback( | |
40 | const S3ResponseProperties *properties, | |
41 | void *callbackData) | |
42 | { | |
43 | return S3StatusOK; | |
44 | } | |
45 | ||
46 | static void responseCompleteCallback( | |
47 | S3Status status, | |
48 | const S3ErrorDetails *error, | |
49 | void *callbackData) | |
50 | { | |
51 | return; | |
52 | } | |
53 | ||
54 | S3ResponseHandler responseHandler = | |
55 | { | |
56 | &responsePropertiesCallback, | |
57 | &responseCompleteCallback | |
58 | }; | |
59 | ||
60 | ||
61 | Creating (and Closing) a Connection | |
62 | ----------------------------------- | |
63 | ||
64 | This creates a connection so that you can interact with the server. | |
65 | ||
66 | .. code-block:: cpp | |
67 | ||
68 | S3_initialize("s3", S3_INIT_ALL, host); | |
69 | // Do stuff... | |
70 | S3_deinitialize(); | |
71 | ||
72 | ||
73 | Listing Owned Buckets | |
74 | --------------------- | |
75 | ||
76 | This gets a list of Buckets that you own. | |
77 | This also prints out the bucket name, owner ID, and display name | |
78 | for each bucket. | |
79 | ||
80 | .. code-block:: cpp | |
81 | ||
82 | static S3Status listServiceCallback( | |
83 | const char *ownerId, | |
84 | const char *ownerDisplayName, | |
85 | const char *bucketName, | |
86 | int64_t creationDate, void *callbackData) | |
87 | { | |
88 | bool *header_printed = (bool*) callbackData; | |
89 | if (!*header_printed) { | |
90 | *header_printed = true; | |
91 | printf("%-22s", " Bucket"); | |
92 | printf(" %-20s %-12s", " Owner ID", "Display Name"); | |
93 | printf("\n"); | |
94 | printf("----------------------"); | |
95 | printf(" --------------------" " ------------"); | |
96 | printf("\n"); | |
97 | } | |
98 | ||
99 | printf("%-22s", bucketName); | |
100 | printf(" %-20s %-12s", ownerId ? ownerId : "", ownerDisplayName ? ownerDisplayName : ""); | |
101 | printf("\n"); | |
102 | ||
103 | return S3StatusOK; | |
104 | } | |
105 | ||
106 | S3ListServiceHandler listServiceHandler = | |
107 | { | |
108 | responseHandler, | |
109 | &listServiceCallback | |
110 | }; | |
111 | bool header_printed = false; | |
11fdf7f2 TL |
112 | S3_list_service(S3ProtocolHTTP, access_key, secret_key, security_token, host, |
113 | auth_region, NULL, 0, &listServiceHandler, &header_printed); | |
7c673cae FG |
114 | |
115 | ||
116 | Creating a Bucket | |
117 | ----------------- | |
118 | ||
119 | This creates a new bucket. | |
120 | ||
121 | .. code-block:: cpp | |
122 | ||
123 | S3_create_bucket(S3ProtocolHTTP, access_key, secret_key, NULL, host, sample_bucket, S3CannedAclPrivate, NULL, NULL, &responseHandler, NULL); | |
124 | ||
125 | ||
126 | Listing a Bucket's Content | |
127 | -------------------------- | |
128 | ||
129 | This gets a list of objects in the bucket. | |
130 | This also prints out each object's name, the file size, and | |
131 | last modified date. | |
132 | ||
133 | .. code-block:: cpp | |
134 | ||
135 | static S3Status listBucketCallback( | |
136 | int isTruncated, | |
137 | const char *nextMarker, | |
138 | int contentsCount, | |
139 | const S3ListBucketContent *contents, | |
140 | int commonPrefixesCount, | |
141 | const char **commonPrefixes, | |
142 | void *callbackData) | |
143 | { | |
144 | printf("%-22s", " Object Name"); | |
145 | printf(" %-5s %-20s", "Size", " Last Modified"); | |
146 | printf("\n"); | |
147 | printf("----------------------"); | |
148 | printf(" -----" " --------------------"); | |
149 | printf("\n"); | |
150 | ||
151 | for (int i = 0; i < contentsCount; i++) { | |
152 | char timebuf[256]; | |
153 | char sizebuf[16]; | |
154 | const S3ListBucketContent *content = &(contents[i]); | |
155 | time_t t = (time_t) content->lastModified; | |
156 | ||
157 | strftime(timebuf, sizeof(timebuf), "%Y-%m-%dT%H:%M:%SZ", gmtime(&t)); | |
158 | sprintf(sizebuf, "%5llu", (unsigned long long) content->size); | |
159 | printf("%-22s %s %s\n", content->key, sizebuf, timebuf); | |
160 | } | |
161 | ||
162 | return S3StatusOK; | |
163 | } | |
164 | ||
165 | S3ListBucketHandler listBucketHandler = | |
166 | { | |
167 | responseHandler, | |
168 | &listBucketCallback | |
169 | }; | |
11fdf7f2 | 170 | S3_list_bucket(&bucketContext, NULL, NULL, NULL, 0, NULL, 0, &listBucketHandler, NULL); |
7c673cae FG |
171 | |
172 | The output will look something like this:: | |
173 | ||
174 | myphoto1.jpg 251262 2011-08-08T21:35:48.000Z | |
175 | myphoto2.jpg 262518 2011-08-08T21:38:01.000Z | |
176 | ||
177 | ||
178 | Deleting a Bucket | |
179 | ----------------- | |
180 | ||
181 | .. note:: | |
182 | ||
183 | The Bucket must be empty! Otherwise it won't work! | |
184 | ||
185 | .. code-block:: cpp | |
186 | ||
11fdf7f2 | 187 | S3_delete_bucket(S3ProtocolHTTP, S3UriStylePath, access_key, secret_key, 0, host, sample_bucket, NULL, NULL, 0, &responseHandler, NULL); |
7c673cae FG |
188 | |
189 | ||
190 | Creating an Object (from a file) | |
191 | -------------------------------- | |
192 | ||
193 | This creates a file ``hello.txt``. | |
194 | ||
195 | .. code-block:: cpp | |
196 | ||
197 | #include <sys/stat.h> | |
198 | typedef struct put_object_callback_data | |
199 | { | |
200 | FILE *infile; | |
201 | uint64_t contentLength; | |
202 | } put_object_callback_data; | |
203 | ||
204 | ||
205 | static int putObjectDataCallback(int bufferSize, char *buffer, void *callbackData) | |
206 | { | |
207 | put_object_callback_data *data = (put_object_callback_data *) callbackData; | |
208 | ||
209 | int ret = 0; | |
210 | ||
211 | if (data->contentLength) { | |
212 | int toRead = ((data->contentLength > (unsigned) bufferSize) ? (unsigned) bufferSize : data->contentLength); | |
213 | ret = fread(buffer, 1, toRead, data->infile); | |
214 | } | |
215 | data->contentLength -= ret; | |
216 | return ret; | |
217 | } | |
218 | ||
219 | put_object_callback_data data; | |
220 | struct stat statbuf; | |
221 | if (stat(sample_file, &statbuf) == -1) { | |
222 | fprintf(stderr, "\nERROR: Failed to stat file %s: ", sample_file); | |
223 | perror(0); | |
224 | exit(-1); | |
225 | } | |
226 | ||
227 | int contentLength = statbuf.st_size; | |
228 | data.contentLength = contentLength; | |
229 | ||
230 | if (!(data.infile = fopen(sample_file, "r"))) { | |
231 | fprintf(stderr, "\nERROR: Failed to open input file %s: ", sample_file); | |
232 | perror(0); | |
233 | exit(-1); | |
234 | } | |
235 | ||
236 | S3PutObjectHandler putObjectHandler = | |
237 | { | |
238 | responseHandler, | |
239 | &putObjectDataCallback | |
240 | }; | |
241 | ||
11fdf7f2 TL |
242 | S3_put_object(&bucketContext, sample_key, contentLength, NULL, NULL, 0, &putObjectHandler, &data); |
243 | fclose(data.infile); | |
7c673cae FG |
244 | |
245 | ||
246 | Download an Object (to a file) | |
247 | ------------------------------ | |
248 | ||
249 | This downloads a file and prints the contents. | |
250 | ||
251 | .. code-block:: cpp | |
252 | ||
253 | static S3Status getObjectDataCallback(int bufferSize, const char *buffer, void *callbackData) | |
254 | { | |
255 | FILE *outfile = (FILE *) callbackData; | |
256 | size_t wrote = fwrite(buffer, 1, bufferSize, outfile); | |
257 | return ((wrote < (size_t) bufferSize) ? S3StatusAbortedByCallback : S3StatusOK); | |
258 | } | |
259 | ||
260 | S3GetObjectHandler getObjectHandler = | |
261 | { | |
262 | responseHandler, | |
263 | &getObjectDataCallback | |
264 | }; | |
265 | FILE *outfile = stdout; | |
11fdf7f2 | 266 | S3_get_object(&bucketContext, sample_key, NULL, 0, 0, NULL, 0, &getObjectHandler, outfile); |
7c673cae FG |
267 | |
268 | ||
269 | Delete an Object | |
270 | ---------------- | |
271 | ||
272 | This deletes an object. | |
273 | ||
274 | .. code-block:: cpp | |
275 | ||
276 | S3ResponseHandler deleteResponseHandler = | |
277 | { | |
278 | 0, | |
279 | &responseCompleteCallback | |
280 | }; | |
11fdf7f2 | 281 | S3_delete_object(&bucketContext, sample_key, 0, 0, &deleteResponseHandler, 0); |
7c673cae FG |
282 | |
283 | ||
284 | Change an Object's ACL | |
285 | ---------------------- | |
286 | ||
287 | This changes an object's ACL to grant full control to another user. | |
288 | ||
289 | ||
290 | .. code-block:: cpp | |
291 | ||
292 | #include <string.h> | |
293 | char ownerId[] = "owner"; | |
294 | char ownerDisplayName[] = "owner"; | |
295 | char granteeId[] = "grantee"; | |
296 | char granteeDisplayName[] = "grantee"; | |
297 | ||
298 | S3AclGrant grants[] = { | |
299 | { | |
300 | S3GranteeTypeCanonicalUser, | |
301 | {{}}, | |
302 | S3PermissionFullControl | |
303 | }, | |
304 | { | |
305 | S3GranteeTypeCanonicalUser, | |
306 | {{}}, | |
307 | S3PermissionReadACP | |
308 | }, | |
309 | { | |
310 | S3GranteeTypeAllUsers, | |
311 | {{}}, | |
312 | S3PermissionRead | |
313 | } | |
314 | }; | |
315 | ||
316 | strncpy(grants[0].grantee.canonicalUser.id, ownerId, S3_MAX_GRANTEE_USER_ID_SIZE); | |
317 | strncpy(grants[0].grantee.canonicalUser.displayName, ownerDisplayName, S3_MAX_GRANTEE_DISPLAY_NAME_SIZE); | |
318 | ||
319 | strncpy(grants[1].grantee.canonicalUser.id, granteeId, S3_MAX_GRANTEE_USER_ID_SIZE); | |
320 | strncpy(grants[1].grantee.canonicalUser.displayName, granteeDisplayName, S3_MAX_GRANTEE_DISPLAY_NAME_SIZE); | |
321 | ||
322 | S3_set_acl(&bucketContext, sample_key, ownerId, ownerDisplayName, 3, grants, 0, &responseHandler, 0); | |
323 | ||
324 | ||
325 | Generate Object Download URL (signed) | |
326 | ------------------------------------- | |
327 | ||
328 | This generates a signed download URL that will be valid for 5 minutes. | |
329 | ||
330 | .. code-block:: cpp | |
331 | ||
332 | #include <time.h> | |
333 | char buffer[S3_MAX_AUTHENTICATED_QUERY_STRING_SIZE]; | |
334 | int64_t expires = time(NULL) + 60 * 5; // Current time + 5 minutes | |
335 | ||
11fdf7f2 | 336 | S3_generate_authenticated_query_string(buffer, &bucketContext, sample_key, expires, NULL, "GET"); |
7c673cae | 337 |