C++ S3 Examples

Setup

The following contains includes and globals that will be used in later examples:

  1. #include "libs3.h"
  2. #include <stdlib.h>
  3. #include <iostream>
  4. #include <fstream>
  5. const char access_key[] = "ACCESS_KEY";
  6. const char secret_key[] = "SECRET_KEY";
  7. const char host[] = "HOST";
  8. const char sample_bucket[] = "sample_bucket";
  9. const char sample_key[] = "hello.txt";
  10. const char sample_file[] = "resource/hello.txt";
  11. const char *security_token = NULL;
  12. const char *auth_region = NULL;
  13. S3BucketContext bucketContext =
  14. {
  15. host,
  16. sample_bucket,
  17. S3ProtocolHTTP,
  18. S3UriStylePath,
  19. access_key,
  20. secret_key,
  21. security_token,
  22. auth_region
  23. };
  24. S3Status responsePropertiesCallback(
  25. const S3ResponseProperties *properties,
  26. void *callbackData)
  27. {
  28. return S3StatusOK;
  29. }
  30. static void responseCompleteCallback(
  31. S3Status status,
  32. const S3ErrorDetails *error,
  33. void *callbackData)
  34. {
  35. return;
  36. }
  37. S3ResponseHandler responseHandler =
  38. {
  39. &responsePropertiesCallback,
  40. &responseCompleteCallback
  41. };

Creating (and Closing) a Connection

This creates a connection so that you can interact with the server.

  1. S3_initialize("s3", S3_INIT_ALL, host);
  2. // Do stuff...
  3. S3_deinitialize();

Listing Owned Buckets

This gets a list of Buckets that you own. This also prints out the bucket name, owner ID, and display name for each bucket.

  1. static S3Status listServiceCallback(
  2. const char *ownerId,
  3. const char *ownerDisplayName,
  4. const char *bucketName,
  5. int64_t creationDate, void *callbackData)
  6. {
  7. bool *header_printed = (bool*) callbackData;
  8. if (!*header_printed) {
  9. *header_printed = true;
  10. printf("%-22s", " Bucket");
  11. printf(" %-20s %-12s", " Owner ID", "Display Name");
  12. printf("\n");
  13. printf("----------------------");
  14. printf(" --------------------" " ------------");
  15. printf("\n");
  16. }
  17. printf("%-22s", bucketName);
  18. printf(" %-20s %-12s", ownerId ? ownerId : "", ownerDisplayName ? ownerDisplayName : "");
  19. printf("\n");
  20. return S3StatusOK;
  21. }
  22. S3ListServiceHandler listServiceHandler =
  23. {
  24. responseHandler,
  25. &listServiceCallback
  26. };
  27. bool header_printed = false;
  28. S3_list_service(S3ProtocolHTTP, access_key, secret_key, security_token, host,
  29. auth_region, NULL, 0, &listServiceHandler, &header_printed);

Creating a Bucket

This creates a new bucket.

  1. S3_create_bucket(S3ProtocolHTTP, access_key, secret_key, NULL, host, sample_bucket, S3CannedAclPrivate, NULL, NULL, &responseHandler, NULL);

Listing a Bucket’s Content

This gets a list of objects in the bucket. This also prints out each object’s name, the file size, and last modified date.

  1. static S3Status listBucketCallback(
  2. int isTruncated,
  3. const char *nextMarker,
  4. int contentsCount,
  5. const S3ListBucketContent *contents,
  6. int commonPrefixesCount,
  7. const char **commonPrefixes,
  8. void *callbackData)
  9. {
  10. printf("%-22s", " Object Name");
  11. printf(" %-5s %-20s", "Size", " Last Modified");
  12. printf("\n");
  13. printf("----------------------");
  14. printf(" -----" " --------------------");
  15. printf("\n");
  16. for (int i = 0; i < contentsCount; i++) {
  17. char timebuf[256];
  18. char sizebuf[16];
  19. const S3ListBucketContent *content = &(contents[i]);
  20. time_t t = (time_t) content->lastModified;
  21. strftime(timebuf, sizeof(timebuf), "%Y-%m-%dT%H:%M:%SZ", gmtime(&t));
  22. sprintf(sizebuf, "%5llu", (unsigned long long) content->size);
  23. printf("%-22s %s %s\n", content->key, sizebuf, timebuf);
  24. }
  25. return S3StatusOK;
  26. }
  27. S3ListBucketHandler listBucketHandler =
  28. {
  29. responseHandler,
  30. &listBucketCallback
  31. };
  32. S3_list_bucket(&bucketContext, NULL, NULL, NULL, 0, NULL, 0, &listBucketHandler, NULL);

The output will look something like this:

  1. myphoto1.jpg 251262 2011-08-08T21:35:48.000Z
  2. myphoto2.jpg 262518 2011-08-08T21:38:01.000Z

Deleting a Bucket

Note

