带有CannedAccessControlList.Private的generatePresignedUrlRequest时,S3预签名URL文件上传不起作用

时间:2017-03-25 08:30:41

标签: java amazon-s3

我的例子:

AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
    ClientConfiguration clientConfig = new ClientConfiguration();
    clientConfig.setProtocol(Protocol.HTTP);
    BasicAWSCredentials creds = new BasicAWSCredentials(accessKey, secretKey); 
    AmazonS3 s3client = new AmazonS3Client(credentials, clientConfig);
    Region region = Region.getRegion(Regions.EU_WEST_1);
    s3client.setRegion(region);
    s3client.setEndpoint(serverCeph);
    Bucket bucket = s3client.createBucket(bucketName);
    File file = new File(fileNameTest);
    AccessControlList acl = new AccessControlList();
    acl.grantPermission(GroupGrantee.AllUsers, Permission.FullControl);
       s3client.putObject(new PutObjectRequest(bucketName, keyObject,   file).withAccessControlList(acl));
    s3client.setObjectAcl(bucket.getName(), keyObject,CannedAccessControlList.Private);


           // Generate URL
    System.out.println("Generating pre-signed URL.");
    java.util.Date expiration = new java.util.Date();
    long milliSeconds = expiration.getTime();
    milliSeconds += 1000 * 60 * 60; // Add 1 hour.
    expiration.setTime(milliSeconds);
    GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, keyObject);
    generatePresignedUrlRequest.setMethod(HttpMethod.GET);
    generatePresignedUrlRequest.setExpiration(expiration);
    generatePresignedUrlRequest.setContentType("image/png");
    URL url = s3client.generatePresignedUrl(generatePresignedUrlRequest);
    System.out.println("Pre-Signed URL = " + url.toString());

如果我使用s3client.setObjectAcl(bucket.getName(), keyObject,CannedAccessControlList.PublicRead);,那么我可以获取网址,这项工作正常 但是,如果我使用s3client.setObjectAcl(bucket.getName(), keyObject,CannedAccessControlList.Private);,那么我可以获取URL并且URL无法正常工作

当我将URl传递给浏览器时,我总是在下面收到错误。

<Error>
<Code>SignatureDoesNotMatch</Code>
<RequestId>tx000000000000000068415-0058d62341-5229e3-default</RequestId>
<HostId>5229e3-default-default</HostId>
</Error>

这是我生成的网址:

http://server:port/bucketname/keyobject?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20170325T081827Z&X-Amz-SignedHeaders=content-type%3Bhost&X-Amz-Expires=3599&X-Amz-Credential=HGPX8TW9RGI5LEFLARTX%2F20170325%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Signature=76cb890603b2d9a64fd9c5c670584c73f457c08fc0a31f67c7b2a6c92265a427

有人可以有任何想法或我以前正确运行URL的方式吗?

1 个答案:

答案 0 :(得分:0)

以防万一有人在寻找解决方案。

如果看到 SignatureDoesNotMatch -首先要检查的是上载文件时要发送的“ Content-Type”标头。 必须具有与生成URL相同的值。