我的例子:
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>
这是我生成的网址:
有人可以有任何想法或我以前正确运行URL的方式吗?
答案 0 :(得分:0)
以防万一有人在寻找解决方案。
如果看到 SignatureDoesNotMatch
-首先要检查的是上载文件时要发送的“ Content-Type”标头。 必须具有与生成URL相同的值。