gpt4 book ai didi

java - AWS Java SDK 2.0 为 S3 对象创建预签名 URL

转载 作者:搜寻专家 更新时间:2023-10-30 21:13:06 27 4
gpt4 key购买 nike

我知道 1.x 版本的 SDK 就像 docs 一样简单

java.util.Date expiration = new java.util.Date();
long msec = expiration.getTime();
msec += 1000 * 60 * 60; // Add 1 hour.
expiration.setTime(msec);

GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, objectKey);
generatePresignedUrlRequest.setMethod(HttpMethod.PUT);
generatePresignedUrlRequest.setExpiration(expiration);

URL s = s3client.generatePresignedUrl(generatePresignedUrlRequest);

然而看着 2.0 docs但我找不到任何接近 GeneratePresignedUrlRequest 的内容。

希望有另一种简单的模式吗?

最佳答案

现在支持 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(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 也支持这一点。示例 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());

关于java - AWS Java SDK 2.0 为 S3 对象创建预签名 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48493657/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com