|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +/** |
| 5 | + * Before running this C++ code example, set up your development environment, including your credentials. |
| 6 | + * |
| 7 | + * For more information, see the following documentation topic: |
| 8 | + * |
| 9 | + * https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started.html |
| 10 | + * |
| 11 | + * For information on the structure of the code examples and how to build and run the examples, see |
| 12 | + * https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started-code-examples.html. |
| 13 | + * |
| 14 | + **/ |
| 15 | + |
| 16 | +#include <iostream> |
| 17 | +#include <fstream> |
| 18 | +#include <aws/core/Aws.h> |
| 19 | +#include <aws/s3/S3Client.h> |
| 20 | +#include <aws/s3/model/GetObjectRequest.h> |
| 21 | +#include "sdk_customization_samples.h" |
| 22 | + |
| 23 | +// snippet-start:[cpp.example_code.sdk_customization.CustomResponseStream] |
| 24 | +//! Use a custom response stream when downloading an object from an Amazon Simple |
| 25 | +//! Storage Service (Amazon S3) bucket. |
| 26 | +/*! |
| 27 | + \param bucketName: The Amazon S3 bucket name. |
| 28 | + \param objectKey: The object key. |
| 29 | + \param filePath: File path for custom response stream. |
| 30 | + \param clientConfiguration: AWS client configuration. |
| 31 | + \return bool: Function succeeded. |
| 32 | + */ |
| 33 | + |
| 34 | +bool AwsDoc::SdkCustomization::customResponseStream(const Aws::String &bucketName, |
| 35 | + const Aws::String &objectKey, |
| 36 | + const Aws::String &filePath, |
| 37 | + const Aws::Client::ClientConfiguration &clientConfiguration) { |
| 38 | + |
| 39 | + Aws::S3::S3Client s3_client(clientConfiguration); |
| 40 | + |
| 41 | + Aws::S3::Model::GetObjectRequest getObjectRequest; |
| 42 | + getObjectRequest.WithBucket(bucketName).WithKey(objectKey); |
| 43 | + |
| 44 | + getObjectRequest.SetResponseStreamFactory([filePath]() { |
| 45 | + return Aws::New<Aws::FStream>( |
| 46 | + "FStreamAllocationTag", filePath, std::ios_base::out); |
| 47 | + }); |
| 48 | + |
| 49 | + Aws::S3::Model::GetObjectOutcome getObjectOutcome = s3_client.GetObject( |
| 50 | + getObjectRequest); |
| 51 | + |
| 52 | + if (getObjectOutcome.IsSuccess()) { |
| 53 | + std::cout << "Successfully retrieved object to file " << filePath << std::endl; |
| 54 | + } |
| 55 | + else { |
| 56 | + std::cerr << "Error getting object. " |
| 57 | + << getObjectOutcome.GetError().GetMessage() << std::endl; |
| 58 | + } |
| 59 | + |
| 60 | + return getObjectOutcome.IsSuccess(); |
| 61 | +} |
| 62 | +// snippet-end:[cpp.example_code.sdk_customization.CustomResponseStream] |
| 63 | + |
| 64 | + |
| 65 | +/* |
| 66 | + * |
| 67 | + * main function |
| 68 | + * |
| 69 | + * Usage: 'run_custom_response_stream <bucket_name> <object_key> <file_name>' |
| 70 | + * |
| 71 | + * Prerequisites: An Amazon S3 bucket containing an object. |
| 72 | + * |
| 73 | + */ |
| 74 | + |
| 75 | +#ifndef TESTING_BUILD |
| 76 | + |
| 77 | +int main(int argc, char **argv) { |
| 78 | + if (argc != 4) { |
| 79 | + std::cout << "Usage: run_custom_response_stream <bucket_name> <object_key> <file_name>" << std::endl; |
| 80 | + return 1; |
| 81 | + } |
| 82 | + |
| 83 | + Aws::String bucketName = argv[1]; |
| 84 | + Aws::String keyName = argv[2]; |
| 85 | + Aws::String fileName = argv[3]; |
| 86 | + |
| 87 | + Aws::SDKOptions options; |
| 88 | + options.loggingOptions.logLevel = Aws::Utils::Logging::LogLevel::Trace; |
| 89 | + Aws::InitAPI(options); |
| 90 | + { |
| 91 | + Aws::Client::ClientConfiguration clientConfig; |
| 92 | + // Optional: Set to the AWS Region (overrides config file). |
| 93 | + // clientConfig.region = "us-east-1"; |
| 94 | + |
| 95 | + AwsDoc::SdkCustomization::customResponseStream(bucketName, keyName, fileName, clientConfig); |
| 96 | + } |
| 97 | + |
| 98 | + Aws::ShutdownAPI(options); |
| 99 | + |
| 100 | + return 0; |
| 101 | +} |
| 102 | + |
| 103 | +#endif // TESTING_BUILD |
| 104 | + |
0 commit comments