2013-05-14 2 views
1

Amazon S3에서 Bucket에있는 개체 및 폴더 목록을 가져오고 싶지만 Amazon S3 SDK를 사용하지 않아야합니다.Amazon S3 버킷 하위 개체 SDK가없는 REST 및 Java

Rest와 Java에서만 SDK를 사용하지 않는 것이 중요하며 요청을 보내고 응답을 받아야합니다. 나는이 같은 방법이 있습니다

public String BucketSubList(String strPath) throws Exception { 

    String answer = null; 

    // S3 timestamp pattern. 
    String fmt = "EEE, dd MMM yyyy HH:mm:ss "; 
    SimpleDateFormat df = new SimpleDateFormat(fmt, Locale.US); 
    df.setTimeZone(TimeZone.getTimeZone("GMT")); 

    // Data needed for signature 
    String method = "GET"; 
    String contentMD5 = ""; 
    String contentType = ""; 
    String date = df.format(new Date()) + "GMT"; 
    String bucket = "/" + strPath + "/"; 

    // Generate signature 
    StringBuffer buf = new StringBuffer(); 
    buf.append(method).append("\n"); 
    buf.append(contentMD5).append("\n"); 
    buf.append(contentType).append("\n"); 
    buf.append(date).append("\n"); 
    buf.append(bucket); 
    // try { 
    String signature = sign(buf.toString()); 

    // Connection to s3.amazonaws.com 
    URL url = new URL("http", "s3.amazonaws.com", 80, bucket); 

    HttpURLConnection httpConn = null; 
    httpConn = (HttpURLConnection) url.openConnection(); 
    httpConn.setDoInput(true); 
    httpConn.setDoOutput(true); 
    httpConn.setUseCaches(false); 
    httpConn.setDefaultUseCaches(false); 
    httpConn.setAllowUserInteraction(true); 
    httpConn.setRequestMethod(method); 
    httpConn.setRequestProperty("Date", date); 
    // httpConn.setRequestProperty("Content-Type", "text/plain"); 

    String AWSAuth = "AWS " + keyId + ":" + signature; 
    httpConn.setRequestProperty("Authorization", AWSAuth); 

    // Send the HTTP PUT request. 
    int statusCode = httpConn.getResponseCode(); 
    System.out.println(statusCode); 
    if ((statusCode/100) != 2) { 
     // Deal with S3 error stream. 
     InputStream in = httpConn.getErrorStream(); 
     String errorStr = getS3ErrorCode(in); 
     System.out.println("Error: " + errorStr); 
    } else { 
     answer = ""; 
     // System.out.println("Bucket listed successfully"); 
     InputStream inst = httpConn.getInputStream(); 
     BufferedReader in = new BufferedReader(new InputStreamReader(inst)); 
     String decodedString; 
     while ((decodedString = in.readLine()) != null) { 
      answer += decodedString; 
      System.out.println(answer); 
     } 
     in.close(); 
    } 

    return answer; 
} 
+1

무엇이 문제입니까? –

+0

이것은 숙제와 비슷합니다. –

답변

1

문제가 무엇인지 모른 채, 난 그냥 당신에게 AWS S3 Rest API에 대한 링크를 제공 할 수 있습니다.

This method 원하는 것을 수행합니다.

도움이되기를 바랍니다.

그렇지 않으면 문제에 대한 추가 정보를 제공해주세요.