本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用SDK適用於 Java 的 執行和管理分段上傳
使用 Amazon S3 on Outposts,您可以在 AWS Outposts 資源上建立 S3 儲存貯體,並為需要本機資料存取、本機資料處理和資料駐留的應用程式在內部部署儲存和擷取物件。您可以透過 AWS Management Console、 AWS Command Line Interface (AWS CLI) AWS SDKs或 REST 使用 S3 on OutpostsAPI。如需詳細資訊,請參閱 什麼是 Amazon S3 on Outposts?
下列範例示範如何搭配 使用 S3 on Outposts AWS SDK for Java ,以執行和管理分段上傳。
主題
在 S3 on Outposts 儲存貯體中執行物件的分段上傳
下列 S3 on Outposts 範例使用 SDK for Java 啟動、上傳和完成物件至儲存貯體的分段上傳。若要使用此範例,請以您自己的資訊取代每個
。如需詳細資訊,請參閱 Amazon Simple Storage Service 使用者指南 中的使用分段上傳來上傳物件。user input placeholder
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; }
使用分段上傳在 S3 on Outposts 儲存貯體中複製大型物件
下列 S3 on Outposts 範例使用 SDK for Java 來複製儲存貯體中的物件。若要使用此範例,請以您自己的資訊取代每個
。user input
placeholder
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; } }
列出 S3 on Outposts 儲存貯體中物件的片段
下列 S3 on Outposts 範例使用 SDK for Java 列出儲存貯體中物件的部分。若要使用此範例,請以您自己的資訊取代每個
。user input
placeholder
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(); } } }
擷取 S3 on Outposts 儲存貯體中進行中的分段上傳清單
下列 S3 on Outposts 範例示範如何使用SDK適用於 Java 的 從 Outposts 儲存貯體擷取進行中分段上傳的清單。若要使用此範例,請以您自己的資訊取代每個
。user input placeholder
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(); } } }