9 The following contains includes and globals that will be used in later examples:
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";
25 S3BucketContext bucketContext =
35 S3Status responsePropertiesCallback(
36 const S3ResponseProperties *properties,
42 static void responseCompleteCallback(
44 const S3ErrorDetails *error,
50 S3ResponseHandler responseHandler =
52 &responsePropertiesCallback,
53 &responseCompleteCallback
57 Creating (and Closing) a Connection
58 -----------------------------------
60 This creates a connection so that you can interact with the server.
64 S3_initialize("s3", S3_INIT_ALL, host);
72 This gets a list of Buckets that you own.
73 This also prints out the bucket name, owner ID, and display name
78 static S3Status listServiceCallback(
80 const char *ownerDisplayName,
81 const char *bucketName,
82 int64_t creationDate, void *callbackData)
84 bool *header_printed = (bool*) callbackData;
85 if (!*header_printed) {
86 *header_printed = true;
87 printf("%-22s", " Bucket");
88 printf(" %-20s %-12s", " Owner ID", "Display Name");
90 printf("----------------------");
91 printf(" --------------------" " ------------");
95 printf("%-22s", bucketName);
96 printf(" %-20s %-12s", ownerId ? ownerId : "", ownerDisplayName ? ownerDisplayName : "");
102 S3ListServiceHandler listServiceHandler =
107 bool header_printed = false;
108 S3_list_service(S3ProtocolHTTP, access_key, secret_key, host, 0, NULL, &listServiceHandler, &header_printed);
114 This creates a new bucket.
118 S3_create_bucket(S3ProtocolHTTP, access_key, secret_key, NULL, host, sample_bucket, S3CannedAclPrivate, NULL, NULL, &responseHandler, NULL);
121 Listing a Bucket's Content
122 --------------------------
124 This gets a list of objects in the bucket.
125 This also prints out each object's name, the file size, and
130 static S3Status listBucketCallback(
132 const char *nextMarker,
134 const S3ListBucketContent *contents,
135 int commonPrefixesCount,
136 const char **commonPrefixes,
139 printf("%-22s", " Object Name");
140 printf(" %-5s %-20s", "Size", " Last Modified");
142 printf("----------------------");
143 printf(" -----" " --------------------");
146 for (int i = 0; i < contentsCount; i++) {
149 const S3ListBucketContent *content = &(contents[i]);
150 time_t t = (time_t) content->lastModified;
152 strftime(timebuf, sizeof(timebuf), "%Y-%m-%dT%H:%M:%SZ", gmtime(&t));
153 sprintf(sizebuf, "%5llu", (unsigned long long) content->size);
154 printf("%-22s %s %s\n", content->key, sizebuf, timebuf);
160 S3ListBucketHandler listBucketHandler =
165 S3_list_bucket(&bucketContext, NULL, NULL, NULL, 0, NULL, &listBucketHandler, NULL);
167 The output will look something like this::
169 myphoto1.jpg 251262 2011-08-08T21:35:48.000Z
170 myphoto2.jpg 262518 2011-08-08T21:38:01.000Z
178 The Bucket must be empty! Otherwise it won't work!
182 S3_delete_bucket(S3ProtocolHTTP, S3UriStylePath, access_key, secret_key, host, sample_bucket, NULL, &responseHandler, NULL);
185 Creating an Object (from a file)
186 --------------------------------
188 This creates a file ``hello.txt``.
192 #include <sys/stat.h>
193 typedef struct put_object_callback_data
196 uint64_t contentLength;
197 } put_object_callback_data;
200 static int putObjectDataCallback(int bufferSize, char *buffer, void *callbackData)
202 put_object_callback_data *data = (put_object_callback_data *) callbackData;
206 if (data->contentLength) {
207 int toRead = ((data->contentLength > (unsigned) bufferSize) ? (unsigned) bufferSize : data->contentLength);
208 ret = fread(buffer, 1, toRead, data->infile);
210 data->contentLength -= ret;
214 put_object_callback_data data;
216 if (stat(sample_file, &statbuf) == -1) {
217 fprintf(stderr, "\nERROR: Failed to stat file %s: ", sample_file);
222 int contentLength = statbuf.st_size;
223 data.contentLength = contentLength;
225 if (!(data.infile = fopen(sample_file, "r"))) {
226 fprintf(stderr, "\nERROR: Failed to open input file %s: ", sample_file);
231 S3PutObjectHandler putObjectHandler =
234 &putObjectDataCallback
237 S3_put_object(&bucketContext, sample_key, contentLength, NULL, NULL, &putObjectHandler, &data);
240 Download an Object (to a file)
241 ------------------------------
243 This downloads a file and prints the contents.
247 static S3Status getObjectDataCallback(int bufferSize, const char *buffer, void *callbackData)
249 FILE *outfile = (FILE *) callbackData;
250 size_t wrote = fwrite(buffer, 1, bufferSize, outfile);
251 return ((wrote < (size_t) bufferSize) ? S3StatusAbortedByCallback : S3StatusOK);
254 S3GetObjectHandler getObjectHandler =
257 &getObjectDataCallback
259 FILE *outfile = stdout;
260 S3_get_object(&bucketContext, sample_key, NULL, 0, 0, NULL, &getObjectHandler, outfile);
266 This deletes an object.
270 S3ResponseHandler deleteResponseHandler =
273 &responseCompleteCallback
275 S3_delete_object(&bucketContext, sample_key, 0, &deleteResponseHandler, 0);
278 Change an Object's ACL
279 ----------------------
281 This changes an object's ACL to grant full control to another user.
287 char ownerId[] = "owner";
288 char ownerDisplayName[] = "owner";
289 char granteeId[] = "grantee";
290 char granteeDisplayName[] = "grantee";
292 S3AclGrant grants[] = {
294 S3GranteeTypeCanonicalUser,
296 S3PermissionFullControl
299 S3GranteeTypeCanonicalUser,
304 S3GranteeTypeAllUsers,
310 strncpy(grants[0].grantee.canonicalUser.id, ownerId, S3_MAX_GRANTEE_USER_ID_SIZE);
311 strncpy(grants[0].grantee.canonicalUser.displayName, ownerDisplayName, S3_MAX_GRANTEE_DISPLAY_NAME_SIZE);
313 strncpy(grants[1].grantee.canonicalUser.id, granteeId, S3_MAX_GRANTEE_USER_ID_SIZE);
314 strncpy(grants[1].grantee.canonicalUser.displayName, granteeDisplayName, S3_MAX_GRANTEE_DISPLAY_NAME_SIZE);
316 S3_set_acl(&bucketContext, sample_key, ownerId, ownerDisplayName, 3, grants, 0, &responseHandler, 0);
319 Generate Object Download URL (signed)
320 -------------------------------------
322 This generates a signed download URL that will be valid for 5 minutes.
327 char buffer[S3_MAX_AUTHENTICATED_QUERY_STRING_SIZE];
328 int64_t expires = time(NULL) + 60 * 5; // Current time + 5 minutes
330 S3_generate_authenticated_query_string(buffer, &bucketContext, sample_key, expires, NULL);