|
| 1 | +/* |
| 2 | + * ****************************************************************************** |
| 3 | + * Copyright 2014-2016 Spectra Logic Corporation. All Rights Reserved. |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). You may not use |
| 5 | + * this file except in compliance with the License. A copy of the License is located at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * or in the "license" file accompanying this file. |
| 10 | + * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 11 | + * CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 12 | + * specific language governing permissions and limitations under the License. |
| 13 | + * **************************************************************************** |
| 14 | + */ |
| 15 | + |
| 16 | +#include <stdlib.h> |
| 17 | +#include <unistd.h> |
| 18 | +#include <sys/stat.h> |
| 19 | +#include "ds3.h" |
| 20 | +#include "samples.h" |
| 21 | + |
| 22 | +int main(void) { |
| 23 | + |
| 24 | + // The bucket the files will be stored in |
| 25 | + const char* bucket_name = BUCKETNAME; // defined in samples.h |
| 26 | + |
| 27 | + // Get a client instance which uses the environment variables to get the endpoint and credentials |
| 28 | + ds3_client* client; |
| 29 | + ds3_request* request; |
| 30 | + ds3_error* error; |
| 31 | + ds3_list_bucket_result_response* list_bucket_result; |
| 32 | + ds3_bulk_object_list_response* obj_list; |
| 33 | + ds3_master_object_list_response* chunks_response; |
| 34 | + ds3_bulk_object_response* current_obj_to_get; |
| 35 | + uint64_t chunk_count, current_chunk_count = 0; |
| 36 | + uint64_t chunk_index, obj_index; |
| 37 | + FILE* obj_file; |
| 38 | + |
| 39 | + // Create a client from environment variables |
| 40 | + error = ds3_create_client_from_env(&client); |
| 41 | + handle_error(error); |
| 42 | + |
| 43 | + // Get a list of all objects in the bucket to get |
| 44 | + request = ds3_init_get_bucket_request(bucket_name); // We need to create the request |
| 45 | + error = ds3_get_bucket_request(client, request, &list_bucket_result); // This will send the request |
| 46 | + ds3_request_free(request); |
| 47 | + handle_error(error); |
| 48 | + |
| 49 | + obj_list = ds3_convert_object_list((const ds3_contents_response**)list_bucket_result->objects, list_bucket_result->num_objects); |
| 50 | + ds3_list_bucket_result_response_free(list_bucket_result); |
| 51 | + |
| 52 | + // Initialize the bulk get request |
| 53 | + request = ds3_init_get_bulk_job_spectra_s3_request(bucket_name, obj_list); |
| 54 | + |
| 55 | + // Send the bulk get request to the server |
| 56 | + error = ds3_get_bulk_job_spectra_s3_request(client, request, &chunks_response); |
| 57 | + ds3_request_free(request); |
| 58 | + ds3_bulk_object_list_response_free(obj_list); |
| 59 | + handle_error(error); |
| 60 | + |
| 61 | + chunk_count = chunks_response->num_objects; |
| 62 | + |
| 63 | + // Bulk jobs are split into multiple chunks which then need to be transferred |
| 64 | + while (current_chunk_count < chunk_count) { |
| 65 | + |
| 66 | + // Get the chunks that the server can send. The server may need to retrieve objects into cache from tape |
| 67 | + request = ds3_init_get_job_chunks_ready_for_client_processing_spectra_s3_request(chunks_response->job_id->value); |
| 68 | + error = ds3_get_job_chunks_ready_for_client_processing_spectra_s3_request(client, request, &chunks_response); |
| 69 | + ds3_request_free(request); |
| 70 | + handle_error(error); |
| 71 | + |
| 72 | + // Check to see if any chunks can be processed |
| 73 | + if (chunks_response->num_objects > 0) { |
| 74 | + // Loop through all the chunks that are avaiable for processing, and get the files that are contained in them |
| 75 | + for (chunk_index = 0; chunk_index < chunks_response->num_objects; chunk_index++) { |
| 76 | + ds3_objects_response* chunk_object_list = chunks_response->objects[chunk_index]; |
| 77 | + for (obj_index = 0; obj_index < chunk_object_list->num_objects; obj_index++) { |
| 78 | + current_obj_to_get = chunk_object_list->objects[obj_index]; |
| 79 | + |
| 80 | + request = ds3_init_get_object_request(bucket_name, current_obj_to_get->name->value, current_obj_to_get->length); |
| 81 | + ds3_request_set_job(request, chunks_response->job_id->value); |
| 82 | + ds3_request_set_offset(request, current_obj_to_get->offset); |
| 83 | + obj_file = fopen(current_obj_to_get->name->value, "r"); |
| 84 | + |
| 85 | + error = ds3_put_object_request(client, request, obj_file, ds3_write_to_file); |
| 86 | + ds3_request_free(request); |
| 87 | + fclose(obj_file); |
| 88 | + handle_error(error); |
| 89 | + } |
| 90 | + current_chunk_count++; |
| 91 | + } |
| 92 | + } |
| 93 | + else { |
| 94 | + // When no chunks are returned we need to sleep to allow for objects to be retrieved from tape into cache |
| 95 | + sleep(300); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + // Cleanup the client and sdk |
| 100 | + ds3_master_object_list_response_free(chunks_response); |
| 101 | + ds3_creds_free(client->creds); |
| 102 | + ds3_client_free(client); |
| 103 | + ds3_cleanup(); |
| 104 | + return 0; |
| 105 | +} |
| 106 | + |
0 commit comments