我在amazon aws上创建了一个新账户来使用s3 bucket,我写了这段代码来上传一个图片文件到s3 bucket,但是它给了我这个例外:〉无法执行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条答案
按热度按时间yeotifhr1#
你必须确定
用于身份验证目的。并使用:
现在你可以使用
s3
对象上传图像。