ArchiveTransferManagerBuilder Unable to find a region via the region provider chain - java

The AWS Glacier API gives me an error about not finding the region even when I specify it specifically:
EndpointConfiguration endpointConfig = new EndpointConfiguration("https://glacier.us-east-2.amazonaws.com/", "us-east-2");
AmazonGlacier glacierClient = AmazonGlacierClientBuilder.standard()
.withEndpointConfiguration(endpointConfig)
.withCredentials(credentials)
.build();
ArchiveTransferManager xferMgr = new ArchiveTransferManagerBuilder()
.withGlacierClient(glacierClient)
.build();
UploadResult result = xferMgr.upload("Data_Full", "my archive " + (new Date()), new File("C:\\myBigFile"));
I get this stack trace:
com.amazonaws.SdkClientException: Unable to find a region via the region provider chain. Must provide an explicit region in the builder
or setup environment to supply a region. at
com.amazonaws.client.builder.AwsClientBuilder.setRegion(AwsClientBuilder.java:371)
at
com.amazonaws.client.builder.AwsClientBuilder.configureMutableProperties(AwsClientBuilder.java:337)
at
com.amazonaws.client.builder.AwsSyncClientBuilder.build(AwsSyncClientBuilder.java:46)
at
com.amazonaws.services.sqs.AmazonSQSClientBuilder.defaultClient(AmazonSQSClientBuilder.java:44)
at
com.amazonaws.services.glacier.transfer.ArchiveTransferManagerBuilder.resolveSQSClient(ArchiveTransferManagerBuilder.java:129)
at
com.amazonaws.services.glacier.transfer.ArchiveTransferManagerBuilder.getParams(ArchiveTransferManagerBuilder.java:135)
at
com.amazonaws.services.glacier.transfer.ArchiveTransferManagerBuilder.build(ArchiveTransferManagerBuilder.java:143)
Note that I use the API to list vaults and it works:
AmazonGlacierClientBuilder clientbuilder = AmazonGlacierClientBuilder.standard();
EndpointConfiguration endpointConfig = new EndpointConfiguration("https://glacier.us-east-2.amazonaws.com/", "us-east-2");
clientbuilder.withEndpointConfiguration(endpointConfig);
ProfilesConfigFile cf = new ProfilesConfigFile();
AWSCredentialsProvider credentials = new ProfileCredentialsProvider(cf, "My AWS Profile Name");
clientbuilder.setCredentials(credentials);
AmazonGlacier glacierClient = CustomAmazonGlacierClientBuilder.buildCustomAmazonGlacierClient();
ListVaultsRequest request = new ListVaultsRequest();
ListVaultsResult result = glacierClient.listVaults(request);
I recently downloaded the AWS / Glacier libraries as an Eclipse plugin. It shows the .jar version of aws-java-sdk-opensdk-1.11.130.jar
Does anyone have any insight as to what I could put in the code to satisfy the region requirement? I'd rather do it programmatically

I solved this by adding the AWS_REGION environment variable. E.g. us-east-2. When using Eclipse, you can add this using the Run --> Run Configurations.
I also updated the Eclipse and AWS Eclipse plugins using the Eclipse Help --> Check for Updates feature.

Related

I tried to retrieve keyvault secret by appservice using MSI in my local machine but got the error:java.net.ConnectException:Connection refused:connect

This current code is giving me the error error:java.net.ConnectException:Connection refused:connect
AppServiceMSICredentials msiCredentials = new AppServiceMSICredentials(AzureEnvironment.AZURE,msiEndpoint, msiSecret);
keyVaultClient = new KeyVaultClient(msiCredentials);
SecretBundle secretBundle = keyVaultClient.getSecret(keyvault url, secretName);
I test the sample in my local, and face the same error. But I can't find detailed information if AppServiceMSICredentials supports access key vault locally.
You could try the official tutorial using a managed identity to connect Key Vault to an Azure Spring Cloud app.
The tutorial is based on Azure Identity library, and you could learn more about it with Java: https://learn.microsoft.com/en-us/azure/developer/java/sdk/identity-azure-hosted-auth.
private void getSecretWithManagedIdentity() {
ManagedIdentityCredential managedIdentityCredential = new ManagedIdentityCredentialBuilder()
.maxRetry(1)
.retryTimeout(duration -> Duration.ofMinutes(1))
.build();
secretClient = new SecretClientBuilder()
.vaultUrl(keyVaultUrl)
.credential(managedIdentityCredential)
.buildClient();
KeyVaultSecret secret = secretClient.getSecret(secret-name);
}

