

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

# 從 Amason S3 on Outposts 儲存貯體取得物件
<a name="S3OutpostsGetObject"></a>

物件是存放在 Amazon S3 on Outposts 中的基本實體。每個物件都包含在儲存貯體中。您必須使用存取點來存取 Outpost 儲存貯體中的任何物件。針對物件操作指定儲存貯體時，您可以使用存取點 Amazon Resource Name (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 SDKs 透過存取點上傳和管理物件。

下列範例示範如何使用 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](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/s3api/get-object.html)。

```
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 開發套件
<a name="S3OutpostsGetObjectJava"></a>

下列 S3 on Outposts 範例使用適用於 Java 的開發套件取得物件。若要使用此範例，請以您自己的資訊取代每個 `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();
    }
}
```