

# 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) と AWS SDK for Java を使用してオブジェクトをダウンロード (取得) する方法を示しています。

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

次の例では、S3 on Outposts バケット (`s3-outposts:GetObject`) から、AWS CLI を使って `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
```

## AWS SDK for Java の使用
<a name="S3OutpostsGetObjectJava"></a>

次の S3 on Outposts の例では、SDK for Java を使用してオブジェクトを取得します。この例を実行するには、それぞれの `user input placeholder` をユーザー自身の情報に置き換えます。詳細については、[Amazon Simple Storage Service API リファレンス](https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetObject.html)の *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 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();
    }
}
```