Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Java SDK - Unable to find a region via the region provider chain

I have gone through the question titled "Setting the AWS region programmatically 1" but it doesn't provide all the answers I need.

Q1: I'm getting a SDKClientException-Unable to find a region via the region provider chain. What am I doing wrong? or is there a typo that I missed.

public class CreateS3Bucket {  public static void main(String[] args) throws IOException {      BasicAWSCredentials creds = new BasicAWSCredentials("aws-access-key", "aws-secret-key");     AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(creds)).build();      Region region = Region.getRegion(Regions.US_EAST_1);     s3Client.setRegion(region);      try {         String bucketName = "testBucket" + UUID.randomUUID();         s3Client.createBucket(bucketName);         System.out.println("Bucket Created Successfully.");      } catch(AmazonServiceException awse) {          System.out.println("This means that your request made it AWS S3 but got rejected");         System.out.println("Error Message:" +awse.getMessage());         System.out.println("Error Message:" +awse.getErrorCode());         System.out.println("Error Message:" +awse.getErrorType());         System.out.println("Error Message:" +awse.getRequestId());      } catch (AmazonClientException ace) {          System.out.println("The Amazon Client encountered an Error with network Connectivity");         System.out.println("Error Message:" + ace.getMessage());     }   } 

}

Q2: What code changes needs to be done if I want to build a Lambda Function out of it? I'm aware how to create a lambda function and roles that it needs. Just need to know if the code that I have written needs to changed. How should I implement the LambdaFuctionHandler class as below:

import com.amazonaws.services.lambda.runtime.Context; import com.amazonaws.services.lambda.runtime.RequestHandler;   public class LambdaFunctionHandler implements RequestHandler<String, String> {  @Override public String handleRequest(String input, Context context) {     context.getLogger().log("Input: " + input);       return null; }  } 
like image 548
Codistan Avatar asked May 24 '17 07:05

Codistan


People also ask

Could not connect to repository unable to load AWS credentials from any provider in the chain?

You are getting this exception because your AWS SDK is unable to load your credentials. What you should do is goto Preferences then goto AWS and add your secret key and access key. So that your project can retrieve both keys.

How do I find my AWS region?

To find your Regions using the consoleOpen the Amazon EC2 console at https://console.aws.amazon.com/ec2/ . From the navigation bar, view the options in the Region selector. Your EC2 resources for this Region are displayed on the EC2 Dashboard in the Resources section.

What is my default region name AWS?

Region. The Default region name identifies the AWS Region whose servers you want to send your requests to by default. This is typically the Region closest to you, but it can be any Region. For example, you can type us-west-2 to use US West (Oregon).


1 Answers

Regarding Q1, try to build your client using the following syntax:

AmazonS3 amazonS3 = AmazonS3Client.builder()     .withRegion("us-east-1")     .withCredentials(new AWSStaticCredentialsProvider(creds))     .build(); 
like image 119
mapm Avatar answered Sep 25 '22 07:09

mapm