How to access AWS services on local machine(using eclipse)?

Exception in thread "main" com.amazonaws.SdkClientException: Unable to find a region via the region provider chain. Must provide an explicit region in the builder or setup environment to supply a region.
at com.amazonaws.client.builder.AwsClientBuilder.setRegion(AwsClientBuilder.java:371)
The Error message itself self sufficient and the error message clearly explains that you had not set the amazonAWSRegion when you are building Amazon service client. As your question does not clearly depicts which AWS service your are trying to connect through your code, I have shown a sample example to build AWS dynamoDB client.
when you were building AmazonDynamoDB client instance use the below code sample.
String amazonAWSAccessKey = "yourAmazonAWSAccessKey";
String amazonAWSSecretKey = "yourAmazonAWSSecretKey";
String amazonDynamoDBEndpoint = "AmazonDynamoDBEndpoint";
String amazonAWSRegion = "amazonAWSRegion"; //(ex: us-east-1/us-west-1)
AWSStaticCredentialsProvider awsCredentialsProvider = new AWSStaticCredentialsProvider(new BasicAWSCredentials(
amazonAWSAccessKey, amazonAWSSecretKey));
AmazonDynamoDB amazonDynamoDB = AmazonDynamoDBClientBuilder.standard()
.withCredentials(awsCredentialsProvider)
.withEndpointConfiguration(new AwsClientBuilder
.EndpointConfiguration(amazonDynamoDBEndpoint, amazonAWSRegion)).build();
If you want to connect the AWS service through Eclipse, the configuration setup is documented at Set up AWS Toolkit

Restarting app server using AWS API

I need to restart my AWS app server, for this I tried to use AWS API and have done the following:
1) Used the aws java sdk maven repository
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-elasticbeanstalk</artifactId>
<version>1.11.86</version>
</dependency>
2) Used the below code segment:
AWSElasticBeanstalk client = new AWSElasticBeanstalkClient();
RestartAppServerRequest request = new RestartAppServerRequest()
.withEnvironmentId("<myEnvId>")
.withEnvironmentName("<myEnvName>");
RestartAppServerResult response = client.restartAppServer(request);
I get the below error:
com.amazonaws.services.elasticbeanstalk.model.AWSElasticBeanstalkException: No Environment found for EnvironmentId = ''. (Service: AWSElasticBeanstalk; Status Code: 400; Error Code: InvalidParameterValue; Request ID: 4d025449-ed00-11e6-8405-4d5eb8e5ecd9)
The <myEnvId> and <myEnvName> are correct as they are taken from the AWS dashboard.
I also tried including the aws.accessKeyId and aws.secretKey to java system properties. Still I get the same error.
Is there something I am missing or doing wrong? Please advice.
Thanks,
Clyde
It sounds like you need to configure the region. For example to configure the region to us-west-2 you would use the following code:
AWSElasticBeanstalk client = new AWSElasticBeanstalkClient();
client.configureRegion(Regions.US_WEST_2);
Thanks to all who posted. I manged to solve the issue. The code segment used is as follows:
AWSElasticBeanstalk client = new AWSElasticBeanstalkClient();
client.setEndpoint(<set your endpoint>);
RestartAppServerRequest request = new RestartAppServerRequest()
.withEnvironmentId(<set your env id>)
.withEnvironmentName(<set your env name>);
RestartAppServerResult response = client.restartAppServer(request);
This worked find.

AWS Polly Java Client gives error: Unable to load region information from any provider in the chain

