use of com.amazonaws.services.s3.model.Region in project stocator by CODAIT.
the class COSUtils method translateException.
/**
* Translate an exception raised in an operation into an IOException. The
* specific type of IOException depends on the class of
* {@link AmazonClientException} passed in, and any status codes included in
* the operation. That is: HTTP error codes are examined and can be used to
* build a more specific response.
*
* @param operation operation
* @param path path operated on (may be null)
* @param exception amazon exception raised
* @return an IOE which wraps the caught exception
*/
@SuppressWarnings("ThrowableInstanceNeverThrown")
public static IOException translateException(String operation, String path, AmazonClientException exception) {
String message = String.format("%s%s: %s", operation, path != null ? (" on " + path) : "", exception);
if (!(exception instanceof AmazonServiceException)) {
if (containsInterruptedException(exception)) {
return (IOException) new InterruptedIOException(message).initCause(exception);
}
return new COSClientIOException(message, exception);
} else {
IOException ioe;
AmazonServiceException ase = (AmazonServiceException) exception;
// this exception is non-null if the service exception is an COS one
AmazonS3Exception s3Exception = ase instanceof AmazonS3Exception ? (AmazonS3Exception) ase : null;
int status = ase.getStatusCode();
switch(status) {
case 301:
if (s3Exception != null) {
if (s3Exception.getAdditionalDetails() != null && s3Exception.getAdditionalDetails().containsKey(ENDPOINT_KEY)) {
message = String.format("Received permanent redirect response to " + "endpoint %s. This likely indicates that the COS endpoint " + "configured in %s does not match the region containing " + "the bucket.", s3Exception.getAdditionalDetails().get(ENDPOINT_KEY), ENDPOINT_URL);
}
ioe = new COSIOException(message, s3Exception);
} else {
ioe = new COSServiceIOException(message, ase);
}
break;
// permissions
case 401:
case 403:
ioe = new AccessDeniedException(path, null, message);
ioe.initCause(ase);
break;
// the object isn't there
case 404:
case 410:
ioe = new FileNotFoundException(message);
ioe.initCause(ase);
break;
// a shorter one while it is being read.
case 416:
ioe = new EOFException(message);
break;
default:
// no specific exit code. Choose an IOE subclass based on the class
// of the caught exception
ioe = s3Exception != null ? new COSIOException(message, s3Exception) : new COSServiceIOException(message, ase);
break;
}
return ioe;
}
}
use of com.amazonaws.services.s3.model.Region in project jackrabbit-oak by apache.
the class Utils method openService.
/**
* Create AmazonS3Client from properties.
*
* @param prop properties to configure @link {@link AmazonS3Client}
* @return {@link AmazonS3Client}
*/
public static AmazonS3Client openService(final Properties prop) {
String accessKey = prop.getProperty(S3Constants.ACCESS_KEY);
String secretKey = prop.getProperty(S3Constants.SECRET_KEY);
AmazonS3Client s3service = null;
if (StringUtils.isNullOrEmpty(accessKey) || StringUtils.isNullOrEmpty(secretKey)) {
LOG.info("Configuring Amazon Client from environment");
s3service = new AmazonS3Client(getClientConfiguration(prop));
} else {
LOG.info("Configuring Amazon Client from property file.");
AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
s3service = new AmazonS3Client(credentials, getClientConfiguration(prop));
}
String region = prop.getProperty(S3Constants.S3_REGION);
String endpoint = null;
String propEndPoint = prop.getProperty(S3Constants.S3_END_POINT);
if ((propEndPoint != null) & !"".equals(propEndPoint)) {
endpoint = propEndPoint;
} else {
if (StringUtils.isNullOrEmpty(region)) {
com.amazonaws.regions.Region s3Region = Regions.getCurrentRegion();
if (s3Region != null) {
region = s3Region.getName();
} else {
throw new AmazonClientException("parameter [" + S3Constants.S3_REGION + "] not configured and cannot be derived from environment");
}
}
if (DEFAULT_AWS_BUCKET_REGION.equals(region)) {
endpoint = S3 + DOT + AWSDOTCOM;
} else if (Region.EU_Ireland.toString().equals(region)) {
endpoint = "s3-eu-west-1" + DOT + AWSDOTCOM;
} else {
endpoint = S3 + DASH + region + DOT + AWSDOTCOM;
}
}
/*
* setting endpoint to remove latency of redirection. If endpoint is
* not set, invocation first goes us standard region, which
* redirects it to correct location.
*/
s3service.setEndpoint(endpoint);
LOG.info("S3 service endpoint [{}] ", endpoint);
return s3service;
}
use of com.amazonaws.services.s3.model.Region in project photon-model by vmware.
the class AWSReservedInstancePlanService method getRegionAsyncHandler.
private AsyncHandler<DescribeRegionsRequest, DescribeRegionsResult> getRegionAsyncHandler(AWSReservedInstanceContext context) {
AWSReservedInstancePlanService service = this;
return new AsyncHandler<DescribeRegionsRequest, DescribeRegionsResult>() {
@Override
public void onError(Exception e) {
log(Level.WARNING, "Error while fetching the regions for compute " + context.computeDesc.documentSelfLink + " " + Utils.toString(e));
}
@Override
public void onSuccess(DescribeRegionsRequest request, DescribeRegionsResult describeRegionsResult) {
List<Region> regions = describeRegionsResult.getRegions();
if (CollectionUtils.isEmpty(regions)) {
log(Level.INFO, "No regions exist for compute" + context.computeDesc.documentSelfLink);
return;
}
AtomicInteger currentStageTaskCount = new AtomicInteger(regions.size());
/*
* Fetch all the regions from AWS and collect reserved instances plans for
* only those regions which are supported by SDK.
*/
for (Region region : regions) {
try {
Regions r = Regions.fromName(region.getRegionName());
if (r == null) {
new AWSReservedInstanceAsyncHandler(service.getHost(), currentStageTaskCount, region, context).checkAndPatchReservedInstancesPlans();
continue;
}
} catch (Exception e) {
log(Level.WARNING, e.getMessage());
new AWSReservedInstanceAsyncHandler(service.getHost(), currentStageTaskCount, region, context).checkAndPatchReservedInstancesPlans();
continue;
}
service.ec2ClientManager.getOrCreateEC2ClientAsync(context.parentAuth, region.getRegionName(), service).whenComplete((ec2Client, t) -> {
if (t != null) {
getFailureConsumer(context, "Error while creating EC2 client for").accept(t);
new AWSReservedInstanceAsyncHandler(service.getHost(), currentStageTaskCount, region, context).checkAndPatchReservedInstancesPlans();
logWarning("client is null for region %s for compute %s", region.getRegionName(), context.computeDesc.documentSelfLink);
return;
}
ec2Client.describeReservedInstancesAsync(new AWSReservedInstanceAsyncHandler(service.getHost(), currentStageTaskCount, region, context));
});
}
}
};
}
use of com.amazonaws.services.s3.model.Region in project photon-model by vmware.
the class AWSUtils method getS3TransferManager.
/**
* Method to get an S3 transfer manager client.
*
* Note: ARN-based credentials will not work unless they have already been exchanged to
* AWS for session credentials. If unset, this method will fail. To enable ARN-based
* credentials, migrate to {@link #getS3TransferManagerAsync(AuthCredentialsServiceState,
* String, ExecutorService)}
*
* @param credentials An {@link AuthCredentialsServiceState} object.
* @param region The region to get the AWS client in.
* @param executorService The executor service to run async services in.
*/
public static TransferManager getS3TransferManager(AuthCredentialsServiceState credentials, String region, ExecutorService executorService) {
AmazonS3ClientBuilder amazonS3ClientBuilder = AmazonS3ClientBuilder.standard().withCredentials(getAwsStaticCredentialsProvider(credentials)).withForceGlobalBucketAccessEnabled(true);
if (region == null) {
region = Regions.DEFAULT_REGION.getName();
}
if (isAwsS3Proxy()) {
AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration(getAwsS3ProxyHost(), region);
amazonS3ClientBuilder.setEndpointConfiguration(endpointConfiguration);
} else {
amazonS3ClientBuilder.setRegion(region);
}
TransferManagerBuilder transferManagerBuilder = TransferManagerBuilder.standard().withS3Client(amazonS3ClientBuilder.build()).withExecutorFactory(() -> executorService).withShutDownThreadPools(false);
return transferManagerBuilder.build();
}
use of com.amazonaws.services.s3.model.Region in project photon-model by vmware.
the class AWSClientManager method getOrCreateCloudWatchClient.
/**
* Get or create a CloudWatch Client instance that will be used to get stats from AWS.
*
* Note: ARN-based credentials will not be accepted unless they have already been exchanged to
* AWS for session credentials. If unset, this method will throw a
* {@link UnsupportedOperationException} exception in this circumstance. To enable ARN-based
* credentials, migrate to {@link #getOrCreateCloudWatchClientAsync(AuthCredentialsServiceState,
* String, StatelessService, boolean)}.
*
* @param credentials The auth credentials to be used for the client creation
* @param regionId The region of the AWS client
* @param service The stateless service for which the operation is being performed.
* @param isMock Indicates if this a mock request
* @return
*/
public AmazonCloudWatchAsyncClient getOrCreateCloudWatchClient(AuthCredentialsServiceState credentials, String regionId, StatelessService service, boolean isMock, Consumer<Throwable> failConsumer) {
if (this.awsClientType != AwsClientType.CLOUD_WATCH) {
throw new UnsupportedOperationException("This client manager supports only AWS " + this.awsClientType + " clients.");
}
if (isArnCredentials(credentials) && !isSetCredentials(credentials)) {
throw new UnsupportedOperationException("For ARN-based credentials, exchange for session-based access key/secret key first before retrieving the client.");
}
String cacheKey = createCredentialRegionCacheKey(credentials, regionId);
if (isCloudWatchClientInvalid(cacheKey)) {
failConsumer.accept(new IllegalStateException("Invalid cloud watch client for key: " + cacheKey));
return null;
}
AmazonCloudWatchAsyncClient amazonCloudWatchClient = null;
try {
amazonCloudWatchClient = this.cloudWatchClientCache.computeIfAbsent(cacheKey, key -> {
AmazonCloudWatchAsyncClient client = AWSUtils.getStatsAsyncClient(credentials, regionId, getExecutor(), isMock);
client.describeAlarmsAsync(new AsyncHandler<DescribeAlarmsRequest, DescribeAlarmsResult>() {
@Override
public void onError(Exception exception) {
markCloudWatchClientInvalid(service, cacheKey);
}
@Override
public void onSuccess(DescribeAlarmsRequest request, DescribeAlarmsResult result) {
// noop
}
});
return client;
});
} catch (Throwable e) {
service.logSevere(e);
failConsumer.accept(e);
}
return amazonCloudWatchClient;
}
Aggregations