$npx -y skills add giuseppe-trisciuoglio/developer-kit --skill aws-sdk-java-v2-s3Provides Amazon S3 patterns and examples using AWS SDK for Java 2.x. Use when working with S3 buckets, uploading/downloading objects, multipart uploads, presigned URLs, S3 Transfer Manager, object operations, or S3-specific configurations.
| 1 | # AWS SDK for Java 2.x - Amazon S3 |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | Provides patterns for S3 operations: bucket management, object upload/download with multipart support, presigned URLs, S3 Transfer Manager, and S3-specific configurations using AWS SDK for Java 2.x. |
| 6 | |
| 7 | ## When to Use |
| 8 | |
| 9 | - Creating, listing, or deleting S3 buckets with proper configuration |
| 10 | - Uploading or downloading objects from S3 with metadata and encryption |
| 11 | - Working with multipart uploads for large files (>100MB) with error handling |
| 12 | - Generating presigned URLs for temporary access to S3 objects |
| 13 | - Copying or moving objects between S3 buckets with metadata preservation |
| 14 | - Setting object metadata, storage classes, and access controls |
| 15 | - Implementing S3 Transfer Manager for optimized file transfers |
| 16 | - Integrating S3 with Spring Boot applications for cloud storage |
| 17 | |
| 18 | ## Quick Reference |
| 19 | |
| 20 | | Operation | Method | Notes | |
| 21 | |-----------|--------|-------| |
| 22 | | Create bucket | `createBucket()` | Wait with `waiter().waitUntilBucketExists()` | |
| 23 | | Upload object | `putObject()` | Use `RequestBody.fromFile()` | |
| 24 | | Download object | `getObject()` | Streams to file or memory | |
| 25 | | Delete objects | `deleteObjects()` | Batch up to 1000 keys | |
| 26 | | Presigned URL | `presigner.presignGetObject()` | Max 7 days expiration | |
| 27 | |
| 28 | ### Storage Classes |
| 29 | |
| 30 | | Class | Use Case | |
| 31 | |-------|----------| |
| 32 | | `STANDARD` | Frequently accessed data | |
| 33 | | `STANDARD_IA` | Infrequently accessed data | |
| 34 | | `GLACIER` | Long-term archive | |
| 35 | | `INTELLIGENT_TIERING` | Automatic cost optimization | |
| 36 | |
| 37 | ## Instructions |
| 38 | |
| 39 | ### 1. Add Dependencies |
| 40 | |
| 41 | ```xml |
| 42 | <dependency> |
| 43 | <groupId>software.amazon.awssdk</groupId> |
| 44 | <artifactId>s3</artifactId> |
| 45 | <version>2.20.0</version> |
| 46 | </dependency> |
| 47 | |
| 48 | <dependency> |
| 49 | <groupId>software.amazon.awssdk</groupId> |
| 50 | <artifactId>s3-transfer-manager</artifactId> |
| 51 | <version>2.20.0</version> |
| 52 | </dependency> |
| 53 | ``` |
| 54 | |
| 55 | ### 2. Create S3 Client |
| 56 | |
| 57 | ```java |
| 58 | S3Client s3Client = S3Client.builder() |
| 59 | .region(Region.US_EAST_1) |
| 60 | .build(); |
| 61 | |
| 62 | // With retry logic |
| 63 | S3Client s3Client = S3Client.builder() |
| 64 | .region(Region.US_EAST_1) |
| 65 | .overrideConfiguration(b -> b |
| 66 | .retryPolicy(RetryPolicy.builder() |
| 67 | .numRetries(3) |
| 68 | .build())) |
| 69 | .build(); |
| 70 | ``` |
| 71 | |
| 72 | ### 3. Create Bucket |
| 73 | |
| 74 | ```java |
| 75 | CreateBucketRequest request = CreateBucketRequest.builder() |
| 76 | .bucket(bucketName) |
| 77 | .build(); |
| 78 | |
| 79 | s3Client.createBucket(request); |
| 80 | |
| 81 | // Wait until ready |
| 82 | s3Client.waiter().waitUntilBucketExists( |
| 83 | HeadBucketRequest.builder().bucket(bucketName).build() |
| 84 | ); |
| 85 | ``` |
| 86 | |
| 87 | ### 4. Upload Object |
| 88 | |
| 89 | ```java |
| 90 | PutObjectRequest request = PutObjectRequest.builder() |
| 91 | .bucket(bucketName) |
| 92 | .key(key) |
| 93 | .contentType("application/pdf") |
| 94 | .serverSideEncryption(ServerSideEncryption.AES256) |
| 95 | .storageClass(StorageClass.STANDARD_IA) |
| 96 | .build(); |
| 97 | |
| 98 | s3Client.putObject(request, RequestBody.fromFile(Paths.get(filePath))); |
| 99 | |
| 100 | // Validate upload completion |
| 101 | HeadObjectResponse headResp = s3Client.headObject(HeadObjectRequest.builder() |
| 102 | .bucket(bucketName) |
| 103 | .key(key) |
| 104 | .build()); |
| 105 | ``` |
| 106 | |
| 107 | ### 5. Download Object |
| 108 | |
| 109 | ```java |
| 110 | GetObjectRequest request = GetObjectRequest.builder() |
| 111 | .bucket(bucketName) |
| 112 | .key(key) |
| 113 | .build(); |
| 114 | |
| 115 | s3Client.getObject(request, Paths.get(destPath)); |
| 116 | ``` |
| 117 | |
| 118 | ### 6. Generate Presigned URL |
| 119 | |
| 120 | ```java |
| 121 | try (S3Presigner presigner = S3Presigner.create()) { |
| 122 | GetObjectRequest getRequest = GetObjectRequest.builder() |
| 123 | .bucket(bucketName) |
| 124 | .key(key) |
| 125 | .build(); |
| 126 | |
| 127 | GetObjectPresignRequest presignRequest = GetObjectPresignRequest.builder() |
| 128 | .signatureDuration(Duration.ofMinutes(10)) |
| 129 | .getObjectRequest(getRequest) |
| 130 | .build(); |
| 131 | |
| 132 | String url = presigner.presignGetObject(presignRequest).url().toString(); |
| 133 | } |
| 134 | ``` |
| 135 | |
| 136 | ### 7. Use Transfer Manager (Large Files) |
| 137 | |
| 138 | ```java |
| 139 | try (S3TransferManager tm = S3TransferManager.create()) { |
| 140 | UploadFileRequest request = UploadFileRequest.builder() |
| 141 | .putObjectRequest(req -> req.bucket(bucketName).key(key)) |
| 142 | .source(Paths.get(filePath)) |
| 143 | .build(); |
| 144 | |
| 145 | FileUpload upload = tm.uploadFile(request); |
| 146 | CompletedFileUpload result = upload.completionFuture().join(); |
| 147 | } |
| 148 | ``` |
| 149 | |
| 150 | ## Best Practices |
| 151 | |
| 152 | ### Performance |
| 153 | - **Use S3 Transfer Manager**: Automatic multipart uploads for files >100MB |
| 154 | - **Reuse S3 Client**: Clients are thread-safe; reuse throughout application |
| 155 | - **Enable async operations**: Use `S3AsyncClient` for I/O-bound operations |
| 156 | - **Configure timeouts**: Set appropriate timeouts for large file operations |
| 157 | |
| 158 | ### Security |
| 159 | - **Use temporary credentials**: IAM roles or AWS STS for short-lived tokens |
| 160 | - **Enable encryption**: Use AES-256 or AWS KMS for sensitive data |
| 161 | - **Use presigned URLs**: Avoid exposing credentials with temporary access |
| 162 | - * |