AWSApiClientJobConfig.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <AzCore/std/smart_ptr/make_shared.h>
  10. #include <Framework/AWSApiJobConfig.h>
  11. #include <aws/s3/S3Client.h>
  12. // Forward declarations
  13. namespace Aws
  14. {
  15. namespace Client
  16. {
  17. struct ClientConfiguration;
  18. }
  19. namespace Http
  20. {
  21. class HttpClient;
  22. }
  23. namespace Auth
  24. {
  25. class AWSCredentials;
  26. }
  27. }
  28. namespace AWSCore
  29. {
  30. /// Provides configuration for AWS jobs using a specific client type.
  31. template<class ClientType>
  32. class IAwsApiClientJobConfig
  33. : public virtual IAwsApiJobConfig
  34. {
  35. public:
  36. //! Returns the created AWS client for the Job
  37. virtual std::shared_ptr<ClientType> GetClient() = 0;
  38. };
  39. // warning C4250: 'AWSCore::AwsApiClientJobConfig<ClientType>': inherits 'AWSCore::AwsApiJobConfig::AWSCore::AwsApiJobConfig::GetJobContext' via dominance
  40. // Thanks to http://stackoverflow.com/questions/11965596/diamond-inheritance-scenario-compiles-fine-in-g-but-produces-warnings-errors for the explanation
  41. // This is the expected and desired behavior. The warning is superfluous.
  42. AZ_PUSH_DISABLE_WARNING(4250, "-Wunknown-warning-option")
  43. /// Configuration for AWS jobs using a specific client type.
  44. template<class ClientType>
  45. class AwsApiClientJobConfig
  46. : public AwsApiJobConfig
  47. , public virtual IAwsApiClientJobConfig<ClientType>
  48. {
  49. public:
  50. AZ_CLASS_ALLOCATOR(AwsApiClientJobConfig, AZ::SystemAllocator);
  51. using AwsApiClientJobConfigType = AwsApiClientJobConfig<ClientType>;
  52. using InitializerFunction = AZStd::function<void(AwsApiClientJobConfigType& config)>;
  53. /// Initialize an AwsApiClientJobConfig object.
  54. ///
  55. /// \param defaultConfig - the config object that provides values when
  56. /// no override has been set in this object. The default is nullptr, which
  57. /// will cause a default value to be used.
  58. ///
  59. /// \param initializer - a function called to initialize this object.
  60. /// This simplifies the initialization of static instances. The default
  61. /// value is nullptr, in which case no initializer will be called.
  62. AwsApiClientJobConfig(AwsApiJobConfig* defaultConfig = nullptr, InitializerFunction initializer = nullptr)
  63. : AwsApiJobConfig{ defaultConfig }
  64. {
  65. if (initializer)
  66. {
  67. initializer(*this);
  68. }
  69. }
  70. ~AwsApiClientJobConfig() override = default;
  71. /// Gets a client initialized used currently applied settings. If
  72. /// any settings change after first use, code must call
  73. /// ApplySettings before those changes will take effect.
  74. std::shared_ptr<ClientType> GetClient() override
  75. {
  76. EnsureSettingsApplied();
  77. return m_client;
  78. }
  79. protected:
  80. void ApplySettings() override
  81. {
  82. AwsApiJobConfig::ApplySettings();
  83. m_client = CreateClient();
  84. }
  85. /// Create a client configured using this object's settings. ClientType
  86. /// can be any of the AWS API service clients (e.g. LambdaClient, etc.).
  87. std::shared_ptr<ClientType> CreateClient() const;
  88. private:
  89. /// Set by ApplySettings
  90. std::shared_ptr<ClientType> m_client;
  91. };
  92. template<class ClientType>
  93. std::shared_ptr<ClientType> AwsApiClientJobConfig<ClientType>::CreateClient() const
  94. {
  95. auto provider = GetCredentialsProvider();
  96. if (provider != nullptr)
  97. {
  98. // Note: This constructor for AWS Client is marked for depreciation
  99. return std::make_shared<ClientType>(provider, GetClientConfiguration());
  100. }
  101. else
  102. {
  103. // If no explicit credentials are provided then AWS C++ SDK will perform standard search
  104. // Note: This constructor for AWS Client is marked for depreciation
  105. return std::make_shared<ClientType>(Aws::Auth::AWSCredentials(), GetClientConfiguration());
  106. }
  107. }
  108. template<>
  109. inline std::shared_ptr<Aws::S3::S3Client> AwsApiClientJobConfig<Aws::S3::S3Client>::CreateClient() const
  110. {
  111. auto signPayloads = Aws::Client::AWSAuthV4Signer::PayloadSigningPolicy::Never;
  112. bool useVirtualAddressing = false;
  113. auto provider = AwsApiJobConfig::GetCredentialsProvider();
  114. if (provider != nullptr)
  115. {
  116. // Note: This constructor for AWS Client is marked for deprecation
  117. return std::make_shared<Aws::S3::S3Client>(provider, GetClientConfiguration(), signPayloads, useVirtualAddressing);
  118. }
  119. else
  120. {
  121. // If no explicit credentials are provided then AWS C++ SDK will perform standard search
  122. // Note: This constructor for AWS Client is marked for deprecation
  123. return std::make_shared<Aws::S3::S3Client>(
  124. Aws::Auth::AWSCredentials(), GetClientConfiguration(), signPayloads, useVirtualAddressing);
  125. }
  126. }
  127. AZ_POP_DISABLE_WARNING
  128. } // namespace AWSCore