2016-07-29 58 views
0

我需要一個servlet從Amazon S3服務器返回文件。只有服務器具有訪問憑據,S3存儲桶不公開。我無法改變這一點。我被告知要使用數據流,但速度很慢。 爲了測試,我有一個縮略圖的小項目,當你點擊它時,它會打開一個帶有完整圖像的新選項卡。 5mb圖像大約需要一分鐘才能加載。那很慢。從亞馬遜S3讀取的Servlet太慢了

從S3讀取並返回該數據流的功能:

public void downloadDirectlyFromS3(String s3Path, String fileName, HttpServletResponse response) { 
    AmazonS3 s3Client = new AmazonS3Client(new ProfileCredentialsProvider()); 
    s3Client.setEndpoint(S3ENDPOINT); 

    S3Object s3object = s3Client.getObject(new GetObjectRequest(s3Path, fileName)); 

    byte[] buffer = new byte[5 * 1024 * 1024]; 

    try { 
     InputStream input = s3object.getObjectContent(); 
     ServletOutputStream output = response.getOutputStream(); 
     for (int length = 0; (length = input.read(buffer)) > 0;) { 
      output.write(buffer, 0, length); 
     } 
     output.close(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

回答

1

我找到了答案。問題是記錄器。我們使用log4j並將其設置爲調試,因此流的所有跟蹤都已寫入控制檯。 以防萬一它發生在別人的時候,這裏就是他們說,這在生產中應儘量避免鏈接: https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/java-dg-logging.html#verbose-wire-logging

而且我也使用TransferManager作爲saravanakumar v說開始,似乎稍快。

例如我在這個示例響應了這個IP到country.bin下載的外觀,同時調試是在

2017-05-23 12:06:21,770 Wire (Wire.java:86) DEBUG - http-outgoing-0 << "[0xf0][0x1]BGY[0xb][0x0][0x1]ITY[0xb][0x10][0x1]I" 
2017-05-23 12:06:21,824 Wire (Wire.java:72) DEBUG - http-outgoing-0 << "RY[0xb] [0x1]CHY[0xb]0[0x1]DEY[0xb]@[0x1]KZY[0xb]P[0x1]ITY[0xb]`[0x1]ESY[0xb][0x80][0x1]PLY[0xb][0xa0][0x1]GEY[0xb][0xb0][0x1]TJY[0xb][0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1]RSY[0x10][0x90][0x1]ESY[0x10][0xa0][0x1]DEY[0x10][0xb0][0x1]CZY[0x10][0xc0][0x1]SEY[0x10][0xd0][0x1]GBY[0x10][0xe0][0x1]CYY[0x10][0xf0][0x1]ESY[0x11][0x0][0x1]NOY[0x11][0x10][0x1]DEY[0x11] [0x1]PLY[0x11]0[0x1]BGY[0x11]@[0x1]SEY[0x11]P[0x1]LTY[0x11]`[0x1]RSY[0x11]p[0x1]RUY[0x11][0x80][0x1]NLY[0x11][0x90][0x1]TRY[0x11][0xa0][0x1]RUY[0x11] 
2
+0

THX,我會嘗試一下,如果批准的回答這個問題的作品;) –

+0

我一直在度假,很抱歉花了這麼長時間來回答。它沒有解決問題,使用TransferManager的速度更慢。問題是這是一個我無法控制的調用,它必須在每個文件中調用一次,然後等待該調用完成以開始下一個調用,因此TransferManager的大部分實用性都會丟失。無論如何。 –