I am using the JAVA SDK from AWS to create a Polly client.
Like this:
BasicAWSCredentials awsCreds = new BasicAWSCredentials("<IAM access Key>", "IAM secret key>");
AmazonPollyClient apClient = (AmazonPollyClient) AmazonPollyClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(awsCreds))
.build();
SynthesizeSpeechRequest tssRequest = new SynthesizeSpeechRequest();
tssRequest.setText(<text>);
tssRequest.setVoiceId(<voiceid>);
tssRequest.setOutputFormat(OutputFormat.Mp3);
SynthesizeSpeechResult tssResult = apClient.synthesizeSpeech(tssRequest);
When I run this code, I get the following error message:
Exception in thread "main" com.amazonaws.SdkClientException: Unable to
load region information from any provider in the chain at
com.amazonaws.regions.AwsRegionProviderChain.getRegion(AwsRegionProviderChain.java:56)
at
com.amazonaws.client.builder.AwsClientBuilder.setRegion(AwsClientBuilder.java:319)
at
com.amazonaws.client.builder.AwsClientBuilder.configureMutableProperties(AwsClientBuilder.java:295)
at
com.amazonaws.client.builder.AwsSyncClientBuilder.build(AwsSyncClientBuilder.java:38)
at com.eoffice.aws.speech.Polly.main(Polly.java:42)
I checked the credentials using the IAM Policy Simulator. This works fine, permissions are OK.
The method to set the Region in the ClientBuilder is NOT visible for the AmazonPollyClientBuilder, so I have no (Java SDK) way to specify the region.
Update:
When I just ask the defaultAwsREgionProviderChain, I get the same error message
DefaultAwsRegionProviderChain defaultAwsRegionProviderChain = new DefaultAwsRegionProviderChain();
System.out.println(defaultAwsRegionProviderChain.getRegion());
Updat 2:
When I create a config file in de .aws folder with the following content:
[default]
region = eu-west-1
It works, but I need a way to set this without relying on the file system.
Providing a System Environment variable with name "AWS_REGION" did the trick.
See screenshot for configuration in IBM Bluemix
I think you can set Region like this
AmazonPollyClient apClient = (AmazonPollyClient) AmazonPollyClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(awsCreds)).withRegion("<aws-region>").build();

AWS S3 Java Embedded Mock for Integration Tests

After searching the internet for a good solution to an embedded Java AWS S3 mock it seemed that S3Ninja and S3Proxy seemed to be the most popular solutions.
However there doesn't seem to be an easy way to fire these up programmatically. After giving up with S3Ninja, I tried to do it with S3Proxy but it's not quite working.
Maven Dependencies
<dependency>
<groupId>org.gaul</groupId>
<artifactId>s3proxy</artifactId>
<version>${s3proxy.version}</version>
<scope>test</scope>
</dependency>
Code
String endpoint = "http://127.0.0.1:8085";
URI uri = URI.create(endpoint);
Properties properties = new Properties();
properties.setProperty("s3proxy.authorization", "none");
properties.setProperty("s3proxy.endpoint", endpoint);
properties.setProperty("jclouds.provider", "filesystem");
properties.setProperty("jclouds.filesystem.basedir", "/tmp/s3proxy");
ContextBuilder builder = ContextBuilder
.newBuilder("filesystem")
.credentials("x", "x")
.modules(ImmutableList.<Module>of(new SLF4JLoggingModule()))
.overrides(properties);
BlobStoreContext context = builder.build(BlobStoreContext.class);
BlobStore blobStore = context.getBlobStore();
S3Proxy s3Proxy = S3Proxy.builder().awsAuthentication("x", "x").endpoint(uri).keyStore("", "").blobStore(blobStore).build();
s3Proxy.start();
BasicAWSCredentials awsCredentials = new BasicAWSCredentials("x", "x");
AmazonS3Client client = new AmazonS3Client(awsCredentials, new ClientConfiguration());
client.setEndpoint(endpoint);
// Should Throw AWS Client Exception as Bucket / Key does not exist!
GetObjectRequest objectRequest = new GetObjectRequest("bucket", "key");
S3Object object = client.getObject(objectRequest);
s3Proxy.stop();
Exception
java.lang.NoSuchMethodError: com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.<init>(Lcom/google/gson/internal/ConstructorConstructor;Lcom/google/gson/FieldNamingStrategy;Lcom/google/gson/internal/Excluder;)V
at org.jclouds.json.internal.DeserializationConstructorAndReflectiveTypeAdapterFactory.<init>(DeserializationConstructorAndReflectiveTypeAdapterFactory.java:116)
at org.jclouds.json.config.GsonModule.provideGson(GsonModule.java:129)
...
at org.jclouds.providers.config.BindProviderMetadataContextAndCredentials.backend(BindProviderMetadataContextAndCredentials.java:84)
...
at org.jclouds.ContextBuilder.build(ContextBuilder.java:581)
Any help is truly appreciated. I'm sure this is a big requirement for many Java Integration Tests that interact with AWS S3.
Just to comment the reason is because your project is using a conflicting version of gson. S3Proxy's dep requires gson 2.5.
Maybe you give ladon-S3-server a chance.
Take a look at my github reference.
The core is based on a servlet and has very little dependencies.

Categories