1

AWS has been deprecated the "new AmazonS3Client()" to "AmazonS3ClientBuilder.defaultClient()". The "new AmazonS3Client()" returns "AmazonS3Client" whereas, "AmazonS3ClientBuilder.defaultClient()" returns "AmazonS3".

Solution1: Cast the 'AmazonS3' object to 'AmazonS3Client' like: method((AmazonS3Client) AmazonS3ClientBuilder.defaultClient())

Solution2: Change the parameters from 'AmazonS3Client' to 'AmazonS3', like: existing code: method(AmazonS3Client amazonS3Client) new code: method(AmazonS3 amazonS3)

But I'm not comfortable to change solution 2, as changing this will cost me a-lot for efforts and time for testing.

Question/Ask : Is there any other way to do this?

Amit
  • 229
  • 5
  • 20

1 Answers1

3

I would say these are the only options for you. As you can see from the class hierarchy AmazonS3 is an interface and AmazonS3Client is one implementation of it (There is one more implementation AmazonS3EncryptionClient). It is always recommended to program to an interface rather than programming to an implementation. What this means is that you should have had (and is always good) the type as AmazonS3.

Option 1

If you do the typecasting of AmazonS3 to AmazonS3Client, it would work for you as I don't really see it returning any other subclass of AmazonS3. But it is good to avoid this as things could change in the future (but Amazon will most probably not do it as they would know that clients would take this approach - but this is a probability if they are willing to break their clients).

Option2:

Your second options is the best as you not only fix the problem but also you clean up your code.

Thiyagu
  • 17,362
  • 5
  • 42
  • 79