构建FAST Web服务,使用jersey从aws s3下载图像

时间:2014-07-08 12:36:43

标签: java amazon-s3 jersey download

我正在尝试构建Web服务,使用jersey 1.18从aws s3下载图像

我有S3ObjectInputStream和文件。

我需要快速的方式来回溯图像,我的方式很慢(5秒) 这样做的正确方法是什么?

这是我的代码

import java.io.InputStream;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;

@Path("/getfile")
public class Temp3 {

    @GET
    @Produces("image/*")
    public Response getFile() throws IOException {

        System.out.println("in getfile");
        awsBL _bl = new awsBL();
        S3Object object = _bl.getFile("gps.png");
        //System.out.println("**meta:\n"+object.getObjectMetadata());
        InputStream objectContent = object.getObjectContent();


        InputStream reader = new BufferedInputStream(objectContent);
        File file = new File("localFilename");      
        OutputStream writer = new BufferedOutputStream(new FileOutputStream(file));

        int read = -1;

        while ( ( read = reader.read() ) != -1 ) {
            writer.write(read);
        }

        writer.flush();
        writer.close();
        reader.close();
        String filename = object.getKey();
        ResponseBuilder response = Response.ok(file);
        response.header("Content-Disposition",
            "attachment; filename="+filename);
        return response.build();

    }

}

1 个答案:

答案 0 :(得分:0)

第一步

static byte[] getBinaryData(String filename, String logId) {
    return S3_SDK.download(S3_SDK.getFilesBucket(),  "/foldername/" + filename, logId);
} 

第二步

 public static byte[] download(String bucketName, String name, String logId) {
    LOG.log(Level.INFO, "{0} :: start download process, bucketName: {1}, name: {2}", new Object[]{logId, bucketName, name});
    S3Object object = downloadAsS3Object(bucketName, name, logId);
    LOG.log(Level.INFO, "{0} :: download process returns, S3Object: {1}", new Object[]{logId, object});
    try {
        return IOUtils.toByteArray(object.getObjectContent());
    } catch (IOException ex) {
        LOG.log(Level.SEVERE, "{0} :: error download process, bucketName: {1}, name: {2}\n{3}", new Object[]{logId, bucketName, name, Utilities.getStackTrace(ex)});
    }
    return null;
}