在 Snowball Edge 设备上使用 S3 对象 - Amazon Snowball Edge 开发者指南
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

在 Snowball Edge 设备上使用 S3 对象

本部分介绍您可以在 Snow 系列设备上对与 Amazon S3 兼容的存储上的对象执行的各种操作。

将对象复制到 Snow 系列设备存储桶上与 Amazon S3 兼容的存储中

以下示例使用 Amazon CLI将名为 sample-object.xml 的文件上传到 Snow 系列设备存储桶上与 Amazon S3 兼容的存储,您拥有使用该存储桶的写入权限。要使用此命令,请将每个用户输入占位符替换为您自己的信息。

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(); } } }

从存储桶中获取对象

以下示例使用 Amazon CLI从 Snow 系列设备存储桶上与 Amazon S3 兼容的存储中获取一个名为 sample-object.xml 的对象。SDK 命令为 s3-snow:GetObject。要使用此命令,请将每个用户输入占位符替换为您自己的信息。

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

有关此命令的更多信息,请参阅《Amazon CLI 命令参考》中的 get-object

以下 Snow 系列设备上与 Amazon S3 兼容的存储示例使用适用于 Java 的开发工具包获取对象。要使用此命令,请将每个用户输入占位符替换为您自己的信息。有关更多信息,请参阅GetObject亚马逊简单存储服务 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.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(); } }

列出存储桶中的对象

以下示例使用 Amazon CLI列出了 Snow 系列设备存储桶上与 Amazon S3 兼容的存储。SDK 命令为 s3-snow:ListObjectsV2。要使用此命令,请将每个用户输入占位符替换为您自己的信息。

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

有关此命令的更多信息,请参阅《Amazon CLI 命令参考》中的 list-objects-v2

以下 Snow 系列设备上与 Amazon S3 兼容的存储示例使用适用于 Java 的开发工具包列出了存储桶中的对象。要使用此命令,请将每个用户输入占位符替换为您自己的信息。

此示例使用 ListObjectsV2,这是 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 兼容的存储中删除一个或多个对象。以下示例使用 Amazon CLI删除一个名为 sample-object.xml 的对象。要使用此命令,请将每个用户输入占位符替换为您自己的信息。

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

有关此命令的更多信息,请参阅《Amazon CLI 命令参考》中的 delete-object

以下 Snow 系列设备上与 Amazon S3 兼容的存储示例使用适用于 Java 的开发工具包删除存储桶中的对象。要使用此示例,请指定您想要删除的对象的密钥名称。有关更多信息,请参阅DeleteObject《亚马逊简单存储服务 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.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(); } } }