请查看以下代码。
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package S3Test;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author Yohan
*/
public class S3Test extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter writer = response.getWriter();
System.out.println("I am in");
String accessKey = "**********";
String secretKey = "**********";
AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
AmazonS3 conn = new AmazonS3Client(credentials);
System.out.println("done");
// List<Bucket> buckets = conn.listBuckets();
//for (Bucket bucket : buckets) {
// System.out.println(bucket.getName() + "\t" +
// StringUtils.fromDate(bucket.getCreationDate()));
//}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
我正在使用此代码列出我在亚马逊s3中的存储桶。然而,这个过程非常缓慢,需要将近1分钟或更长时间。不仅如此,这需要大量的内存,几乎冻结谷歌Chrome。我的存储桶位于美国标准区域,我的应用程序在位于南亚的PC中运行。
延迟是连接配置。
我正在使用Netbeans IDE,上面的代码是servlet的一部分。我使用的服务器是apache tomcat 7.
为什么这需要花费大量时间?我还添加了Amazon SDK for Java附带的所有JAR文件。如果这需要很长时间,对我没有用。
特别注意
我注意到在非Web java应用程序中运行时没有任何时间延迟。我在一个简单的Main
类中运行它,它不是基于Web的,它工作得很好,速度很快。这里真的发生了什么?
答案 0 :(得分:0)
我注意到在非Web java应用程序中运行时没有任何时间延迟。我在一个简单的Main类中运行它,它不是基于Web的,它运行良好,快速。这里真的发生了什么?
所以你说你提出的代码运行得很快,但你的代码库中的其他代码还是很慢吗?可能问题在于你没有展示的其他代码。