Seleccione sus preferencias de cookies

Usamos cookies esenciales y herramientas similares que son necesarias para proporcionar nuestro sitio y nuestros servicios. Usamos cookies de rendimiento para recopilar estadísticas anónimas para que podamos entender cómo los clientes usan nuestro sitio y hacer mejoras. Las cookies esenciales no se pueden desactivar, pero puede hacer clic en “Personalizar” o “Rechazar” para rechazar las cookies de rendimiento.

Si está de acuerdo, AWS y los terceros aprobados también utilizarán cookies para proporcionar características útiles del sitio, recordar sus preferencias y mostrar contenido relevante, incluida publicidad relevante. Para aceptar o rechazar todas las cookies no esenciales, haga clic en “Aceptar” o “Rechazar”. Para elegir opciones más detalladas, haga clic en “Personalizar”.

Use UploadPart with an AWS SDK - Amazon Simple Storage Service
Esta página no se ha traducido a su idioma. Solicitar traducción

Use UploadPart with an AWS SDK

The following code example shows how to use UploadPart.

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.

Upload part of a multipart upload for a directory bucket.

import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.awssdk.core.sync.RequestBody; 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.S3Exception; import software.amazon.awssdk.services.s3.model.UploadPartRequest; import software.amazon.awssdk.services.s3.model.UploadPartResponse; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.ByteBuffer; 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.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; /** * This method creates part requests and uploads individual parts to S3. * While it uses the UploadPart API to upload a single part, it does so * sequentially to handle multiple parts of a file, returning all the completed * 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 * @param uploadId The upload ID used to track the multipart upload * @param filePath The path to the file to be uploaded * @return A list of uploaded parts * @throws IOException if an I/O error occurs */ public static List<CompletedPart> multipartUploadForDirectoryBucket(S3Client s3Client, String bucketName, String objectKey, String uploadId, Path filePath) throws IOException { logger.info("Uploading parts for object: {} in bucket: {}", objectKey, bucketName); int partNumber = 1; List<CompletedPart> uploadedParts = new ArrayList<>(); ByteBuffer bb = ByteBuffer.allocate(1024 * 1024 * 5); // 5 MB byte buffer // Read the local file, break down into chunks and process try (RandomAccessFile file = new RandomAccessFile(filePath.toFile(), "r")) { long fileSize = file.length(); int position = 0; // Sequentially upload parts of the file while (position < fileSize) { file.seek(position); int read = file.getChannel().read(bb); bb.flip(); // Swap position and limit before reading from the buffer UploadPartRequest uploadPartRequest = UploadPartRequest.builder() .bucket(bucketName) .key(objectKey) .uploadId(uploadId) .partNumber(partNumber) .build(); UploadPartResponse partResponse = s3Client.uploadPart( uploadPartRequest, RequestBody.fromByteBuffer(bb)); // Build the uploaded part CompletedPart uploadedPart = CompletedPart.builder() .partNumber(partNumber) .eTag(partResponse.eTag()) .build(); // Add the uploaded part to the list uploadedParts.add(uploadedPart); // Log to indicate the part upload is done logger.info("Uploaded part number: {} with ETag: {}", partNumber, partResponse.eTag()); bb.clear(); position += read; partNumber++; } } catch (S3Exception e) { logger.error("Failed to list parts: {} - Error code: {}", e.awsErrorDetails().errorMessage(), e.awsErrorDetails().errorCode()); throw e; } return uploadedParts; }
  • For API details, see UploadPart in AWS SDK for Java 2.x API Reference.

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.

Upload part of a multipart upload for a directory bucket.

import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.awssdk.core.sync.RequestBody; 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.S3Exception; import software.amazon.awssdk.services.s3.model.UploadPartRequest; import software.amazon.awssdk.services.s3.model.UploadPartResponse; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.ByteBuffer; 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.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; /** * This method creates part requests and uploads individual parts to S3. * While it uses the UploadPart API to upload a single part, it does so * sequentially to handle multiple parts of a file, returning all the completed * 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 * @param uploadId The upload ID used to track the multipart upload * @param filePath The path to the file to be uploaded * @return A list of uploaded parts * @throws IOException if an I/O error occurs */ public static List<CompletedPart> multipartUploadForDirectoryBucket(S3Client s3Client, String bucketName, String objectKey, String uploadId, Path filePath) throws IOException { logger.info("Uploading parts for object: {} in bucket: {}", objectKey, bucketName); int partNumber = 1; List<CompletedPart> uploadedParts = new ArrayList<>(); ByteBuffer bb = ByteBuffer.allocate(1024 * 1024 * 5); // 5 MB byte buffer // Read the local file, break down into chunks and process try (RandomAccessFile file = new RandomAccessFile(filePath.toFile(), "r")) { long fileSize = file.length(); int position = 0; // Sequentially upload parts of the file while (position < fileSize) { file.seek(position); int read = file.getChannel().read(bb); bb.flip(); // Swap position and limit before reading from the buffer UploadPartRequest uploadPartRequest = UploadPartRequest.builder() .bucket(bucketName) .key(objectKey) .uploadId(uploadId) .partNumber(partNumber) .build(); UploadPartResponse partResponse = s3Client.uploadPart( uploadPartRequest, RequestBody.fromByteBuffer(bb)); // Build the uploaded part CompletedPart uploadedPart = CompletedPart.builder() .partNumber(partNumber) .eTag(partResponse.eTag()) .build(); // Add the uploaded part to the list uploadedParts.add(uploadedPart); // Log to indicate the part upload is done logger.info("Uploaded part number: {} with ETag: {}", partNumber, partResponse.eTag()); bb.clear(); position += read; partNumber++; } } catch (S3Exception e) { logger.error("Failed to list parts: {} - Error code: {}", e.awsErrorDetails().errorMessage(), e.awsErrorDetails().errorCode()); throw e; } return uploadedParts; }
  • For API details, see UploadPart in AWS SDK for Java 2.x API Reference.

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.

PrivacidadTérminos del sitioPreferencias de cookies
© 2024, Amazon Web Services, Inc o sus afiliados. Todos los derechos reservados.