1/*
2* Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3*
4* Licensed under the Apache License, Version 2.0 (the "License").
5* You may not use this file except in compliance with the License.
6* A copy of the License is located at
7*
8* http://aws.amazon.com/apache2.0
9*
10* or in the "license" file accompanying this file. This file is distributed
11* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12* express or implied. See the License for the specific language governing
13* permissions and limitations under the License.
14*/
15
16#include <aws/s3/S3Endpoint.h>
17#include <aws/core/utils/memory/stl/AWSStringStream.h>
18#include <aws/core/utils/HashingUtils.h>
19
20using namespace Aws;
21using namespace Aws::S3;
22
23namespace Aws
24{
25namespace S3
26{
27namespace S3Endpoint
28{
29 static const int CN_NORTH_1_HASH = Aws::Utils::HashingUtils::HashString("cn-north-1");
30 static const int CN_NORTHWEST_1_HASH = Aws::Utils::HashingUtils::HashString("cn-northwest-1");
31
32 static const int FIPS_US_GOV_WEST_1_HASH = Aws::Utils::HashingUtils::HashString("fips-us-gov-west-1");
33 static const int US_GOV_WEST_1_HASH = Aws::Utils::HashingUtils::HashString("us-gov-west-1");
34 static const int US_GOV_EAST_1_HASH = Aws::Utils::HashingUtils::HashString("us-gov-east-1");
35 static const int US_EAST_1_HASH = Aws::Utils::HashingUtils::HashString("us-east-1");
36
37 Aws::String ForAccessPointArn(const S3ARN& arn, const Aws::String& regionNameOverride, bool useDualStack)
38 {
39 const Aws::String& region = regionNameOverride.empty() ? arn.GetRegion() : regionNameOverride;
40 auto hash = Aws::Utils::HashingUtils::HashString(region.c_str());
41
42 Aws::StringStream ss;
43 ss << arn.GetResourceId() << "-" << arn.GetAccountId() << ".s3-accesspoint.";
44 if (useDualStack)
45 {
46 ss << "dualstack.";
47 }
48 ss << region << "." << "amazonaws.com";
49
50 if (hash == CN_NORTH_1_HASH || hash == CN_NORTHWEST_1_HASH)
51 {
52 ss << ".cn";
53 }
54
55 return ss.str();
56 }
57
58 Aws::String ForRegion(const Aws::String& regionName, bool useDualStack, bool USEast1UseRegionalEndpoint)
59 {
60 auto hash = Aws::Utils::HashingUtils::HashString(regionName.c_str());
61
62 if(!useDualStack)
63 {
64 if(hash == FIPS_US_GOV_WEST_1_HASH)
65 {
66 return "s3-fips-us-gov-west-1.amazonaws.com";
67 }
68 if(hash == US_GOV_WEST_1_HASH)
69 {
70 return "s3.us-gov-west-1.amazonaws.com";
71 }
72 if(hash == US_GOV_EAST_1_HASH)
73 {
74 return "s3.us-gov-east-1.amazonaws.com";
75 }
76 if(hash == US_EAST_1_HASH)
77 {
78 if (USEast1UseRegionalEndpoint)
79 {
80 return "s3.us-east-1.amazonaws.com";
81 }
82 else
83 {
84 return "s3.amazonaws.com";
85 }
86 }
87 }
88 Aws::StringStream ss;
89 ss << "s3" << ".";
90
91 if(useDualStack)
92 {
93 ss << "dualstack.";
94 }
95
96 ss << regionName << ".amazonaws.com";
97
98 if (hash == CN_NORTH_1_HASH || hash == CN_NORTHWEST_1_HASH)
99 {
100 ss << ".cn";
101 }
102
103 return ss.str();
104 }
105
106} // namespace S3Endpoint
107} // namespace S3
108} // namespace Aws