I am trying to create an aws s3 bucket using the following java code.
AmazonS3 s3client = AmazonS3ClientBuilder.defaultClient();
s3client.setRegion(Region.getRegion(Regions.AP_SOUTH_1));
But I am getting the following error:
"exception": "com.amazonaws.SdkClientException",
"message": "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."
Am I trying to set region in an incorrect way? Please advice.
If you are not using any proxies and you already setup your credentials, you can use below code:
AmazonS3 s3client = AmazonS3ClientBuilder.standard()
.withRegion(Region.getRegion(Regions.AP_SOUTH_1));
But if you need to setup a proxy and manually set the credentials, you can use below code:
AWSCredentials cred = new BasicAWSCredentials(<accessKey>,<secretKey>);
AmazonS3 s3client = AmazonS3ClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(cred))
.withClientConfiguration(<your configuration>)
.withRegion(Region.getRegion(Regions.AP_SOUTH_1));
The reason you are getting the error is you have not setup AWS with Eclipse.
If you are using Eclipse as your IDE then read:
http://docs.aws.amazon.com/toolkit-for-eclipse/v1/user-guide/welcome.html
Once the profile is setup then
AmazonS3 s3 = new AmazonS3Client(new ProfileCredentialsProvider());
Region apSouth1 = Region.getRegion(Regions.AP_SOUTH_1);
s3.setRegion(apSouth1);
Also make sure to import:
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
Related
I am working on java project and using aws sdk version 2 library to use s3 services. I am using S3Client for using services like getObject(),getBucekt,listObject etc.
And I want to use S3TransactionManager for get logging process of upload and download file.
Here are my code sample:-
static String sAccessKey = "XXXXXXXXXX";
static String sSecretKey = "XXXXXXXXXXXXX";
static AwsBasicCredentials awsCreds = AwsBasicCredentials.create(
sAccessKey,
sSecretKey);
AwsCredentialsProvider awsCredentialsProvider = StaticCredentialsProvider.create(awsCreds);
static S3Client s3Client = S3Client.builder().region(Region.US_WEST_2)
.credentialsProvider(StaticCredentialsProvider.create(awsCreds))
.build();
S3TransferManager s3TransferManager = S3TransferManager
.builder()
.s3ClientConfiguration(S3ClientConfiguration.builder()
.region(Region.US_WEST_2)
.credentialsProvider(awsCredentialsProvider).build()).build();
S3TransferManager s3TransferManager1 = S3TransferManager.create();
Region region = Region.US_WEST_2;
S3ClientConfiguration s3ClientConfiguration =
S3ClientConfiguration.builder()
.region(region)
.credentialsProvider(awsCredentialsProvider)
.targetThroughputInGbps(20.0)
.build();
Upload upload =
s3TransferManager1.upload(b -> b.putObjectRequest(r -> r.bucket("SSSS").key("test.ppt"))
.source(Paths.get("fileToUpload.txt")));
My issue is that when I used both s3 and s3_transaction_manager jar then I am getting error while creating object of S3TransactionManger.
Error are:-
Exception in thread "main" java.lang.NoSuchMethodError: software.amazon.awssdk.utils.Validate.isPositiveOrNull(Ljava/lang/Double;Ljava/lang/String;)Ljava/lang/Double;
at software.amazon.awssdk.transfer.s3.S3ClientConfiguration.<init>(S3ClientConfiguration.java:49)
at software.amazon.awssdk.transfer.s3.S3ClientConfiguration.<init>(S3ClientConfiguration.java:37)
at software.amazon.awssdk.transfer.s3.S3ClientConfiguration$DefaultBuilder.build(S3ClientConfiguration.java:301)
at software.amazon.awssdk.transfer.s3.S3ClientConfiguration$DefaultBuilder.build(S3ClientConfiguration.java:243)
at software.amazon.awssdk.transfer.s3.internal.DefaultS3TransferManager$DefaultBuilder.<init>(DefaultS3TransferManager.java:405)
at software.amazon.awssdk.transfer.s3.internal.DefaultS3TransferManager$DefaultBuilder.<init>(DefaultS3TransferManager.java:404)
at software.amazon.awssdk.transfer.s3.internal.DefaultS3TransferManager.builder(DefaultS3TransferManager.java:364)
at software.amazon.awssdk.transfer.s3.S3TransferManager.builder(S3TransferManager.java:497)
at com.bucketexplorer.main.SdkTest.main(SdkTest.java:88)
I think util class from both jar have conflict.
Jar files which I am using are :-
s3-2.16.46.jar
s3-transfer-manager-2.17.257-PREVIEW.jar
Please suggest how can I used both jar together?
It is a conflict between the two libraries.
Upgrading s3-2.16.46.jar to the same version of s3-transfer-manager (without PREVIEW) will solve the problem.
I am doing a small Java project (not Android) that is used to load files into an Amazon S3 bucket and everything has been loaded correctly as follows:
private BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials("secrey-key", "access-key");
private final AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(basicAWSCredentials)).withRegion(Regions.REGION_U_KNOW).build();
s3Client.putObject("bucket-name", "concat-path", new File("path"));
But now I have to do the authentication through a Cognito IDENTITY_POOL_ID, and according to what I see in the documentation it is done through the class:AmazonCognitoIdentityClientBuilder
In this class there is a method called: getCredentials() and this method can return an object of type AWSCredentialsProvider.
Can someone help me understand all this and how to upload my files to the bucket with the credentials that I get from AmazonCognitoIdentityClientBuilder and my IDENTITY_POOL_ID? Please
Seeing as s3 client class got depreciated, I am trying to use this new static class provided to us by aws and it's confusing me quite a bit. I am creating a new client and passing it some false credentials, yet it does not throw any errors. Am I missing something here?
BasicAWSCredentials awsCreds = new BasicAWSCredentials("uselessinfo", "abcd1234");
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(awsCreds))
.withRegion(Regions.DEFAULT_REGION)
.build();
s3Client.getS3AccountOwner();
Also, even when I give correct credentials the getS3AccountOwner() method is unable to make a connection and keeps timing out.
What am I missing here?
For getting s3 client object i am using below code.
BasicAWSCredentials creds = new BasicAWSCredentials(key, S3secretKey);
AmazonS3 s3Client =AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(creds)).build();
Getting below errors
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.
I had to change to:
AmazonS3 client = AmazonS3ClientBuilder.standard()
.withRegion(Regions.US_EAST_1)
.withForceGlobalBucketAccess(true)
.build();
to emulate the "old" way (i.e. new AmazonS3Client() )
With a builder you need to provide your S3 bucket region using builder method, like .withRegion(Regions.US_EAST_1)
One way to do it with the 1.11.98 version of the sdk, in your code, you would do:
AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient();
And you need to have ~/.aws/credentials and ~/.aws/config files:
~/.aws/credentials contents:
[pca]
aws_access_key_id = KDDDJGIzzz3VVBXYA6Z
aws_secret_access_key = afafaoRDrJhzzzzg/Hhcccppeeddaf
[deault]
aws_access_key_id = AMVKNEIzzzNEBXYJ4m
aws_secret_access_key = bU4rUwwwhhzzzzcppeeddoRDrJhogA
~/.aws/config contents:
[default]
region = us-west-1
[pca]
region = us-west-1
Make sure they're readable, and that you export a profile if you have multiple as above before starting your service:
alper$ export AWS_PROFILE="pca"
There's a good description here
I am trying to upload a file to S3. The code to do so is below:
AmazonS3 s3Client = new AmazonS3Client(new ProfileCredentialsProvider());
String key = String.format(Constants.KEY_NAME + "/%s/%s", activity_id, aFile.getName());
s3Client.putObject(Constants.BUCKET_NAME, key, aFile.getInputStream(), new ObjectMetadata());
The problem I am having is that my ProfileCredentialsProvider cannot access my AWS keys. I have set my environment variables:
AWS_ACCESS_KEY=keys go here
AWS_SECRET_KEY=keys go here
AWS_ACCESS_KEY_ID=keys go here
AWS_DEFAULT_REGION=us-east-1
AWS_SECRET_ACCESS_KEY=keys go here
And as per Amazon's Documentation the set environment variables have precedence over any configuration files. This leads me to ask, why are my keys not being grabbed from my environment variables?
Figured it out.
If you specify a ProfileCredentialsProvider() the AWS SDK will look for a configuration file, regardless of precedence. Simply creating a S3 Client like this:
AmazonS3 s3Client = new AmazonS3Client();
Will check the various locations specified for credentials.