Durchführen und Verwalten eines mehrteiligen Uploads mit dem SDK for Java - Amazon Simple Storage Service

Die vorliegende Übersetzung wurde maschinell erstellt. Im Falle eines Konflikts oder eines Widerspruchs zwischen dieser übersetzten Fassung und der englischen Fassung (einschließlich infolge von Verzögerungen bei der Übersetzung) ist die englische Fassung maßgeblich.

Durchführen und Verwalten eines mehrteiligen Uploads mit dem SDK for Java

Mit Amazon S3 in Outposts können Sie S3-Buckets in Ihren AWS Outposts-Ressourcen erstellen und Objekte On-Premises für Anwendungen speichern und abrufen, die einen lokalen Datenzugriff, eine lokale Datenverarbeitung und eine lokale Datenresidenz erfordern. Sie können S3 on Outposts über die AWS Management Console, AWS Command Line Interface (AWS CLI), AWS-SDKs oder die REST-API verwenden. Weitere Informationen finden Sie unter Was ist Amazon S3 on Outposts?.

Die folgenden Beispiele veranschaulichen, wie Sie S3 on Outposts mit der AWS SDK for Java verwenden können, um einen mehrteiligen Upload durchzuführen und zu verwalten.

Durchführen eines mehrteiligen Uploads eines Objekts in einem S3-on-Outposts-Bucket

Das folgende S3-on-Outposts-Beispiel initiiert, lädt und beendet einen mehrteiligen Upload eines Objekts in einen Bucket mithilfe des SDK for Java. Zum Verwenden dieses Beispiels ersetzen Sie jeden user input placeholder durch Ihre eigenen Informationen. Weitere Informationen finden Sie unter Hochladen eines Objekts mit Multipart-Upload.

