Pilih preferensi cookie Anda

Kami menggunakan cookie penting serta alat serupa yang diperlukan untuk menyediakan situs dan layanan. Kami menggunakan cookie performa untuk mengumpulkan statistik anonim sehingga kami dapat memahami cara pelanggan menggunakan situs dan melakukan perbaikan. Cookie penting tidak dapat dinonaktifkan, tetapi Anda dapat mengklik “Kustom” atau “Tolak” untuk menolak cookie performa.

Jika Anda setuju, AWS dan pihak ketiga yang disetujui juga akan menggunakan cookie untuk menyediakan fitur situs yang berguna, mengingat preferensi Anda, dan menampilkan konten yang relevan, termasuk iklan yang relevan. Untuk menerima atau menolak semua cookie yang tidak penting, klik “Terima” atau “Tolak”. Untuk membuat pilihan yang lebih detail, klik “Kustomisasi”.

Gunakan UploadPartCopy dengan AWS SDK - AWS Contoh Kode SDK

Ada lebih banyak contoh AWS SDK yang tersedia di repo Contoh SDK AWS Doc. GitHub

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Ada lebih banyak contoh AWS SDK yang tersedia di repo Contoh SDK AWS Doc. GitHub

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Gunakan UploadPartCopy dengan AWS SDK

Contoh kode berikut menunjukkan cara menggunakanUploadPartCopy.

Java
SDK untuk Java 2.x
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

