S3 的 现在支持此功能。请参阅此处。GetObject
// Create an S3Presigner using the default region and credentials.
// This is usually done at application startup, because creating a presigner can be expensive.
S3Presigner presigner = S3Presigner.create();
// Create a GetObjectRequest to be pre-signed
GetObjectRequest getObjectRequest =
GetObjectRequest.builder()
.bucket("my-bucket")
.key("my-key")
.build();
// Create a GetObjectPresignRequest to specify the signature duration
GetObjectPresignRequest getObjectPresignRequest =
GetObjectPresignRequest.builder()
.signatureDuration(Duration.ofMinutes(10))
.getObjectRequest(getObjectRequest)
.build();
// Generate the presigned request
PresignedGetObjectRequest presignedGetObjectRequest =
presigner.presignGetObject(getObjectPresignRequest);
// Log the presigned URL, for example.
System.out.println("Presigned URL: " + presignedGetObjectRequest.url());
// It is recommended to close the S3Presigner when it is done being used, because some credential
// providers (e.g. if your AWS profile is configured to assume an STS role) require system resources
// that need to be freed. If you are using one S3Presigner per application (as recommended), this
// usually is not needed.
presigner.close();
S3 的 现在也支持此功能。示例如下。PutObject
S3Presigner presigner = S3Presigner.create();
PresignedPutObjectRequest presignedRequest =
presigner.presignPutObject(r -> r.signatureDuration(Duration.ofMinutes(5))
.putObjectRequest(por -> por.bucket(testBucket).key(objectKey)));
System.out.println("Pre-signed URL to upload a file to: " +
presignedRequest.url());
System.out.println("Which HTTP method needs to be used when uploading a file: " +
presignedRequest.httpRequest().method());
System.out.println("Which headers need to be sent with the upload: " +
presignedRequest.signedHeaders())
下面是使用预签名项目对象请求上传到 S3 的示例:
PresignedPutObjectRequest presignedRequest = ...;
SdkHttpClient httpClient = ApacheHttpClient.builder().build();
ContentStreamProvider contentToUpload = () -> new StringInputStream("Hello, World!");
HttpExecuteRequest uploadRequest = HttpExecuteRequest.builder()
.request(presignedRequest.httpRequest())
.contentStreamProvider(contentToUpload)
.build();
HttpExecuteResponse response = httpClient.prepareRequest(uploadRequest).call();
Validate.isTrue(response.httpResponse().isSuccessful());