software.amazon.awssdk.services.s3.internal.settingproviders.UseArnRegionProviderChain Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of s3 Show documentation
Show all versions of s3 Show documentation
The AWS Java SDK for Amazon S3 module holds the client classes that are used for communicating with
Amazon Simple Storage Service
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package software.amazon.awssdk.services.s3.internal.settingproviders;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.profiles.ProfileFile;
import software.amazon.awssdk.profiles.ProfileFileSystemSetting;
import software.amazon.awssdk.utils.Logger;
/**
* {@link UseArnRegionProvider} implementation that chains together multiple useArnRegion providers.
*/
@SdkInternalApi
public final class UseArnRegionProviderChain implements UseArnRegionProvider {
private static final Logger log = Logger.loggerFor(UseArnRegionProvider.class);
private final List providers;
private UseArnRegionProviderChain(List providers) {
this.providers = providers;
}
/**
* Creates a default {@link UseArnRegionProviderChain}.
*
*
* AWS use arn region provider that looks for the useArnRegion in this order:
*
*
* - Check if 'aws.s3UseArnRegion' system property is set.
* - Check if 'AWS_USE_ARN_REGION' environment variable is set.
* - Check if 's3_use_arn_region' profile file configuration is set.
*
*/
public static UseArnRegionProviderChain create() {
return create(ProfileFile::defaultProfileFile,
ProfileFileSystemSetting.AWS_PROFILE.getStringValueOrThrow());
}
public static UseArnRegionProviderChain create(ProfileFile profileFile, String profileName) {
return new UseArnRegionProviderChain(Arrays.asList(SystemsSettingsUseArnRegionProvider.create(),
ProfileUseArnRegionProvider.create(profileFile, profileName)));
}
public static UseArnRegionProviderChain create(Supplier profileFile, String profileName) {
return new UseArnRegionProviderChain(Arrays.asList(SystemsSettingsUseArnRegionProvider.create(),
ProfileUseArnRegionProvider.create(profileFile, profileName)));
}
@Override
public Optional resolveUseArnRegion() {
for (UseArnRegionProvider provider : providers) {
try {
Optional useArnRegion = provider.resolveUseArnRegion();
if (useArnRegion.isPresent()) {
return useArnRegion;
}
} catch (Exception ex) {
log.warn(() -> "Failed to retrieve useArnRegion from " + provider, ex);
}
}
return Optional.empty();
}
}