There are more AWS SDK examples available in the AWS Doc SDK Examples
Use PutObject
with an AWS SDK
The following code example shows how to use PutObject
.
Action examples are code excerpts from larger programs and must be run in context. You can see this action in context in the following code example:
- 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
. Put an object into a directory bucket.
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.awssdk.awscore.exception.AwsErrorDetails; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.PutObjectRequest; import software.amazon.awssdk.services.s3.model.S3Exception; import java.io.UncheckedIOException; import java.nio.file.Path; import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.createS3Client; import static com.example.s3.util.S3DirectoryBucketUtils.deleteAllObjectsInDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.deleteDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.getFilePath; /** * Puts an object into the specified S3 directory bucket. * * @param s3Client The S3 client used to interact with S3 * @param bucketName The name of the directory bucket * @param objectKey The key (name) of the object to be placed in the bucket * @param filePath The path of the file to be uploaded */ public static void putDirectoryBucketObject(S3Client s3Client, String bucketName, String objectKey, Path filePath) { logger.info("Putting object: {} into bucket: {}", objectKey, bucketName); try { // Create a PutObjectRequest PutObjectRequest putObj = PutObjectRequest.builder() .bucket(bucketName) .key(objectKey) .build(); // Upload the object s3Client.putObject(putObj, filePath); logger.info("Successfully placed {} into bucket {}", objectKey, bucketName); } catch (UncheckedIOException e) { throw S3Exception.builder().message("Failed to read the file: " + e.getMessage()).cause(e) .awsErrorDetails(AwsErrorDetails.builder() .errorCode("ClientSideException:FailedToReadFile") .errorMessage(e.getMessage()) .build()) .build(); } catch (S3Exception e) { logger.error("Failed to put object: {}", e.getMessage(), e); throw e; } }
-
For API details, see PutObject in AWS SDK for Java 2.x API Reference.
-
PutBucketPolicy
UploadPart