Manage objects

Last Updated: Aug 09, 2017

View all objects

You can list all objects in a bucket using the oss_list_object interface.

  1. aos_pool_t *p;
  2. oss_request_options_t *options;
  3. aos_status_t *s;
  4. aos_table_t *resp_headers;
  5. char *bucket_name = "<Name of your bucket>";
  6. aos_string_t bucket;
  7. oss_list_object_params_t *params;
  8. oss_list_object_content_t *content;
  9. int max_ret = 1000;
  10. char *key;
  11. aos_pool_create(&p, NULL);
  12. options = oss_request_options_create(p);
  13. init_options(options);
  14. aos_str_set(&bucket, bucket_name);
  15. params = oss_create_list_object_params(p);
  16. params->max_ret = max_ret;
  17. aos_str_set(&params->prefix, "<View the prefix of the object>");
  18. aos_str_set(&params->delimiter, "<View the delimiter of the object>");
  19. aos_str_set(&params->marker, "<View the start point of the object>");
  20. s = oss_list_object(options, &bucket, params, &resp_headers);
  21. if (aos_status_is_ok(s)) {
  22. printf("get object succeeded\n");
  23. /* Download objects */
  24. aos_list_for_each_entry(content, &params->object_list, node) {
  25. key = apr_psprintf(p, "%.*s", content->key.len, content->key.data);
  26. }
  27. } else {
  28. printf("get object failed\n");
  29. }
  30. aos_pool_destroy(p);

Notice:

  • By default, if a bucket contains more than 1,000 objects, only the first 1,000 objects will be returned and the IsTruncated parameter in the returned results will be true. The NextMarker will be returned to serve as the start point of next read. To increase the quantity of returned objects, you can modify the max_ret parameter or use the marker parameter for multiple reads.
  • Complete code can be found at GitHub.

Create a simulate directory

OSS is an object-based storage service. It has no directory. The creation of a simulate directory is essentially the creation of an object with the size of 0. This object can be uploaded and downloaded as a normal object, but the console will display the object whose name ends with a slash (/) as a folder. So you can use the above method to create a simulate directory.

  1. aos_pool_t *p;
  2. oss_request_options_t *options;
  3. aos_status_t *s;
  4. aos_table_t *headers;
  5. aos_table_t *resp_headers;
  6. char *bucket_name = "<Name of your bucket>";
  7. char *object_name = "<Your folder name/>";
  8. aos_string_t bucket;
  9. aos_string_t object;
  10. char *data = "";
  11. aos_list_t buffer;
  12. aos_buf_t *content;
  13. aos_pool_create(&p, NULL);
  14. options = oss_request_options_create(p);
  15. init_options(options);
  16. aos_str_set(&bucket, bucket_name);
  17. aos_str_set(&object, object_name);
  18. headers = aos_table_make(p, 0);
  19. aos_list_init(&buffer);
  20. content = aos_buf_pack(options->pool, data, strlen(data));
  21. aos_list_add_tail(&content->node, &buffer);
  22. s = oss_put_object_from_buffer(options, &bucket, &object, &buffer, headers, &resp_headers);
  23. if (aos_status_is_ok(s)) {
  24. printf("create dir succeeded\n");
  25. } else {
  26. printf("create dir failed\n");
  27. }
  28. aos_pool_destroy(p);

Note: Complete code can be found at GitHub.

Set the HTTP Header of an object

When uploading an object to OSS, except for the object content, you can specify certain attribute information for the object. The attribute information is called meta information. The metadata is stored together with the object during uploading, and returned together with the object during downloading. The following code sets the expiration time for an object:

  1. aos_pool_t *p;
  2. oss_request_options_t *options;
  3. aos_status_t *s;
  4. aos_table_t *headers;
  5. aos_table_t *resp_headers;
  6. char *bucket_name = "<Name of your bucket>";
  7. char *object_name = "<Name of your object>";
  8. aos_string_t bucket;
  9. aos_string_t object;
  10. char *data = "<object content>";
  11. aos_list_t buffer;
  12. aos_buf_t *content;
  13. aos_pool_create(&p, NULL);
  14. options = oss_request_options_create(p);
  15. init_options(options);
  16. aos_str_set(&object, object_name);
  17. aos_str_set(&bucket, bucket_name);
  18. headers = aos_table_make(p, 0);
  19. /* Set the HTTP header */
  20. headers = aos_table_make(p, 1);
  21. apr_table_set(headers, "Expires", "Fri, 28 Feb 2012 05:38:42 GMT");
  22. /* Read data into the buffer */
  23. aos_list_init(&buffer);
  24. content = aos_buf_pack(options->pool, data, strlen(data));
  25. aos_list_add_tail(&content->node, &buffer);
  26. s = oss_put_object_from_buffer(options, &bucket, &object, &buffer, headers, &resp_headers);
  27. if (aos_status_is_ok(s)) {
  28. printf("put object succeeded\n");
  29. } else {
  30. printf("put object failed\n");
  31. }
  32. aos_pool_destroy(p);

