我知道SDK版本1.x就像docs
一样简单GeneratePresignedUrlRequest
然而,查看2.0 docs,但我无法找到int main()
{
int c;
while ((c = getchar() != EOF)) {
if (c == ' ' || c == '\n' || c == '\t') {
printf("\n");
}
else {
putchar(c);
}
}
}
附近的任何内容。
希望还有另一个简单的模式吗?
答案 0 :(得分:3)
S3的GetObject
现在支持此功能。参见here。
// 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(request)
.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
现在也支持此功能。示例here。
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())
以下是使用PresignedPutObjectRequest上传到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());
答案 1 :(得分:1)
正如@Anurudhha在评论中提到的那样,SDK 2.0尚不支持它。这是官方积压的链接
https://github.com/aws/aws-sdk-java-v2/projects/1
这是feature request的一种解决方法,目前可以为您提供帮助
https://gist.github.com/aaronanderson/f9e2806cc5e2c18fab4d7e60c589d160