Buat salinan bagian berdasarkan ukuran objek sumber dan salin bagian individual ke ember direktori.

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.CompletedPart; import software.amazon.awssdk.services.s3.model.HeadObjectRequest; import software.amazon.awssdk.services.s3.model.HeadObjectResponse; import software.amazon.awssdk.services.s3.model.S3Exception; import software.amazon.awssdk.services.s3.model.UploadPartCopyRequest; import software.amazon.awssdk.services.s3.model.UploadPartCopyResponse; import java.io.IOException; import java.nio.file.Path; import java.util.ArrayList; import java.util.List; import static com.example.s3.util.S3DirectoryBucketUtils.abortDirectoryBucketMultipartUploads; import static com.example.s3.util.S3DirectoryBucketUtils.completeDirectoryBucketMultipartUpload; import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucketMultipartUpload; 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; import static com.example.s3.util.S3DirectoryBucketUtils.multipartUploadForDirectoryBucket; /** * Creates copy parts based on source object size and copies over individual * parts. * * @param s3Client The S3 client used to interact with S3 * @param sourceBucket The name of the source bucket * @param sourceKey The key (name) of the source object * @param destinationBucket The name of the destination bucket * @param destinationKey The key (name) of the destination object * @param uploadId The upload ID used to track the multipart upload * @return A list of completed parts */ public static List<CompletedPart> multipartUploadCopyForDirectoryBucket(S3Client s3Client, String sourceBucket, String sourceKey, String destinationBucket, String destinationKey, String uploadId) { // Get the object size to track the end of the copy operation HeadObjectRequest headObjectRequest = HeadObjectRequest.builder() .bucket(sourceBucket) .key(sourceKey) .build(); HeadObjectResponse headObjectResponse = s3Client.headObject(headObjectRequest); long objectSize = headObjectResponse.contentLength(); logger.info("Source Object size: {}", objectSize); // Copy the object using 20 MB parts long partSize = 20 * 1024 * 1024; // 20 MB long bytePosition = 0; int partNum = 1; List<CompletedPart> uploadedParts = new ArrayList<>(); while (bytePosition < objectSize) { long lastByte = Math.min(bytePosition + partSize - 1, objectSize - 1); logger.info("Part Number: {}, Byte Position: {}, Last Byte: {}", partNum, bytePosition, lastByte); try { UploadPartCopyRequest uploadPartCopyRequest = UploadPartCopyRequest.builder() .sourceBucket(sourceBucket) .sourceKey(sourceKey) .destinationBucket(destinationBucket) .destinationKey(destinationKey) .uploadId(uploadId) .copySourceRange("bytes=" + bytePosition + "-" + lastByte) .partNumber(partNum) .build(); UploadPartCopyResponse uploadPartCopyResponse = s3Client.uploadPartCopy(uploadPartCopyRequest); CompletedPart part = CompletedPart.builder() .partNumber(partNum) .eTag(uploadPartCopyResponse.copyPartResult().eTag()) .build(); uploadedParts.add(part); bytePosition += partSize; partNum++; } catch (S3Exception e) { logger.error("Failed to copy part number {}: {} - Error code: {}", partNum, e.awsErrorDetails().errorMessage(), e.awsErrorDetails().errorCode()); throw e; } } return uploadedParts; }
  • Untuk detail API, lihat UploadPartCopydi Referensi AWS SDK for Java 2.x API.

SDK untuk Java 2.x
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

Buat salinan bagian berdasarkan ukuran objek sumber dan salin bagian individual ke ember direktori.

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.CompletedPart; import software.amazon.awssdk.services.s3.model.HeadObjectRequest; import software.amazon.awssdk.services.s3.model.HeadObjectResponse; import software.amazon.awssdk.services.s3.model.S3Exception; import software.amazon.awssdk.services.s3.model.UploadPartCopyRequest; import software.amazon.awssdk.services.s3.model.UploadPartCopyResponse; import java.io.IOException; import java.nio.file.Path; import java.util.ArrayList; import java.util.List; import static com.example.s3.util.S3DirectoryBucketUtils.abortDirectoryBucketMultipartUploads; import static com.example.s3.util.S3DirectoryBucketUtils.completeDirectoryBucketMultipartUpload; import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucketMultipartUpload; 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; import static com.example.s3.util.S3DirectoryBucketUtils.multipartUploadForDirectoryBucket; /** * Creates copy parts based on source object size and copies over individual * parts. * * @param s3Client The S3 client used to interact with S3 * @param sourceBucket The name of the source bucket * @param sourceKey The key (name) of the source object * @param destinationBucket The name of the destination bucket * @param destinationKey The key (name) of the destination object * @param uploadId The upload ID used to track the multipart upload * @return A list of completed parts */ public static List<CompletedPart> multipartUploadCopyForDirectoryBucket(S3Client s3Client, String sourceBucket, String sourceKey, String destinationBucket, String destinationKey, String uploadId) { // Get the object size to track the end of the copy operation HeadObjectRequest headObjectRequest = HeadObjectRequest.builder() .bucket(sourceBucket) .key(sourceKey) .build(); HeadObjectResponse headObjectResponse = s3Client.headObject(headObjectRequest); long objectSize = headObjectResponse.contentLength(); logger.info("Source Object size: {}", objectSize); // Copy the object using 20 MB parts long partSize = 20 * 1024 * 1024; // 20 MB long bytePosition = 0; int partNum = 1; List<CompletedPart> uploadedParts = new ArrayList<>(); while (bytePosition < objectSize) { long lastByte = Math.min(bytePosition + partSize - 1, objectSize - 1); logger.info("Part Number: {}, Byte Position: {}, Last Byte: {}", partNum, bytePosition, lastByte); try { UploadPartCopyRequest uploadPartCopyRequest = UploadPartCopyRequest.builder() .sourceBucket(sourceBucket) .sourceKey(sourceKey) .destinationBucket(destinationBucket) .destinationKey(destinationKey) .uploadId(uploadId) .copySourceRange("bytes=" + bytePosition + "-" + lastByte) .partNumber(partNum) .build(); UploadPartCopyResponse uploadPartCopyResponse = s3Client.uploadPartCopy(uploadPartCopyRequest); CompletedPart part = CompletedPart.builder() .partNumber(partNum) .eTag(uploadPartCopyResponse.copyPartResult().eTag()) .build(); uploadedParts.add(part); bytePosition += partSize; partNum++; } catch (S3Exception e) { logger.error("Failed to copy part number {}: {} - Error code: {}", partNum, e.awsErrorDetails().errorMessage(), e.awsErrorDetails().errorCode()); throw e; } } return uploadedParts; }
  • Untuk detail API, lihat UploadPartCopydi Referensi AWS SDK for Java 2.x API.

Topik berikutnya:

Skenario

Topik sebelumnya:

UploadPart
PrivasiSyarat situsPreferensi cookie
© 2025, Amazon Web Services, Inc. atau afiliasinya. Semua hak dilindungi undang-undang.