如何从S3Object获取输出流?

时间:2016-10-26 12:37:23

标签: java image-processing amazon-s3 aws-java-sdk apache-commons-imaging

我的目标是从S3获取对象(图像),更改文件的元数据,并将其替换为已更改元数据的新文件。

要更改我使用commons imaging library的元数据。我编写了下面的示例,它按预期工作,但不处理S3。

File newFile = new File("newImage2.jpg");
OutputStream os = new BufferedOutputStream(new FileOutputStream(newFile))
InputStream isNew = new BufferedInputStream(new FileInputStream(newFile))
InputStream is = new BufferedInputStream(new FileInputStream(new File("newImage.jpg")))
try {
            String xmpXml = "<x:xmpmeta>" +
            "\n<Lifeshare>" +
            "\n\t<Date>"+"some date"+"</Date>" +
            "\n\t<Latitude>"+"somelat"+"</Latitude>" +
            "\n\t<Longitude>"+"somelong"+"</Longitude>" +
            "\n\t<Altitude>"+"somealt"+"</Altitude>" +
            "\n\t<Z>"+"someZ"+"</Z>" +
            "\n\t<X>"+"someX"+"</X>" +
            "\n\t<Y>"+"Some y"+"</Y>" +
            "\n</Lifeshare>" +
            "\n</x:xmpmeta>";
            JpegXmpRewriter rewriter = new JpegXmpRewriter();
            rewriter.updateXmpXml(is,os, xmpXml);
            String newXmpXml = Imaging.getXmpXml(isNew, "newImage2.jpg");
            println newXmpXml
        }
finally {
   is.close()
   os.close()
}

上述工作正常,因为我可以在exiftool上运行newimage2.jpg并查看设置的元数据属性:

$ exiftool newImage2.jpg | grep "Lifeshare"
Lifeshare Date                  : some date
Lifeshare Latitude              : somelat
Lifeshare Longitude             : somelong
Lifeshare Altitude              : somealt
Lifeshare Z                     : someZ
Lifeshare X                     : someX
Lifeshare Y                     : Some y

问题

如何使用AWS S3 SDK在S3上使用对象执行相同的操作?上面的updateXmpXml方法需要OutputStream作为第二个参数。但是,我在AWS sdk http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/allclasses-noframe.html

中看不到任何输出流类

1 个答案:

答案 0 :(得分:3)

http://docs.aws.amazon.com/AmazonS3/latest/dev/RetrievingObjectUsingJava.html

使用apache ioutils

OutputStream os = new ByteArrayOutputStream();


    AmazonS3 s3Client = new AmazonS3Client(new ProfileCredentialsProvider());        
    S3Object object = s3Client.getObject(
                      new GetObjectRequest(bucketName, key));
    InputStream in= object.getObjectContent();
    IOUtils.copy(in, out);

    in.close();
    out.close();