我的目标是从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
答案 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();