AmazonS3Client(credentials) is deprecated

57,879

Solution 1

You can either use AmazonS3ClientBuilder or AwsClientBuilder as alternatives.

For S3, simplest would be with AmazonS3ClientBuilder,

BasicAWSCredentials creds = new BasicAWSCredentials("access_key", "secret_key"); 
AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(creds)).build();

Solution 2

Use the code listed below to create an S3 client without credentials:

AmazonS3 s3Client = AmazonS3ClientBuilder.standard().build();

An usage example would be a lambda function calling S3.

Solution 3

You need to pass the region information through the

com.amazonaws.regions.Region object.

Use AmazonS3Client(credentials, Region.getRegion(Regions.REPLACE_WITH_YOUR_REGION))

Solution 4

You can create S3 default client as follows(with aws-java-sdk-s3-1.11.232):

AmazonS3ClientBuilder.defaultClient();

Solution 5

Deprecated with only creentials in constructor, you can use something like this:

 val awsConfiguration = AWSConfiguration(context)
 val awsCreds = CognitoCachingCredentialsProvider(context, awsConfiguration)
 val s3Client = AmazonS3Client(awsCreds, Region.getRegion(Regions.EU_CENTRAL_1))
Share:
57,879
Asif Ali
Author by

Asif Ali

Eat sleep code

Updated on December 17, 2021

Comments

  • Asif Ali
    Asif Ali over 2 years

    I'm trying to read the files available on Amazon S3, as the question explains the problem. I couldn't find an alternative call for the deprecated constructor.

    Here's the code:

    private String AccessKeyID = "xxxxxxxxxxxxxxxxxxxx";
    private String SecretAccessKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    private static String bucketName     = "documentcontainer";
    private static String keyName     = "test";
    //private static String uploadFileName    = "/PATH TO FILE WHICH WANT TO UPLOAD/abc.txt";
    
    AWSCredentials credentials = new BasicAWSCredentials(AccessKeyID, SecretAccessKey);
    
    void downloadfile() throws IOException
    {
    
        // Problem lies here - AmazonS3Client is deprecated
        AmazonS3 s3client = new AmazonS3Client(credentials);
            try {
            System.out.println("Downloading an object...");
            S3Object s3object = s3client.getObject(new GetObjectRequest(
                    bucketName, keyName));
            System.out.println("Content-Type: "  +
                    s3object.getObjectMetadata().getContentType());
            InputStream input = s3object.getObjectContent();
    
            BufferedReader reader = new BufferedReader(new InputStreamReader(input));
            while (true) {
                String line = reader.readLine();
                if (line == null) break;
    
                System.out.println("    " + line);
            }
            System.out.println();
        } catch (AmazonServiceException ase) {
              //do something
        } catch (AmazonClientException ace) {
            // do something
        }
     }
    

    Any help? If more explanation is needed please mention it. I have checked on the sample code provided in .zip file of SDK, and it's the same.

  • rockstarjindal
    rockstarjindal almost 7 years
    Was there any reason behind this deprecation, are there any benefits of using AmazonS3ClientBuilder.standard() vs new AmazonS3Client (from the previous SDKs)
  • bikeman868
    bikeman868 almost 7 years
    Both AmazonS3ClientBuilder and AwsClientBuilder are not present in the latest NuGet packages for .Net 4.0, but various methods are depreciated including StoredProfileAWSCredentials (which I am currently using). Does anyone know the recommended approach for .Net 4.0?
  • user3526918
    user3526918 over 6 years
    As you use AmazonS3ClientBuilder, myClient.setRegion() no longer works. You've got to AmazonS3ClientBuilder.standard().withCredential().withRegion‌​("eu-west-1").build(‌​);
  • Clive Sargeant
    Clive Sargeant over 6 years
    thanks but only worked for me when i included withRegion()
  • Amit Kumar
    Amit Kumar about 6 years
    AWSStaticCredentialsProvider is also depricated @franklinsijo
  • Priyadarshi Kunal
    Priyadarshi Kunal almost 6 years
    @rockstarjindal The client builder are better than old constructor in following ways. Clients created with builders are Immutable. Client must have explicit region. Builders provide a cleaner chaining API.
  • Priyadarshi Kunal
    Priyadarshi Kunal almost 6 years
    A client created via the builders must have a region that is defined either explicitly (i.e. by calling withRegion) or as part of the DefaultAwsRegionProviderChain. If the builder can’t determine the region for a client, an SdkClientException is thrown.
  • JaviOverflow
    JaviOverflow over 5 years
    Where is it getting the credentials from?
  • Sach
    Sach over 5 years
    From AWS documentaion at :docs.aws.amazon.com/sdk-for-java/v1/developer-guide/… You can do this in the following ways: 1.Use the default credential provider chain (recommended). 2.Use a specific credential provider or provider chain. 3.Supply the credentials yourself. It is recommended to use IAM roles. E.g. for AWS lambda, create an IAM role which has access to S3 and create the client in lambda code. Supply this Role while configuring your lambda, similarly for EC2 instance or ECS etc.
  • JaviOverflow
    JaviOverflow over 5 years
    could you explain this a little bit more? "1.Use the default credential provider chain"
  • android developer
    android developer over 3 years
    Where do I get the region from ? What is an example of it? And why does it need a region?
  • Abhay Pratap
    Abhay Pratap over 3 years
    Please see this link to find the region in AWS stackoverflow.com/questions/4249488/…
  • android developer
    android developer over 3 years
    What if I had this before: AmazonS3Client s3Client = new AmazonS3Client(new AnonymousAWSCredentials()); ? It has no region, no?