如何将FileInputStream转换为S3ObjectInputStream

时间:2014-12-12 13:32:18

标签: amazon-s3 inputstream fileinputstream

我想将File InputStream转换为S3ObjectInputStream,我尝试了以下方式,但它没有成功,我得到了

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

这是我的代码

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

3 个答案:

答案 0 :(得分:1)

这是将 inputStream 转换为 S3Object inputstream 的方法。确保 src/test/resources 文件夹中存在模拟 S3 文件

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

答案 1 :(得分:0)

您需要投射为InputStream

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

希望有所帮助

答案 2 :(得分:0)

使用构造函数

new S3ObjectInputStream(InputStream in, ...

我使用了aws-java-sdk版本1.11.526