Java aws为SSE-C特定加密预先指定的URL

Java aws为SSE-C特定加密预先指定的URL,java,amazon-web-services,amazon-s3,apache-httpclient-4.x,Java,Amazon Web Services,Amazon S3,Apache Httpclient 4.x,我正在尝试使用客户提供的密钥将文件上载到AmazonS3。我在这里学习了本教程: 我的代码: AWSCredentials credentials = new BasicAWSCredentials("myKey", "mySecretKey"); AmazonS3 s3 = new AmazonS3Client(); try (CloseableHttpClient httpClient = HttpClientFactory.createUploadClient()) { Gen

我正在尝试使用客户提供的密钥将文件上载到AmazonS3。我在这里学习了本教程:

我的代码:

AWSCredentials credentials = new BasicAWSCredentials("myKey", "mySecretKey");
AmazonS3 s3 = new AmazonS3Client();

try (CloseableHttpClient httpClient = HttpClientFactory.createUploadClient()) {
    GeneratePresignedUrlRequest genreq = new GeneratePresignedUrlRequest("bucketName", "test7.pdf", HttpMethod.PUT);
    SecretKey secretKey = generateSecretKey();
    SSECustomerKey sseKey = new SSECustomerKey(secretKey);
    genreq.setSSECustomerKey(sseKey);

    URL puturl = s3.generatePresignedUrl(genreq);

    HttpPut putreq = new HttpPut(URI.create(puturl.toExternalForm()));

    putreq.addHeader(Headers.SERVER_SIDE_ENCRYPTION_CUSTOMER_KEY, sseKey.getKey());
    putreq.addHeader(Headers.SERVER_SIDE_ENCRYPTION_CUSTOMER_ALGORITHM, SSEAlgorithm.AES256.getAlgorithm());
    putreq.addHeader(Headers.SERVER_SIDE_ENCRYPTION_CUSTOMER_KEY_MD5, sseKey.getMd5());
    putreq.setEntity(new FileEntity(new File("filePath")));
    HttpResponse resp = httpClient.execute(putreq);
    Assert.assertTrue(resp.getStatusLine().getStatusCode() == 200);
} catch (IOException e) {
    Assert.fail();
}
URL被生成,然后当我尝试使用时,我提供了所需的头,但问题是响应是403禁止的


我缺少什么?

为了使使用预签名URL的SSE客户端特定加密上载工作,您需要启用SigV4(默认情况下,为我启用了SigV2)。有几种方法可以启用此功能-系统属性、bucket策略等。但对我来说,这是可行的:

AWSCredentials凭证=新的基本凭证(“accessKey”、“secretKey”);

s3=新的AmazonS3Client(凭据,新的客户端配置(),带有SignerOverride(“AWSS3V4SignerType”)

403禁止的
通常伴随着一个响应正文,其中用XML解释了错误。。。你能抓住它并把它编辑成问题吗?这就是问题所在,没有进一步的解释,只是被禁止。看一看:
HttpResponseProxy{HTTP/1.1403禁止[x-amz-request-id:theId,x-amz-id-2:the2ndId,内容类型:application/xml,传输编码:chunked,日期:Tue,2016年4月26日13:22:27 GMT,连接:close,服务器:AmazonS3]ResponseEntityProxy{[Content-Type:application/xml,chunked:true]}
无论您使用什么,都会隐藏响应主体。这些只是标题。