com.amazonaws.AmazonClientException:无法执行 HTTP 请求:对等方未通过身份验证

Posted

技术标签:

【中文标题】com.amazonaws.AmazonClientException:无法执行 HTTP 请求:对等方未通过身份验证【英文标题】:com.amazonaws.AmazonClientException: Unable to execute HTTP request: peer not authenticated 【发布时间】:2015-12-25 15:57:10 【问题描述】:

我在 amazon aws 上创建了一个新帐户以使用 s3 存储桶,并且我编写了此代码以将一个图像文件上传到 s3 存储桶,但它给了我这个异常:>无法执行 HTTP 请求:对等体未通过身份验证。

这是我的主要方法:

public class Main2 

    public static void main(String[] args) throws IOException 
        File file=new File("C:\\Users\\KDSss\\OneDrive\\Pictures\\my photo 001.jpg");
        try 
            InputStream targetStream = new FileInputStream(file);
            S3Upload.uploadImage(targetStream, "Discussion", "jpg");
         catch (FileNotFoundException e) 
            e.printStackTrace();
        
    

这是我的 S3Upload.java

public class S3Upload 


     public static void uploadImage(InputStream path, String imageName, String imageType) throws IOException
     
          String existingBucketName = "housejoy/hjcustomerimage/";
          String keyName = imageName+".png";
          AmazonS3 s3Client = new AmazonS3Client(new PropertiesCredentials(UploadFile.class.getResourceAsStream("AwsCredentials.properties")));
          ObjectMetadata objectMetadata = new ObjectMetadata();
          objectMetadata.setContentType(imageType);
          PutObjectRequest putObjectRequest = new PutObjectRequest(existingBucketName, keyName, path, objectMetadata);
          putObjectRequest.setCannedAcl(CannedAccessControlList.PublicRead);
          PutObjectResult result = s3Client.putObject(putObjectRequest);
          System.out.println("Etag:" + result.getETag() + "-->" + result);
         

运行后,我得到了这个异常:

com.amazonaws.AmazonClientException: Unable to execute HTTP request: peer not authenticated
    at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:328)
    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:165)
    at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:2906)
    at com.amazonaws.services.s3.AmazonS3Client.putObject(AmazonS3Client.java:1123)
    at com.housejoy.ce.jaxb.S3Upload.uploadImage(S3Upload.java:32)......
Caused by: javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated
    at sun.security.ssl.SSLSessionImpl.getPeerCertificates(SSLSessionImpl.java:421)
    at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:128)
    at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:572)
    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:180)
    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:151)........com.amazonaws.AmazonClientException: Unable to execute HTTP request: peer not authenticated
    at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:328)
    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:165)
    at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:2906)
    at com.amazonaws.services.s3.AmazonS3Client.putObject(AmazonS3Client.java:1123)
    at com.housejoy.ce.jaxb.S3Upload.uploadImage(S3Upload.java:32)
    at com.housejoy.ce.service.UploadImageService.uploadImage(UploadImageService.java:171.................Caused by: javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated
    at sun.security.ssl.SSLSessionImpl.getPeerCertificates(SSLSessionImpl.java:421)
    at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:128)
    at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:572)
    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:180)

【问题讨论】:

您是在使用任何类型的公司网络/网络代理还是只是标准的互联网连接? 【参考方案1】:

你必须确定

SECRET_KEY ACCESS_KEY END_POINT_URL

用于身份验证。 并使用:

    AmazonS3 s3;
    AWSCredentials credentials = new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY);
    s3 = new AmazonS3Client(credentials);
    s3.setEndpoint(END_POINT_URL);

现在您可以使用s3 对象上传图片了。

【讨论】:

以上是关于com.amazonaws.AmazonClientException:无法执行 HTTP 请求:对等方未通过身份验证的主要内容,如果未能解决你的问题,请参考以下文章