我的目标是:
- 从 S3 读取文件,
- 更改其元数据
- 再次推送到S3
AWS java SDK 不允许推送输出流。因此,我必须将outputstream
从步骤2到inputstream
。为此我决定使用PipedInputStream
.
但是,我的代码只是挂在writeTo(out);
步。此代码位于 Grails 应用程序中。当代码挂起时CPU不是处于高消耗状态:
import org.apache.commons.imaging.formats.jpeg.xmp.JpegXmpRewriter;
AmazonS3Client client = nfile.getS3Client() //get S3 client
S3Object object1 = client.getObject(
new GetObjectRequest("test-bucket", "myfile.jpg")) //get the object.
InputStream isNew1 = object1.getObjectContent(); //create input stream
ByteArrayOutputStream os = new ByteArrayOutputStream();
PipedInputStream inpipe = new PipedInputStream();
final PipedOutputStream out = new PipedOutputStream(inpipe);
try {
String xmpXml = "<x:xmpmeta>" +
"\n<Lifeshare>" +
"\n\t<Date>"+"some date"+"</Date>" +
"\n</Lifeshare>" +
"\n</x:xmpmeta>";/
JpegXmpRewriter rewriter = new JpegXmpRewriter();
rewriter.updateXmpXml(isNew1,os, xmpXml); //This is step2
try {
new Thread(new Runnable() {
public void run () {
try {
// write the original OutputStream to the PipedOutputStream
println "starting writeto"
os.writeTo(out);
println "ending writeto"
} catch (IOException e) {
// logging and exception handling should go here
}
}
}).start();
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(1024); //just testing
client.putObject(new PutObjectRequest("test-bucket", "myfile_copy.jpg", inpipe, metadata));
os.writeTo(out);
os.close();
out.close();
} catch (IOException e) {
// logging and exception handling should go here
}
}
finally {
isNew1.close()
os.close()
out.close()
}
上面的代码只是打印starting writeto
并挂起。它不打印ending writeto
Update通过把writeTo
在一个单独的线程中,文件现在被写入 S3,但是,仅写入了其中的 1024 个字节。该文件不完整。如何将输出流中的所有内容写入 S3?
当您执行 os.writeTo(out) 时,它会尝试将整个流刷新到out,并且由于还没有人从其另一侧(即内管道)读取数据,因此内部缓冲区已满并且线程停止。
您必须在写入数据之前设置读取器,并确保它在单独的线程中执行(请参阅 PipedOutputStream 上的 javadoc)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)