com.amazonaws.AmazonClientException:无法执行HTTP请求:peer未经过身份验证

时间:2015-09-28 12:00:54

标签: java amazon-web-services amazon-s3 amazon

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

这是我的主要方法:

rowObject.IsActive !== undefined ? rowObject.IsActive : rowObject[2]

这是我的S3Upload.java

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

跑完之后,我得到了这个例外:

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

1 个答案:

答案 0 :(得分:0)

您必须确定

  • 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对象上传图像。