简体   繁体   中英

How to convert FileInputStream to S3ObjectInputStream

I want to convert the File InputStream to S3ObjectInputStream, I tried it in the below way but it did not work out and I am getting

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

This is my code

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

This is how you can convert inputStream to S3Object inputstream. Make sure you have the mock S3 file present on src/test/resources folder

private S3ObjectInputStream getMockS3Content() {
        InputStream inputStream = Thread.currentThread()
            .getContextClassLoader()
            .getResourceAsStream("mockS3content.json");
        S3ObjectInputStream s3ObjectInputStream = new S3ObjectInputStream(inputStream,null);
        return s3ObjectInputStream;
    }

Use the constructor

new S3ObjectInputStream(InputStream in, ...

I used aws-java-sdk version 1.11.526

You need cast as a InputStream

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

Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM