s3_list_objects.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /**
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. * SPDX-License-Identifier: Apache-2.0.
  4. */
  5. #include <aws/s3/private/s3_list_objects.h>
  6. #include <aws/s3/private/s3_paginator.h>
  7. #include <aws/s3/private/s3_util.h>
  8. #include <aws/common/ref_count.h>
  9. #include <aws/common/xml_parser.h>
  10. #include <aws/io/uri.h>
  11. #include <aws/http/http.h>
  12. #include <aws/http/request_response.h>
  13. struct aws_s3_operation_data {
  14. struct aws_allocator *allocator;
  15. struct aws_string *prefix;
  16. struct aws_string *delimiter;
  17. struct aws_ref_count ref_count;
  18. aws_s3_on_object_fn *on_object;
  19. void *user_data;
  20. };
  21. static void s_ref_count_zero_callback(void *arg) {
  22. struct aws_s3_operation_data *operation_data = arg;
  23. if (operation_data->delimiter) {
  24. aws_string_destroy(operation_data->delimiter);
  25. }
  26. if (operation_data->prefix) {
  27. aws_string_destroy(operation_data->prefix);
  28. }
  29. aws_mem_release(operation_data->allocator, operation_data);
  30. }
  31. static void s_on_paginator_cleanup(void *user_data) {
  32. struct aws_s3_operation_data *operation_data = user_data;
  33. aws_ref_count_release(&operation_data->ref_count);
  34. }
  35. struct fs_parser_wrapper {
  36. struct aws_allocator *allocator;
  37. struct aws_s3_object_info fs_info;
  38. };
  39. /* invoked when the ListBucketResult/Contents node is iterated. */
  40. static bool s_on_contents_node(struct aws_xml_parser *parser, struct aws_xml_node *node, void *user_data) {
  41. struct fs_parser_wrapper *fs_wrapper = user_data;
  42. struct aws_s3_object_info *fs_info = &fs_wrapper->fs_info;
  43. /* for each Contents node, get the info from it and send it off as an object we've encountered */
  44. struct aws_byte_cursor node_name;
  45. aws_xml_node_get_name(node, &node_name);
  46. if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "ETag")) {
  47. return aws_xml_node_as_body(parser, node, &fs_info->e_tag) == AWS_OP_SUCCESS;
  48. }
  49. if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "Key")) {
  50. return aws_xml_node_as_body(parser, node, &fs_info->key) == AWS_OP_SUCCESS;
  51. }
  52. if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "LastModified")) {
  53. struct aws_byte_cursor date_cur;
  54. if (aws_xml_node_as_body(parser, node, &date_cur) == AWS_OP_SUCCESS) {
  55. aws_date_time_init_from_str_cursor(&fs_info->last_modified, &date_cur, AWS_DATE_FORMAT_ISO_8601);
  56. return true;
  57. }
  58. return false;
  59. }
  60. if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "Size")) {
  61. struct aws_byte_cursor size_cur;
  62. if (aws_xml_node_as_body(parser, node, &size_cur) == AWS_OP_SUCCESS) {
  63. if (aws_byte_cursor_utf8_parse_u64(size_cur, &fs_info->size)) {
  64. return false;
  65. }
  66. return true;
  67. }
  68. }
  69. return true;
  70. }
  71. /* invoked when the ListBucketResult/CommonPrefixes node is iterated. */
  72. static bool s_on_common_prefixes_node(struct aws_xml_parser *parser, struct aws_xml_node *node, void *user_data) {
  73. struct fs_parser_wrapper *fs_wrapper = user_data;
  74. struct aws_byte_cursor node_name;
  75. aws_xml_node_get_name(node, &node_name);
  76. if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "Prefix")) {
  77. return aws_xml_node_as_body(parser, node, &fs_wrapper->fs_info.prefix) == AWS_OP_SUCCESS;
  78. }
  79. return true;
  80. }
  81. static bool s_on_list_bucket_result_node_encountered(
  82. struct aws_xml_parser *parser,
  83. struct aws_xml_node *node,
  84. void *user_data) {
  85. struct aws_s3_operation_data *operation_data = user_data;
  86. struct aws_byte_cursor node_name;
  87. aws_xml_node_get_name(node, &node_name);
  88. struct fs_parser_wrapper fs_wrapper;
  89. AWS_ZERO_STRUCT(fs_wrapper);
  90. if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "Contents")) {
  91. fs_wrapper.allocator = operation_data->allocator;
  92. /* this will traverse the current Contents node, get the metadata necessary to construct
  93. * an instance of fs_info so we can invoke the callback on it. This happens once per object. */
  94. bool ret_val = aws_xml_node_traverse(parser, node, s_on_contents_node, &fs_wrapper) == AWS_OP_SUCCESS;
  95. if (operation_data->prefix && !fs_wrapper.fs_info.prefix.len) {
  96. fs_wrapper.fs_info.prefix = aws_byte_cursor_from_string(operation_data->prefix);
  97. }
  98. struct aws_byte_buf trimmed_etag;
  99. AWS_ZERO_STRUCT(trimmed_etag);
  100. if (fs_wrapper.fs_info.e_tag.len) {
  101. struct aws_string *quoted_etag_str =
  102. aws_string_new_from_cursor(fs_wrapper.allocator, &fs_wrapper.fs_info.e_tag);
  103. replace_quote_entities(fs_wrapper.allocator, quoted_etag_str, &trimmed_etag);
  104. fs_wrapper.fs_info.e_tag = aws_byte_cursor_from_buf(&trimmed_etag);
  105. aws_string_destroy(quoted_etag_str);
  106. }
  107. if (ret_val && operation_data->on_object) {
  108. ret_val |= operation_data->on_object(&fs_wrapper.fs_info, operation_data->user_data);
  109. }
  110. if (trimmed_etag.len) {
  111. aws_byte_buf_clean_up(&trimmed_etag);
  112. }
  113. return ret_val;
  114. }
  115. if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "CommonPrefixes")) {
  116. /* this will traverse the current CommonPrefixes node, get the metadata necessary to construct
  117. * an instance of fs_info so we can invoke the callback on it. This happens once per prefix. */
  118. bool ret_val = aws_xml_node_traverse(parser, node, s_on_common_prefixes_node, &fs_wrapper) == AWS_OP_SUCCESS;
  119. if (ret_val && operation_data->on_object) {
  120. ret_val |= operation_data->on_object(&fs_wrapper.fs_info, operation_data->user_data);
  121. }
  122. return ret_val;
  123. }
  124. return true;
  125. }
  126. static int s_construct_next_request_http_message(
  127. struct aws_byte_cursor *continuation_token,
  128. void *user_data,
  129. struct aws_http_message **out_message) {
  130. AWS_PRECONDITION(user_data);
  131. struct aws_s3_operation_data *operation_data = user_data;
  132. struct aws_byte_cursor s_path_start = aws_byte_cursor_from_c_str("/?list-type=2");
  133. struct aws_byte_buf request_path;
  134. aws_byte_buf_init_copy_from_cursor(&request_path, operation_data->allocator, s_path_start);
  135. if (operation_data->prefix) {
  136. struct aws_byte_cursor s_prefix = aws_byte_cursor_from_c_str("&prefix=");
  137. aws_byte_buf_append_dynamic(&request_path, &s_prefix);
  138. struct aws_byte_cursor s_prefix_val = aws_byte_cursor_from_string(operation_data->prefix);
  139. aws_byte_buf_append_encoding_uri_param(&request_path, &s_prefix_val);
  140. }
  141. if (operation_data->delimiter) {
  142. struct aws_byte_cursor s_delimiter = aws_byte_cursor_from_c_str("&delimiter=");
  143. aws_byte_buf_append_dynamic(&request_path, &s_delimiter);
  144. struct aws_byte_cursor s_delimiter_val = aws_byte_cursor_from_string(operation_data->delimiter);
  145. aws_byte_buf_append_dynamic(&request_path, &s_delimiter_val);
  146. }
  147. if (continuation_token) {
  148. struct aws_byte_cursor s_continuation = aws_byte_cursor_from_c_str("&continuation-token=");
  149. aws_byte_buf_append_dynamic(&request_path, &s_continuation);
  150. aws_byte_buf_append_encoding_uri_param(&request_path, continuation_token);
  151. }
  152. struct aws_http_message *list_objects_v2_request = aws_http_message_new_request(operation_data->allocator);
  153. aws_http_message_set_request_path(list_objects_v2_request, aws_byte_cursor_from_buf(&request_path));
  154. aws_byte_buf_clean_up(&request_path);
  155. struct aws_http_header accept_header = {
  156. .name = aws_byte_cursor_from_c_str("accept"),
  157. .value = aws_byte_cursor_from_c_str("application/xml"),
  158. };
  159. aws_http_message_add_header(list_objects_v2_request, accept_header);
  160. aws_http_message_set_request_method(list_objects_v2_request, aws_http_method_get);
  161. *out_message = list_objects_v2_request;
  162. return AWS_OP_SUCCESS;
  163. }
  164. struct aws_s3_paginator *aws_s3_initiate_list_objects(
  165. struct aws_allocator *allocator,
  166. const struct aws_s3_list_objects_params *params) {
  167. AWS_FATAL_PRECONDITION(params);
  168. AWS_FATAL_PRECONDITION(params->client);
  169. AWS_FATAL_PRECONDITION(params->bucket_name.len);
  170. AWS_FATAL_PRECONDITION(params->endpoint.len);
  171. struct aws_s3_operation_data *operation_data = aws_mem_calloc(allocator, 1, sizeof(struct aws_s3_operation_data));
  172. operation_data->allocator = allocator;
  173. operation_data->delimiter =
  174. params->delimiter.len > 0 ? aws_string_new_from_cursor(allocator, &params->delimiter) : NULL;
  175. operation_data->prefix = params->prefix.len > 0 ? aws_string_new_from_cursor(allocator, &params->prefix) : NULL;
  176. operation_data->on_object = params->on_object;
  177. operation_data->user_data = params->user_data;
  178. aws_ref_count_init(&operation_data->ref_count, operation_data, s_ref_count_zero_callback);
  179. struct aws_byte_cursor xml_result_node_name = aws_byte_cursor_from_c_str("ListBucketResult");
  180. struct aws_byte_cursor continuation_node_name = aws_byte_cursor_from_c_str("NextContinuationToken");
  181. struct aws_s3_paginated_operation_params operation_params = {
  182. .next_message = s_construct_next_request_http_message,
  183. .on_result_node_encountered_fn = s_on_list_bucket_result_node_encountered,
  184. .on_paginated_operation_cleanup = s_on_paginator_cleanup,
  185. .result_xml_node_name = &xml_result_node_name,
  186. .continuation_token_node_name = &continuation_node_name,
  187. .user_data = operation_data,
  188. };
  189. struct aws_s3_paginated_operation *operation = aws_s3_paginated_operation_new(allocator, &operation_params);
  190. struct aws_s3_paginator_params paginator_params = {
  191. .client = params->client,
  192. .bucket_name = params->bucket_name,
  193. .endpoint = params->endpoint,
  194. .on_page_finished_fn = params->on_list_finished,
  195. .operation = operation,
  196. .user_data = params->user_data,
  197. };
  198. struct aws_s3_paginator *paginator = aws_s3_initiate_paginator(allocator, &paginator_params);
  199. // transfer control to paginator
  200. aws_s3_paginated_operation_release(operation);
  201. return paginator;
  202. }
  203. struct aws_s3_paginated_operation *aws_s3_list_objects_operation_new(
  204. struct aws_allocator *allocator,
  205. const struct aws_s3_list_objects_params *params) {
  206. AWS_FATAL_PRECONDITION(params);
  207. AWS_FATAL_PRECONDITION(params->client);
  208. AWS_FATAL_PRECONDITION(params->bucket_name.len);
  209. AWS_FATAL_PRECONDITION(params->endpoint.len);
  210. struct aws_s3_operation_data *operation_data = aws_mem_calloc(allocator, 1, sizeof(struct aws_s3_operation_data));
  211. operation_data->allocator = allocator;
  212. operation_data->delimiter =
  213. params->delimiter.len > 0 ? aws_string_new_from_cursor(allocator, &params->delimiter) : NULL;
  214. operation_data->prefix = params->prefix.len > 0 ? aws_string_new_from_cursor(allocator, &params->prefix) : NULL;
  215. operation_data->on_object = params->on_object;
  216. operation_data->user_data = params->user_data;
  217. aws_ref_count_init(&operation_data->ref_count, operation_data, s_ref_count_zero_callback);
  218. struct aws_byte_cursor xml_result_node_name = aws_byte_cursor_from_c_str("ListBucketResult");
  219. struct aws_byte_cursor continuation_node_name = aws_byte_cursor_from_c_str("NextContinuationToken");
  220. struct aws_s3_paginated_operation_params operation_params = {
  221. .next_message = s_construct_next_request_http_message,
  222. .on_result_node_encountered_fn = s_on_list_bucket_result_node_encountered,
  223. .on_paginated_operation_cleanup = s_on_paginator_cleanup,
  224. .result_xml_node_name = &xml_result_node_name,
  225. .continuation_token_node_name = &continuation_node_name,
  226. .user_data = operation_data,
  227. };
  228. struct aws_s3_paginated_operation *operation = aws_s3_paginated_operation_new(allocator, &operation_params);
  229. return operation;
  230. }