Notice:

  • The following options are available for you to set the HTTP header: Cache-Control, Content-Disposition, Content-Encoding, and Expires. For their introductions, see RFC2616.
  • Complete code can be found at GitHub.

Set user meta information

The OSS supports user-defined meta information for object description. For example:

  1. aos_pool_t *p;
  2. oss_request_options_t *options;
  3. aos_status_t *s;
  4. aos_table_t *headers;
  5. aos_table_t *resp_headers;
  6. char *bucket_name = "<Name of your bucket>";
  7. char *object_name = "<Name of your object>";
  8. aos_string_t bucket;
  9. aos_string_t object;
  10. char *data = "<object content>";
  11. aos_list_t buffer;
  12. aos_buf_t *content;
  13. aos_pool_create(&p, NULL);
  14. options = oss_request_options_create(p);
  15. init_options(options);
  16. aos_str_set(&bucket, bucket_name);
  17. aos_str_set(&object, object_name);
  18. headers = aos_table_make(p, 1);
  19. /* Set user-defined meta */
  20. apr_table_set(headers, "x-oss-meta-author", "oss");
  21. /* Upload data to OSS */
  22. aos_list_init(&buffer);
  23. content = aos_buf_pack(options->pool, data, strlen(data));
  24. aos_list_add_tail(&content->node, &buffer);
  25. s = oss_put_object_from_buffer(options, &bucket, &object, &buffer, headers, &resp_headers);
  26. if (aos_status_is_ok(s)) {
  27. printf("put object succeeded\n");
  28. } else {
  29. printf("put object failed\n");
  30. }
  31. aos_pool_destroy(p);

Notice:

  • Because the object meta information is stored in HTTP headers during object uploading/downloading, the meta information cannot contain any complex character according to HTTP.
  • A single object may have multiple similar parameters, but the total size of UserMetadata cannot exceed 8KB.

Get the metadata of the object

Sometimes you need to get metadata related with the object after you upload the object to the OSS or before you read the object, such as the length or the object type. In this case, you can get the metadata of the object through the oss_object_head interface. The following codes gets the object length and type information:

  1. void head_object()
  2. {
  3. aos_pool_t *p = NULL;
  4. aos_string_t bucket;
  5. aos_string_t object;
  6. oss_request_options_t *options = NULL;
  7. aos_table_t *headers = NULL;
  8. aos_table_t *resp_headers = NULL;
  9. aos_status_t *s = NULL;
  10. char *content_length_str = NULL;
  11. char *object_type = NULL;
  12. int64_t content_length = 0;
  13. aos_pool_create(&p, NULL);
  14. /* Create and initialize options */
  15. options = oss_request_options_create(p);
  16. init_options(options);
  17. /* Initialize parameters */
  18. aos_str_set(&bucket, "<Name of your bucket>");
  19. aos_str_set(&object, "<Name of your object>");
  20. headers = aos_table_make(p, 0);
  21. /* Get the object metadata */
  22. s = oss_head_object(options, &bucket, &object, headers, &resp_headers);
  23. if (aos_status_is_ok(s)) {
  24. /* Get the object length */
  25. content_length_str = (char*)apr_table_get(resp_headers, OSS_CONTENT_LENGTH);
  26. if (content_length_str != NULL) {
  27. content_length = atoll(content_length_str);
  28. }
  29. /* Get the object type */
  30. object_type = (char*)apr_table_get(resp_headers, OSS_OBJECT_TYPE);
  31. printf("head object succeeded, object type:%s, content_length:%ld\n",
  32. object_type, content_length);
  33. } else {
  34. printf("head object failed\n");
  35. }
  36. aos_pool_destroy(p);
  37. }

Notice:

  • The oss_head_object interface will not download the object content, but only read the object metadata, including the system-level and user-defined metadata.
  • Complete code can be found at GitHub.

Copy an object

