Use CreateMultipartUpload with an AWS SDK - Amazon Simple Storage Service

Use CreateMultipartUpload with an AWS SDK

The following code example shows how to use CreateMultipartUpload.

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.

Create a multipart upload in a directory bucket.

import com.example.s3.util.S3DirectoryBucketUtils; 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.CreateMultipartUploadRequest; import software.amazon.awssdk.services.s3.model.CreateMultipartUploadResponse; import software.amazon.awssdk.services.s3.model.S3Exception; import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.createS3Client; import static com.example.s3.util.S3DirectoryBucketUtils.deleteDirectoryBucket; /** * This method creates a multipart upload request that generates a unique upload * ID used to track * all the upload parts. * * @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 uploaded * @return The upload ID used to track the multipart upload */ public static String createDirectoryBucketMultipartUpload(S3Client s3Client, String bucketName, String objectKey) { logger.info("Creating multipart upload for object: {} in bucket: {}", objectKey, bucketName); try { // Create a CreateMultipartUploadRequest CreateMultipartUploadRequest createMultipartUploadRequest = CreateMultipartUploadRequest.builder() .bucket(bucketName) .key(objectKey) .build(); // Initiate the multipart upload CreateMultipartUploadResponse response = s3Client.createMultipartUpload(createMultipartUploadRequest); String uploadId = response.uploadId(); logger.info("Multipart upload initiated. Upload ID: {}", uploadId); return uploadId; } catch (S3Exception e) { logger.error("Failed to create multipart upload: {} - Error code: {}", e.awsErrorDetails().errorMessage(), e.awsErrorDetails().errorCode(), e); throw e; } }

For a complete list of AWS SDK developer guides and code examples, see Developing with Amazon S3 using the AWS SDKs. This topic also includes information about getting started and details about previous SDK versions.