Create a presigned URL for Amazon S3 directory buckets to get an object using an AWS SDK - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

Create a presigned URL for Amazon S3 directory buckets to get an object using an AWS SDK

The following code example shows how to create a presigned URL for S3 directory buckets and get an object.

Java
SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

Generate a presigned GET URL for accessing an object in an S3 directory bucket.

import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.GetObjectRequest; import software.amazon.awssdk.services.s3.model.S3Exception; import software.amazon.awssdk.services.s3.presigner.S3Presigner; import software.amazon.awssdk.services.s3.presigner.model.GetObjectPresignRequest; import software.amazon.awssdk.services.s3.presigner.model.PresignedGetObjectRequest; import java.nio.file.Path; import java.time.Duration; import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.createS3Client; import static com.example.s3.util.S3DirectoryBucketUtils.createS3Presigner; import static com.example.s3.util.S3DirectoryBucketUtils.deleteAllObjectsInDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.deleteDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.getFilePath; import static com.example.s3.util.S3DirectoryBucketUtils.putDirectoryBucketObject; /** * Generates a presigned URL for accessing an object in the specified S3 * directory bucket. * * @param s3Presigner The S3 presigner client used to generate the presigned URL * @param bucketName The name of the directory bucket * @param objectKey The key (name) of the object to access * @return A presigned URL for accessing the specified object */ public static String generatePresignedGetURLForDirectoryBucket(S3Presigner s3Presigner, String bucketName, String objectKey) { logger.info("Generating presigned URL for object: {} in bucket: {}", objectKey, bucketName); try { // Create a GetObjectRequest GetObjectRequest getObjectRequest = GetObjectRequest.builder() .bucket(bucketName) .key(objectKey) .build(); // Create a GetObjectPresignRequest GetObjectPresignRequest getObjectPresignRequest = GetObjectPresignRequest.builder() .signatureDuration(Duration.ofMinutes(10)) // Presigned URL valid for 10 minutes .getObjectRequest(getObjectRequest) .build(); // Generate the presigned URL PresignedGetObjectRequest presignedGetObjectRequest = s3Presigner.presignGetObject(getObjectPresignRequest); // Get the presigned URL String presignedURL = presignedGetObjectRequest.url().toString(); logger.info("Presigned URL: {}", presignedURL); return presignedURL; } catch (S3Exception e) { logger.error("Failed to generate presigned URL: {} - Error code: {}", e.awsErrorDetails().errorMessage(), e.awsErrorDetails().errorCode(), e); throw e; } }
  • For API details, see GetObject in AWS SDK for Java 2.x API Reference.