java - Amazon S3 存储桶子对象 REST 和 Java(无需 SDK)

标签 java rest amazon-s3

我想要获取 Amazon S3 存储桶中的对象和文件夹的列表,但我不能,我不应该使用 Amazon S3 SDK。

重要的是,如果我发送请求然后收到响应,则不要使用 SDK,而只能使用 Rest 和 Java。 我有一个这样的方法:

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

最佳答案

在不知道您的问题是什么的情况下,我只能给您 AWS S3 Rest API 的链接.

This method做你想做的事。

希望这对您有帮助。

否则,请向我们提供有关您的问题的更多信息。

关于java - Amazon S3 存储桶子对象 REST 和 Java(无需 SDK),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16540835/

相关文章:

java - 干净的代码 - 应该在哪里应用 @Autowired?

java - JSON - 让 Jackson 在序列化时使用 JsonProperty

javascript - 如何使用云存储分段上传音乐/视频(适用于 Laravel)?

amazon-s3 - 尝试使用以下配置创建 S3 存储桶时,继续遇到此错误 : Template validation error

apache-spark - Parquet with Athena VS Redshift

java - 如何避免一个组件另一个组件出现 "put on top of"- Java Swing

java - 在 Java 中设置值失败

java - 以下哪一段代码在 Java 中速度更快

c# - 使用 REST 服务的跨平台文件上传/下载指南

api - REST API 是否应该反射(reflect)服务器端应用程序架构