我知道这可能是使用Streams的,但我不确定正确的语法。
我想将一个字符串传递给Save方法并让它gzip字符串并将其上传到Amazon S3,而不必写入磁盘。当前方法在两者之间低效地读取/写入磁盘。
S3 PutObjectRequest有一个带有InputStream输入的构造函数作为选项。
import java.io.*;
import java.util.zip.GZIPOutputStream;
import com.amazonaws.auth.PropertiesCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.PutObjectRequest;
public class FileStore {
public static void Save(String data) throws IOException
{
File file = File.createTempFile("filemaster-", ".htm");
file.deleteOnExit();
Writer writer = new OutputStreamWriter(new FileOutputStream(file));
writer.write(data);
writer.flush();
writer.close();
String zippedFilename = gzipFile(file.getAbsolutePath());
File zippedFile = new File(zippedFilename);
zippedFile.deleteOnExit();
AmazonS3 s3 = new AmazonS3Client(new PropertiesCredentials(
new FileInputStream("AwsCredentials.properties")));
String bucketName = "mybucket";
String key = "test/" + zippedFile.getName();
s3.putObject(new PutObjectRequest(bucketName, key, zippedFile));
}
public static String gzipFile(String filename) throws IOException
{
try {
// Create the GZIP output stream
String outFilename = filename + ".gz";
GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(outFilename));
// Open the input file
FileInputStream in = new FileInputStream(filename);
// Transfer bytes from the input file to the GZIP output stream
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
// Complete the GZIP file
out.finish();
out.close();
return outFilename;
} catch (IOException e) {
throw e;
}
}
}
答案 0 :(得分:7)
我会使用以下内容:
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
GZipOuputStream gzipOut = new GZipOutputStream(byteOut);
// write your stuff
byte[] bites = byteOut.toByteArray();
//write the bites to the amazon stream
您正在将压缩值写入字节流,然后获取字节值,您可以将其写入其他流。您还可以将流包装到amazon站点(即来自http连接的输出流或类似的东西)并避免整个ByteArrayOutputStream。
编辑:我注意到你的最后一句话 - 布鲁阿。您可以获取您创建的字节,使用它们创建ByteArrayInputStream,然后将其作为输入流传递:
ByteArrayInputStream byteInStream = new ByteArrayInputStream(bites);
如果我正在理解您正确描述的内容,它应该从输入流读取到输出流。否则,您只需写入输出流即可。
答案 1 :(得分:7)
这基本上是aperkins建议的。我不知道AS3的接口,所以他建议在字节数组上创建ByteArrayInputStream可能就是这样。
import java.io.*;
import java.util.zip.GZIPOutputStream;
import com.amazonaws.auth.PropertiesCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.PutObjectRequest;
public class FileStore {
public static void Save(String data) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Writer writer = new OutputStreamWriter(baos);
writer.write(data);
writer.flush();
writer.close();
byte[] zippedBytes = gzipFile(baos.toByteArray());
AmazonS3 s3 = new AmazonS3Client(new PropertiesCredentials(
new FileInputStream("AwsCredentials.properties")));
String bucketName = "mybucket";
String key = "test/" + zippedFile.getName();
s3.putObject(new PutObjectRequest(bucketName, key,
new ByteArrayInputStream(zippedBytes));
}
public static byte[] gzipFile(byte[] bytes) throws IOException {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream out = new GZIPOutputStream(baos);
out.write(bytes, 0, bytes.length);
// Complete the GZIP file
out.finish();
out.close();
return baos.toByteArray();
} catch (IOException e) {
throw e;
}
}
}