You can copy objects that you have permissions on within the same region. The following code copies an object using the oss_copy_object interface:

  1. aos_pool_t *p;
  2. oss_request_options_t *options;
  3. aos_status_t *s;
  4. aos_table_t * headers;
  5. aos_table_t *resp_headers;
  6. char *source_bucket_name = "<Name of your source bucket>";
  7. char *source_object_name = "<Name of your source object>";
  8. char *dest_bucket_name = "<Name of your target bucket>";
  9. char *source_object_name = "<Name of your target object>";
  10. aos_string_t source_bucket;
  11. aos_string_t source_object;
  12. aos_string_t dest_bucket;
  13. aos_string_t dest_object;
  14. aos_pool_create(&p, NULL);
  15. options = oss_request_options_create(p);
  16. init_options(options);
  17. aos_str_set(&source_bucket, source_bucket_name);
  18. aos_str_set(&source_object, source_object_name);
  19. aos_str_set(&dest_bucket, dest_bucket_name);
  20. aos_str_set(&dest_object, dest_object_name);
  21. headers = aos_table_make(p, 0);
  22. /* Copy the object */
  23. s = oss_copy_object(options, &source_bucket, &source_object, &dest_bucket, &dest_object, headers, &resp_headers);
  24. if (aos_status_is_ok(s)) {
  25. printf("copy object succeeded\n");
  26. } else {
  27. printf("copy object failed\n");
  28. }
  29. aos_pool_destroy(p);

Notice:

  • The source bucket and the target bucket must belong to the same region.
  • When copying an object larger than 1 GB, we suggest the multipart copying mode.

Multipart object copying

When copying an object larger than 500 MB, we suggest using the oss_upload_part_copy interface. The following code copies an object in the multipart copying mode:

  1. aos_pool_t *p;
  2. oss_request_options_t *options;
  3. char *source_bucket_name = "<Name of your source bucket>";
  4. char *source_object_name = "<Name of your source object>";
  5. char *dest_bucket_name = "<Name of your target bucket>";
  6. char *source_object_name = "<Name of your target object>";
  7. aos_string_t dest_bucket;
  8. aos_string_t dest_object;
  9. aos_string_t upload_id;
  10. aos_table_t *init_headers;
  11. aos_table_t *copy_headers;
  12. aos_table_t *list_part_resp_headers;
  13. aos_table_t *complete_resp_headers
  14. aos_table_t *resp_headers;
  15. aos_status_t *s;
  16. oss_list_upload_part_params_t *list_upload_part_params;
  17. oss_upload_part_copy_params_t *upload_part_copy_params1;
  18. aos_list_t complete_part_list;
  19. oss_list_part_content_t *part_content;
  20. oss_complete_part_content_t *complete_content;
  21. int part1 = 1;
  22. int64_t range_start1 = 0;
  23. int64_t range_end1 = 6000000;//not less than 5MB
  24. int max_ret = 1000;
  25. aos_pool_create(&p, NULL);
  26. options = oss_request_options_create(p);
  27. init_options(options);
  28. aos_str_set(&dest_bucket, dest_bucket_name);
  29. aos_str_set(&dest_object, dest_object_name);
  30. init_headers = aos_table_make(p, 0);
  31. s = oss_init_multipart_upload(options, &dest_bucket, &dest_object, init_headers, &upload_id, &resp_headers);
  32. /* Copy the first data part */
  33. upload_part_copy_params1 = oss_create_upload_part_copy_params(p);
  34. aos_str_set(&upload_part_copy_params1->source_bucket, source_bucket_name);
  35. aos_str_set(&upload_part_copy_params1->source_object, source_object_name);
  36. aos_str_set(&upload_part_copy_params1->dest_bucket, dest_bucket_name);
  37. aos_str_set(&upload_part_copy_params1->dest_object, dest_object_name);
  38. aos_str_set(&upload_part_copy_params1->upload_id, upload_id->data);
  39. upload_part_copy_params1->part_num = part1;
  40. upload_part_copy_params1->range_start = range_start1;
  41. upload_part_copy_params1->range_end = range_end1;
  42. copy_headers = aos_table_make(p, 0);
  43. s = oss_upload_part_copy(options, upload_part_copy_params1, copy_headers, &resp_headers);
  44. if (aos_status_is_ok(s)) {
  45. printf("upload part copy succeeded\n");
  46. } else {
  47. printf("upload part copy failed\n");
  48. }
  49. /* Continue copying the rest parts. Skipped*/
  50. ...
  51. /* List the parts */
  52. list_upload_part_params = oss_create_list_upload_part_params(p);
  53. list_upload_part_params->max_ret = max_ret;
  54. aos_list_init(&complete_part_list);
  55. s = oss_list_upload_part(options, &dest_bucket, &dest_object, &upload_id,
  56. list_upload_part_params, &list_part_resp_headers);
  57. aos_list_for_each_entry(part_content, &list_upload_part_params->part_list, node) {
  58. complete_content = oss_create_complete_part_content(p);
  59. aos_str_set(&complete_content->part_number, part_content->part_number.data);
  60. aos_str_set(&complete_content->etag, part_content->etag.data);
  61. aos_list_add_tail(&complete_content->node, &complete_part_list);
  62. }
  63. /* Complete the multipart copying */
  64. s = oss_complete_multipart_upload(options, &dest_bucket, &dest_object, &upload_id, &complete_part_list, &complete_resp_headers);
  65. if (aos_status_is_ok(s)) {
  66. printf("complete multipart upload succeeded\n");
  67. } else {
  68. printf("complete multipart upload failed\n");
  69. }
  70. aos_pool_destroy(p);

