Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用REST从s3存储桶下载文件_Java_Rest_Amazon S3 - Fatal编程技术网

Java 使用REST从s3存储桶下载文件

Java 使用REST从s3存储桶下载文件,java,rest,amazon-s3,Java,Rest,Amazon S3,我正试图通过REST调用从s3存储桶下载。我可以创建bucket、删除bucket和列出bucket。下载总是以找不到对象或HTTP响应代码403结束。代码如下。还有人知道amazon was sdk是否可以用于非amazon站点吗 public void getObject() throws Exception { String fmt = "EEE, dd MMM yyyy HH:mm:ss "; SimpleDateFormat df = new SimpleDateFormat(

我正试图通过REST调用从s3存储桶下载。我可以创建bucket、删除bucket和列出bucket。下载总是以找不到对象或HTTP响应代码403结束。代码如下。还有人知道amazon was sdk是否可以用于非amazon站点吗

public void getObject() throws Exception
{
String fmt = "EEE, dd MMM yyyy HH:mm:ss ";
    SimpleDateFormat df = new SimpleDateFormat(fmt, Locale.US);
    df.setTimeZone(TimeZone.getTimeZone("GMT"));
    String ob2 = "/bucket/test.txt";
    String bucket = "/bucket";
    String method = "GET"; 
    String contentMD5 = "";
    String contentType = "";
    String date = df.format(new Date()) + "GMT";        

    // 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(ob2);   
    String signature = sign(buf.toString());

    HttpURLConnection httpConn = null;
    URL url = new URL("https”,”s3demo.s3demosite.com",443,bucket);
    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-Length", "0");
    String AWSAuth = "AWS " + keyId + ":" + signature;
    httpConn.setRequestProperty("Authorization", AWSAuth);

    // Send the HTTP PUT request.
    int statusCode = httpConn.getResponseCode();
    InputStream err = httpConn.getErrorStream();
    InputStream is = null;
    is = httpConn.getInputStream();
    int ch;
    StringBuffer sb = new StringBuffer();
    while ((ch = err.read()) != -1) {
      sb.append((char) ch);
    }
    if ((statusCode/100) != 2)
    {
        // Deal with S3 error stream.
        InputStream in = httpConn.getErrorStream();
        System.out.println("Error: "+errorStr);
    }
    else
    {
        System.out.println("download worked”);
    }
}

您没有使用的原因是什么?我没有连接到Amazon服务器。也不确定它是否会起作用。您知道它是否与其他s3服务器兼容吗?这取决于您使用的s3兼容工具。您提供的信息无法确定它的服务器或客户端问题。