The Bucket must be empty! Otherwise it won’t work!

  1. S3_delete_bucket(S3ProtocolHTTP, S3UriStylePath, access_key, secret_key, 0, host, sample_bucket, NULL, NULL, 0, &responseHandler, NULL);

Creating an Object (from a file)

This creates a file hello.txt.

  1. #include <sys/stat.h>
  2. typedef struct put_object_callback_data
  3. {
  4. FILE *infile;
  5. uint64_t contentLength;
  6. } put_object_callback_data;
  7. static int putObjectDataCallback(int bufferSize, char *buffer, void *callbackData)
  8. {
  9. put_object_callback_data *data = (put_object_callback_data *) callbackData;
  10. int ret = 0;
  11. if (data->contentLength) {
  12. int toRead = ((data->contentLength > (unsigned) bufferSize) ? (unsigned) bufferSize : data->contentLength);
  13. ret = fread(buffer, 1, toRead, data->infile);
  14. }
  15. data->contentLength -= ret;
  16. return ret;
  17. }
  18. put_object_callback_data data;
  19. struct stat statbuf;
  20. if (stat(sample_file, &statbuf) == -1) {
  21. fprintf(stderr, "\nERROR: Failed to stat file %s: ", sample_file);
  22. perror(0);
  23. exit(-1);
  24. }
  25. int contentLength = statbuf.st_size;
  26. data.contentLength = contentLength;
  27. if (!(data.infile = fopen(sample_file, "r"))) {
  28. fprintf(stderr, "\nERROR: Failed to open input file %s: ", sample_file);
  29. perror(0);
  30. exit(-1);
  31. }
  32. S3PutObjectHandler putObjectHandler =
  33. {
  34. responseHandler,
  35. &putObjectDataCallback
  36. };
  37. S3_put_object(&bucketContext, sample_key, contentLength, NULL, NULL, 0, &putObjectHandler, &data);
  38. fclose(data.infile);

Download an Object (to a file)

This downloads a file and prints the contents.

  1. static S3Status getObjectDataCallback(int bufferSize, const char *buffer, void *callbackData)
  2. {
  3. FILE *outfile = (FILE *) callbackData;
  4. size_t wrote = fwrite(buffer, 1, bufferSize, outfile);
  5. return ((wrote < (size_t) bufferSize) ? S3StatusAbortedByCallback : S3StatusOK);
  6. }
  7. S3GetObjectHandler getObjectHandler =
  8. {
  9. responseHandler,
  10. &getObjectDataCallback
  11. };
  12. FILE *outfile = stdout;
  13. S3_get_object(&bucketContext, sample_key, NULL, 0, 0, NULL, 0, &getObjectHandler, outfile);

Delete an Object

This deletes an object.

  1. S3ResponseHandler deleteResponseHandler =
  2. {
  3. 0,
  4. &responseCompleteCallback
  5. };
  6. S3_delete_object(&bucketContext, sample_key, 0, 0, &deleteResponseHandler, 0);

Change an Object’s ACL

This changes an object’s ACL to grant full control to another user.

  1. #include <string.h>
  2. char ownerId[] = "owner";
  3. char ownerDisplayName[] = "owner";
  4. char granteeId[] = "grantee";
  5. char granteeDisplayName[] = "grantee";
  6. S3AclGrant grants[] = {
  7. {
  8. S3GranteeTypeCanonicalUser,
  9. {{}},
  10. S3PermissionFullControl
  11. },
  12. {
  13. S3GranteeTypeCanonicalUser,
  14. {{}},
  15. S3PermissionReadACP
  16. },
  17. {
  18. S3GranteeTypeAllUsers,
  19. {{}},
  20. S3PermissionRead
  21. }
  22. };
  23. strncpy(grants[0].grantee.canonicalUser.id, ownerId, S3_MAX_GRANTEE_USER_ID_SIZE);
  24. strncpy(grants[0].grantee.canonicalUser.displayName, ownerDisplayName, S3_MAX_GRANTEE_DISPLAY_NAME_SIZE);
  25. strncpy(grants[1].grantee.canonicalUser.id, granteeId, S3_MAX_GRANTEE_USER_ID_SIZE);
  26. strncpy(grants[1].grantee.canonicalUser.displayName, granteeDisplayName, S3_MAX_GRANTEE_DISPLAY_NAME_SIZE);
  27. S3_set_acl(&bucketContext, sample_key, ownerId, ownerDisplayName, 3, grants, 0, &responseHandler, 0);

Generate Object Download URL (signed)

This generates a signed download URL that will be valid for 5 minutes.

  1. #include <time.h>
  2. char buffer[S3_MAX_AUTHENTICATED_QUERY_STRING_SIZE];
  3. int64_t expires = time(NULL) + 60 * 5; // Current time + 5 minutes
  4. S3_generate_authenticated_query_string(buffer, &bucketContext, sample_key, expires, NULL, "GET");