CreateMultipartUpload 搭配 使用 AWS SDK - AWS SDK 程式碼範例

文件範例儲存庫中有更多 AWS SDK可用的AWS SDK範例 GitHub 。

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

CreateMultipartUpload 搭配 使用 AWS SDK

下列程式碼範例示範如何使用 CreateMultipartUpload

Java
SDK 適用於 Java 2.x
注意

還有更多功能 GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

在目錄儲存貯體中建立分段上傳。

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; } }