Delete an object

You can delete an object using the oss_delete_object interface:

  1. void delete_object()
  2. {
  3. aos_pool_t *p = NULL;
  4. aos_string_t bucket;
  5. aos_string_t object;
  6. oss_request_options_t *options = NULL;
  7. aos_table_t *resp_headers = NULL;
  8. aos_status_t *s = NULL;
  9. aos_pool_create(&p, NULL);
  10. /* Create and initialize options */
  11. options = oss_request_options_create(p);
  12. init_options(options);
  13. aos_str_set(&bucket, "<Name of your bucket>");
  14. aos_str_set(&object, "<Name of your object>");
  15. /* Delete an object */
  16. s = oss_delete_object(options, &bucket, &object, &resp_headers);
  17. /* Determine whether the deletion was successful */
  18. if (aos_status_is_ok(s)) {
  19. printf("delete object succeed\n");
  20. } else {
  21. printf("delete object failed\n");
  22. }
  23. /* Release the resources*/
  24. aos_pool_destroy(p);
  25. }

Note: Complete code can be found at GitHub.

Delete objects in batches

You can delete objects in batches using the oss_delete_objects interface. You can use the is_quiet parameter to determine whether to return the deletion results:

  1. aos_pool_t *p;
  2. aos_status_t *s;
  3. aos_table_t *resp_headers;
  4. oss_request_options_t *options;
  5. char *bucket_name = "<Name of your bucket>";
  6. char *object_name1 = "<Name 1 of your object>";
  7. char *object_name2 = "<Name 2 of your object>";
  8. aos_string_t bucket;
  9. aos_string_t object1;
  10. aos_string_t object2;
  11. oss_object_key_t *content1;
  12. oss_object_key_t *content2;
  13. aos_list_t object_list;
  14. aos_list_t deleted_object_list;
  15. int is_quiet = 1;
  16. options = oss_request_options_create(p);
  17. init_options(options);
  18. aos_str_set(&bucket, bucket_name);
  19. aos_str_set(&object1, object_name1);
  20. aos_str_set(&object2, object_name2);
  21. /* Construct the list of objects to be deleted */
  22. aos_list_init(&object_list);
  23. aos_list_init(&deleted_object_list);
  24. content1 = oss_create_oss_object_key(p);
  25. aos_str_set(&content1->key, object_name1);
  26. aos_list_add_tail(&content1->node, &object_list);
  27. content2 = oss_create_oss_object_key(p);
  28. aos_str_set(&content2->key, object_name2);
  29. aos_list_add_tail(&content2->node, &object_list);
  30. /* Delete multiple objects */
  31. s = oss_delete_objects(options, &bucket, &object_list, is_quiet, &resp_headers, &deleted_object_list);
  32. if (aos_status_is_ok(s)) {
  33. printf("delete objects succeeded\n");
  34. } else {
  35. printf("delete objects failed\n");
  36. }
  37. aos_pool_destroy(p);

Delete objects with a specified prefix in batches

You can delete objects with a specified prefix in batches using the oss_delete_objects_by_prefix interface:

  1. aos_pool_t *p;
  2. oss_request_options_t *options;
  3. aos_status_t *s;
  4. aos_table_t *resp_headers;
  5. char *bucket_name = "<Name of your bucket>";
  6. char *prefix_str = "<The prefix of the objects to be deleted>";
  7. aos_string_t bucket;
  8. aos_string_t prefix;
  9. aos_pool_create(&p, NULL);
  10. options = oss_request_options_create(p);
  11. init_options(options);
  12. aos_str_set(&bucket, bucket_name);
  13. aos_str_set(&prefix, prefix_str);
  14. /* Delete objects with the specified prefix*/
  15. s = oss_delete_objects_by_prefix(options, &bucket, &prefix);
  16. if (aos_status_is_ok(s)) {
  17. printf("delete objects by prefix succeeded\n");
  18. } else {
  19. printf("delete objects by prefix failed\n");
  20. }
  21. aos_pool_destroy(p);

Notice:

  • You can delete a directory through this batch deletion of objects with a specified prefix. For example, to delete the dir directory, you can set the prefix to dir/.
  • If the prefix is a null string (“”) or NULL, the whole bucket will be deleted. Therefore, exercise caution when using this function.
Thank you! We've received your feedback.