6

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
Ermiya Eskandary
  • 15,323
  • 3
  • 31
  • 44
Ivory
  • 67
  • 1
  • 7

3 Answers3

6

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.

Ermiya Eskandary
  • 15,323
  • 3
  • 31
  • 44
1

Use the constructor

new S3ObjectInputStream(InputStream in, ...

I used aws-java-sdk version 1.11.526

Ermiya Eskandary
  • 15,323
  • 3
  • 31
  • 44
Peter
  • 5,556
  • 3
  • 23
  • 38
-6

You need cast as a InputStream

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

Hope it helps

  • Is it possible to do the opposite? Can I convert S3ObjectInputStream to FileInputStream? – Sonu Mishra May 06 '17 at 00:54
  • This gives a `ClassCastException`. How is this answer accepted? – ares Dec 26 '17 at 07:56
  • 5
    No, actually, the code won't even compile. You can't assign an `InputStream` to `S3ObjectInputStream` And the cast is redundant... `is` is already declared to be `InputStream`.. – Harald K May 07 '18 at 19:27