

# 从 Amazon S3 on Outposts 存储桶获取对象
<a name="S3OutpostsGetObject"></a>

对象是 Amazon S3 on Outposts 中存储的基础实体。每个对象都储存在一个存储桶中。您必须使用访问点才能访问 Outpost 存储桶中的任何对象。当您为对象操作指定桶时，可以使用访问点 Amazon 资源名称（ARN）或访问点别名。有关访问点别名的更多信息，请参阅[为您的 S3 on Outposts 桶访问点使用桶式别名](s3-outposts-access-points-alias.md)。

以下示例显示了 S3 on Outposts 访问点的 ARN 格式，其中包括 Outpost 所属区域的 AWS 区域 代码、AWS 账户 ID、Outpost ID 和访问点名称：

```
arn:aws:s3-outposts:region:account-id:outpost/outpost-id/accesspoint/accesspoint-name
```

有关 S3 on Outposts ARN 的更多信息，请参阅[S3 on Outposts 的资源 ARN](S3OutpostsIAM.md#S3OutpostsARN)。

对于 Amazon S3 on Outposts，对象数据始终存储在 Outpost 上。当 AWS 安装 Outpost 机架时，您的数据将保留在 Outpost 的本地，以满足数据驻留要求。您的对象永远不会离开您的 Outpost，也不在 AWS 区域 中。由于 AWS 管理控制台 托管在区域内，您无法使用控制台上传或管理 Outpost 中的对象。但是，您可以使用 REST API、AWS Command Line Interface (AWS CLI) 和 AWS SDK 通过访问点上传和管理对象。

下面的示例演示如何使用 AWS Command Line Interface (AWS CLI) 和适用于 Java 的 AWS SDK 下载（获取）对象。

## 使用 AWS CLI
<a name="S3OutpostsGetObjectCLI"></a>

以下示例使用 AWS CLI 从 S3 on Outposts 存储桶 (`s3-outposts:GetObject`) 获取一个名为 `sample-object.xml` 的对象。要使用此命令，请将每个 `user input placeholder` 替换为您自己的信息。有关此命令的更多信息，请参阅《AWS CLI 参考》中的 *get-object*。

```
aws s3api get-object --bucket arn:aws:s3-outposts:region:123456789012:outpost/op-01ac5d28a6a232904/accesspoint/example-outposts-access-point --key testkey sample-object.xml
```

## 使用适用于 Java 的 AWS SDK
<a name="S3OutpostsGetObjectJava"></a>

以下 S3 on Outposts 示例使用适用于 Java 的 SDK 获取对象。要使用此示例，请将每个 `user input placeholder` 替换为您自己的信息。有关更多信息，请参阅 *Amazon Simple Storage Service API 参考*中的 [GetObject](https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetObject.html)。

```
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 accessPointArn = "*** access point ARN ***";
        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();

            // Get an object and print its contents.
            System.out.println("Downloading an object");
            fullObject = s3Client.getObject(new GetObjectRequest(accessPointArn, key));
            System.out.println("Content-Type: " + fullObject.getObjectMetadata().getContentType());
            System.out.println("Content: ");
            displayTextInputStream(fullObject.getObjectContent());

            // Get a range of bytes from an object and print the bytes.
            GetObjectRequest rangeObjectRequest = new GetObjectRequest(accessPointArn, key)
                    .withRange(0, 9);
            objectPortion = s3Client.getObject(rangeObjectRequest);
            System.out.println("Printing bytes retrieved.");
            displayTextInputStream(objectPortion.getObjectContent());

            // Get an entire object, overriding the specified response headers, and print the object's content.
            ResponseHeaderOverrides headerOverrides = new ResponseHeaderOverrides()
                    .withCacheControl("No-cache")
                    .withContentDisposition("attachment; filename=example.txt");
            GetObjectRequest getObjectRequestHeaderOverride = new GetObjectRequest(accessPointArn, key)
                    .withResponseHeaders(headerOverrides);
            headerOverrideObject = s3Client.getObject(getObjectRequestHeaderOverride);
            displayTextInputStream(headerOverrideObject.getObjectContent());
        } 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();
    }
}
```