简体   繁体   中英

Amazon S3 bucket sub objects REST and Java without SDK

I want to get a list of objects and folders that are in a Bucket in Amazon S3 but I can't, I should not use Amazon S3 SDK.

It's important not to use SDK and only with Rest and Java should I sent a Request and then receive a Response. I have a method like this :

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;
}

Without knowing what your problem is, i just can give you the link to the AWS S3 Rest API .

This method does what you want.

I hope this might help you.

Otherwise please give us some more information about your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM