在 Snowball 邊緣裝置上使用 S3 物件 - AWS Snowball Edge 開發者指南

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

在 Snowball 邊緣裝置上使用 S3 物件

本節說明您可以在 Snow 系列裝置上使用 Amazon S3 相容儲存上的物件執行的各種操作。

將物件複製到 Snow 系列裝置儲存貯體上的 Amazon S3 相容儲存體

下列範例會上傳名為的檔案 sample-object.xml 儲存至 Snow 系列裝置儲存貯體上的 Amazon S3 相容儲存貯體,您具有使用 AWS CLI. 若要使用此指令,請以您自己的資訊取代每個使用者輸入預留位置。

aws s3api put-object --bucket sample-bucket --key sample-object.xml --body sample-object.xml --profile your-profile --endpoint-url s3api-endpoint-ip

下列 Snow 系列裝置上的 Amazon S3 相容儲存範例會使用 Java 將物件複製到同一儲存貯體中的SDK新物件。若要使用此指令,請以您自己的資訊取代每個使用者輸入預留位置。

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.CopyObjectRequest; add : import java.io.IOException; public class CopyObject { public static void main(String[] args) { String bucketName = "*** Bucket name ***"; String sourceKey = "*** Source object key ***"; String destinationKey = "*** Destination 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(); // Copy the object into a new object in the same bucket. CopyObjectRequest copyObjectRequest = new CopyObjectRequest(sourceKey, destinationKey); s3Client.copyObject(copyObjectRequest); CopyObjectRequest copyObjectRequest = CopyObjectRequest.builder() .sourceKey(sourceKey) .destinationKey(destKey) .build(); } 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(); } } }

從值區取得物件

下列範例會取得名為的物件 sample-object.xml 從 Snow 系列設備存儲桶上的 Amazon S3 兼容存儲使用 AWS CLI. 命SDK令是s3-snow:GetObject。若要使用此指令,請以您自己的資訊取代每個使用者輸入預留位置。

aws s3api get-object --bucket sample-bucket --key sample-object.xml --profile your-profile --endpoint-url s3api-endpoint-ip

若要取得有關此指令的更多資訊,請參閱《指AWS CLI 令參考》中的 get-object

下列 Snow 系列裝置上的 Amazon S3 相容儲存範例會取得使用 Java SDK 的物件。若要使用此指令,請以您自己的資訊取代每個使用者輸入預留位置。如需詳細資訊,請參閱 Amazon 簡易儲存服務API參考GetObject中的。

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.GetObjectRequest; import com.amazonaws.services.s3.model.ResponseHeaderOverrides; import com.amazonaws.services.s3.model.S3Object; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class GetObject { public static void main(String[] args) throws IOException { String bucketName = "*** Bucket name ***"; String key = "*** Object key ***"; S3Object fullObject = null, objectPortion = null, headerOverrideObject = null; 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(); GetObjectRequest getObjectRequest = GetObjectRequest.builder() .bucket(bucketName) .key(key) .build()); s3Client.getObject(getObjectRequest); } 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(); } finally { // To ensure that the network connection doesn't remain open, close any open input streams. if (fullObject != null) { fullObject.close(); } if (objectPortion != null) { objectPortion.close(); } if (headerOverrideObject != null) { headerOverrideObject.close(); } } } private static void displayTextInputStream(InputStream input) throws IOException { // Read the text input stream one line at a time and display each line. BufferedReader reader = new BufferedReader(new InputStreamReader(input)); String line = null; while ((line = reader.readLine()) != null) { System.out.println(line); } System.out.println(); } }

列出值區中的物件

下列範例使用列出 Snow 系列裝置儲存貯體上 Amazon S3 相容儲存貯體中的物件 AWS CLI。命SDK令是s3-snow:ListObjectsV2。若要使用此指令,請以您自己的資訊取代每個使用者輸入預留位置。

aws s3api list-objects-v2 --bucket sample-bucket --profile your-profile --endpoint-url s3api-endpoint-ip

若要取得有關此指令的更多資訊,請參閱《AWS CLI 指令參考》中的 list-objects-v2

下列 Snow 系列裝置上的 Amazon S3 相容儲存範例會列出儲存貯體中使用 Java SDK 的物件。若要使用此指令,請以您自己的資訊取代每個使用者輸入預留位置。

此範例使用 ListObjectsV2,V2 是 ListObjects API作業的最新修訂版本。我們建議您使用此修改後的API操作進行應用程序開發。為了向後相容,Amazon S3 會繼續支援此API操作的先前版本。

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.ListObjectsV2Request; import com.amazonaws.services.s3.model.ListObjectsV2Result; import com.amazonaws.services.s3.model.S3ObjectSummary; public class ListObjectsV2 { public static void main(String[] args) { String bucketName = "*** Bucket name ***"; 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(); System.out.println("Listing objects"); // maxKeys is set to 2 to demonstrate the use of // ListObjectsV2Result.getNextContinuationToken() ListObjectsV2Request req = new ListObjectsV2Request().withBucketName(bucketName).withMaxKeys(2); ListObjectsV2Result result; do { result = s3Client.listObjectsV2(req); for (S3ObjectSummary objectSummary : result.getObjectSummaries()) { System.out.printf(" - %s (size: %d)\n", objectSummary.getKey(), objectSummary.getSize()); } // If there are more than maxKeys keys in the bucket, get a continuation token // and list the next objects. String token = result.getNextContinuationToken(); System.out.println("Next Continuation Token: " + token); req.setContinuationToken(token); } while (result.isTruncated()); } 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(); } } }

刪除值區中的物件

您可以從 Snow 系列裝置儲存貯體上的 Amazon S3 相容儲存體中刪除一或多個物件。下列範例會刪除名為的物件 sample-object.xml 使用 AWS CLI. 若要使用此指令,請以您自己的資訊取代每個使用者輸入預留位置。

aws s3api delete-object --bucket sample-bucket --key key --profile your-profile --endpoint-url s3api-endpoint-ip

若要取得有關此指令的更多資訊,請參閱《指AWS CLI 令參考》中的刪除物件

下列 Snow 系列裝置上的 Amazon S3 相容儲存範例會使用 Java 刪除儲存貯體中SDK的物件。若要使用此範例,請指定要刪除之物件的索引鍵名稱。如需詳細資訊,請參閱 Amazon 簡易儲存服務API參考DeleteObject中的。

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.DeleteObjectRequest; public class DeleteObject { public static void main(String[] args) { String bucketName = "*** Bucket name ***"; String keyName = "*** key name ****"; 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(); DeleteObjectRequest deleteObjectRequest = DeleteObjectRequest.builder() .bucket(bucketName) .key(keyName) .build())); s3Client.deleteObject(deleteObjectRequest); } 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(); } } }