import com.amazonaws.AmazonServiceException; import com.amazonaws.SdkClientException; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.*; import java.util.ArrayList; import java.util.List; public class MultipartUploadCopy { public static void main(String[] args) { String accessPointArn = "*** Source access point ARN ***"; String sourceObjectKey = "*** Source object key ***"; String destObjectKey = "*** Target object key ***"; try { // This code expects that you have AWS credentials set up per: // https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/setup-credentials.html AmazonS3 s3Client = AmazonS3ClientBuilder.standard() .enableUseArnRegion() .build(); // Initiate the multipart upload. InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(accessPointArn, destObjectKey); InitiateMultipartUploadResult initResult = s3Client.initiateMultipartUpload(initRequest); // Get the object size to track the end of the copy operation. GetObjectMetadataRequest metadataRequest = new GetObjectMetadataRequest(accessPointArn, sourceObjectKey); ObjectMetadata metadataResult = s3Client.getObjectMetadata(metadataRequest); long objectSize = metadataResult.getContentLength(); // Copy the object using 5 MB parts. long partSize = 5 * 1024 * 1024; long bytePosition = 0; int partNum = 1; List<CopyPartResult> copyResponses = new ArrayList<CopyPartResult>(); while (bytePosition < objectSize) { // The last part might be smaller than partSize, so check to make sure // that lastByte isn't beyond the end of the object. long lastByte = Math.min(bytePosition + partSize - 1, objectSize - 1); // Copy this part. CopyPartRequest copyRequest = new CopyPartRequest() .withSourceBucketName(accessPointArn) .withSourceKey(sourceObjectKey) .withDestinationBucketName(accessPointArn) .withDestinationKey(destObjectKey) .withUploadId(initResult.getUploadId()) .withFirstByte(bytePosition) .withLastByte(lastByte) .withPartNumber(partNum++); copyResponses.add(s3Client.copyPart(copyRequest)); bytePosition += partSize; } // Complete the upload request to concatenate all uploaded parts and make the copied object available. CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest( accessPointArn, destObjectKey, initResult.getUploadId(), getETags(copyResponses)); s3Client.completeMultipartUpload(completeRequest); System.out.println("Multipart copy complete."); } catch (AmazonServiceException e) { // The call was transmitted successfully, but Amazon S3 couldn't process // it, so it returned an error response. e.printStackTrace(); } catch (SdkClientException e) { // Amazon S3 couldn't be contacted for a response, or the client // couldn't parse the response from Amazon S3. e.printStackTrace(); } } // This is a helper function to construct a list of ETags. private static List<PartETag> getETags(List<CopyPartResult> responses) { List<PartETag> etags = new ArrayList<PartETag>(); for (CopyPartResult response : responses) { etags.add(new PartETag(response.getPartNumber(), response.getETag())); } return etags; }

Kopieren eines großen Objekts in einem S3-on-Outposts-Bucket mithilfe eines mehrteiligen Uploads

Im folgenden Beispiel wird ein Objekt mithilfe des SDK for Java in einem S3-on-Outposts-Bucket kopiert. Zum Verwenden dieses Beispiels ersetzen Sie jeden user input placeholder durch Ihre eigenen Informationen. Dieses Beispiel basiert auf Kopieren eines Objekts mit Multipart-Upload.

import com.amazonaws.AmazonServiceException; import com.amazonaws.SdkClientException; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.*; import java.util.ArrayList; import java.util.List; public class MultipartUploadCopy { public static void main(String[] args) { String accessPointArn = "*** Source access point ARN ***"; String sourceObjectKey = "*** Source object key ***"; String destObjectKey = "*** Target object key ***"; try { // This code expects that you have AWS credentials set up per: // https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/setup-credentials.html AmazonS3 s3Client = AmazonS3ClientBuilder.standard() .enableUseArnRegion() .build(); // Initiate the multipart upload. InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(accessPointArn, destObjectKey); InitiateMultipartUploadResult initResult = s3Client.initiateMultipartUpload(initRequest); // Get the object size to track the end of the copy operation. GetObjectMetadataRequest metadataRequest = new GetObjectMetadataRequest(accessPointArn, sourceObjectKey); ObjectMetadata metadataResult = s3Client.getObjectMetadata(metadataRequest); long objectSize = metadataResult.getContentLength(); // Copy the object using 5 MB parts. long partSize = 5 * 1024 * 1024; long bytePosition = 0; int partNum = 1; List<CopyPartResult> copyResponses = new ArrayList<CopyPartResult>(); while (bytePosition < objectSize) { // The last part might be smaller than partSize, so check to make sure // that lastByte isn't beyond the end of the object. long lastByte = Math.min(bytePosition + partSize - 1, objectSize - 1); // Copy this part. CopyPartRequest copyRequest = new CopyPartRequest() .withSourceBucketName(accessPointArn) .withSourceKey(sourceObjectKey) .withDestinationBucketName(accessPointArn) .withDestinationKey(destObjectKey) .withUploadId(initResult.getUploadId()) .withFirstByte(bytePosition) .withLastByte(lastByte) .withPartNumber(partNum++); copyResponses.add(s3Client.copyPart(copyRequest)); bytePosition += partSize; } // Complete the upload request to concatenate all uploaded parts and make the copied object available. CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest( accessPointArn, destObjectKey, initResult.getUploadId(), getETags(copyResponses)); s3Client.completeMultipartUpload(completeRequest); System.out.println("Multipart copy complete."); } catch (AmazonServiceException e) { // The call was transmitted successfully, but Amazon S3 couldn't process // it, so it returned an error response. e.printStackTrace(); } catch (SdkClientException e) { // Amazon S3 couldn't be contacted for a response, or the client // couldn't parse the response from Amazon S3. e.printStackTrace(); } } // This is a helper function to construct a list of ETags. private static List<PartETag> getETags(List<CopyPartResult> responses) { List<PartETag> etags = new ArrayList<PartETag>(); for (CopyPartResult response : responses) { etags.add(new PartETag(response.getPartNumber(), response.getETag())); } return etags; } }

Auflisten von Teilen eines Objekts in einem S3-on-Outposts-Bucket

Das folgende S3-on-Outposts-Beispiel listet die Teile eines Objekts in einem Bucket mit dem SDK for Java auf. Zum Verwenden dieses Beispiels ersetzen Sie jeden user input placeholder durch Ihre eigenen Informationen.

import com.amazonaws.AmazonServiceException; import com.amazonaws.SdkClientException; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.*; import java.util.List; public class ListParts { public static void main(String[] args) { String accessPointArn = "*** access point ARN ***"; String keyName = "*** Key name ***"; String uploadId = "*** Upload ID ***"; try { // This code expects that you have AWS credentials set up per: // https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/setup-credentials.html AmazonS3 s3Client = AmazonS3ClientBuilder.standard() .enableUseArnRegion() .build(); ListPartsRequest listPartsRequest = new ListPartsRequest(accessPointArn, keyName, uploadId); PartListing partListing = s3Client.listParts(listPartsRequest); List<PartSummary> partSummaries = partListing.getParts(); System.out.println(partSummaries.size() + " multipart upload parts"); for (PartSummary p : partSummaries) { System.out.println("Upload part: Part number = \"" + p.getPartNumber() + "\", ETag = " + p.getETag()); } } catch (AmazonServiceException e) { // The call was transmitted successfully, but Amazon S3 couldn't process // it, so it returned an error response. e.printStackTrace(); } catch (SdkClientException e) { // Amazon S3 couldn't be contacted for a response, or the client // couldn't parse the response from Amazon S3. e.printStackTrace(); } } }

Abrufen einer Liste der in Bearbeitung befindlichen mehrteiligen Uploads in einem S3-on-Outposts-Bucket

Das folgende S3-on-Outposts-Beispiel zeigt, wie Sie mit dem SDK for Java eine Liste der laufenden mehrteiligen Uploads aus einem Outposts-Bucket abrufen. Zum Verwenden dieses Beispiels ersetzen Sie jeden user input placeholder durch Ihre eigenen Informationen. Dies ist ein Beispiel, das aus dem Beispiel für Auflisten von mehrteiligen Uploads für Amazon S3 adaptiert wurde.

import com.amazonaws.AmazonServiceException; import com.amazonaws.SdkClientException; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.ListMultipartUploadsRequest; import com.amazonaws.services.s3.model.MultipartUpload; import com.amazonaws.services.s3.model.MultipartUploadListing; import java.util.List; public class ListMultipartUploads { public static void main(String[] args) { String accessPointArn = "*** access point ARN ***"; try { // This code expects that you have AWS credentials set up per: // https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/setup-credentials.html AmazonS3 s3Client = AmazonS3ClientBuilder.standard() .enableUseArnRegion() .build(); // Retrieve a list of all in-progress multipart uploads. ListMultipartUploadsRequest allMultipartUploadsRequest = new ListMultipartUploadsRequest(accessPointArn); MultipartUploadListing multipartUploadListing = s3Client.listMultipartUploads(allMultipartUploadsRequest); List<MultipartUpload> uploads = multipartUploadListing.getMultipartUploads(); // Display information about all in-progress multipart uploads. System.out.println(uploads.size() + " multipart upload(s) in progress."); for (MultipartUpload u : uploads) { System.out.println("Upload in progress: Key = \"" + u.getKey() + "\", id = " + u.getUploadId()); } } catch (AmazonServiceException e) { // The call was transmitted successfully, but Amazon S3 couldn't process // it, so it returned an error response. e.printStackTrace(); } catch (SdkClientException e) { // Amazon S3 couldn't be contacted for a response, or the client // couldn't parse the response from Amazon S3. e.printStackTrace(); } } }