Ivory
Ivory

Reputation: 67

How do I convert an InputStream to S3ObjectInputStream in Java?

I want to convert a FileInputStream to a S3ObjectInputStream.

This is my current code:

InputStream is = new FileInputStream(file);
S3ObjectInputStream sObject = (S3ObjectInputStream)is;

However, it's giving the error:

java.io.FileInputStream cannot be cast to com.amazonaws.services.s3.model.S3ObjectInputStream

Upvotes: 6

Views: 10521

Answers (3)

Peter
Peter

Reputation: 5884

Use the constructor

new S3ObjectInputStream(InputStream in, ...

I used aws-java-sdk version 1.11.526

Upvotes: 1

Use the below constructor to convert an InputStream to a S3ObjectInputStream:

var s3ObjectInputStream = new S3ObjectInputStream(inputStream, null);

You can safely use null for the 2nd argument in the constructor (HttpRequestBase) as the abort method which uses it, isn't available in the cast InputStream.

Upvotes: 6

You need cast as a InputStream

InputStream is = new FileInputStream(file);
S3ObjectInputStream sObject = (InputStream) is;

Hope it helps

Upvotes: -6

Related Questions