

# S3 Access Grants が提供する認証情報を使用した S3 データへのアクセス
<a name="access-grants-get-data"></a>

被付与者が[アクセス許可を通じて一時的な認証情報を取得](https://docs.aws.amazon.com/AmazonS3/latest/userguide/access-grants-credentials.html) すると、その一時的な認証情報を使用して Amazon S3 API オペレーションを呼び出し、データにアクセスできます。

被付与者は、AWS Command Line Interface (AWS CLI)、AWS SDK、Amazon S3 REST API を使用して、S3 データにアクセスできます。さらに、AWS [Python](https://github.com/aws/boto3-s3-access-grants-plugin) および [Java](https://github.com/aws/aws-s3-accessgrants-plugin-java-v2) プラグインを使用して S3 Access Grants を呼び出すことができます。

## の使用AWS CLI
<a name="access-grants-get-data-cli"></a>

被付与者は S3 Access Grants から一時的な認証情報を取得したら、その認証情報を使用してプロファイルを設定してデータを取得できます。

AWS CLI をインストールするには、**「AWS Command Line Interface ユーザーガイド」の「[AWS CLI をインストールする](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)」を参照してください。

次のコマンド例を使用する際は、`{{user input placeholders}}` をユーザー自身の情報に置き換えます。

**Example – プロファイルを設定する**  

```
aws configure set aws_access_key_id "{{$accessKey}}" --profile {{access-grants-consumer-access-profile}}
aws configure set aws_secret_access_key "{{$secretKey}}" --profile {{access-grants-consumer-access-profile}}
aws configure set aws_session_token "{{$sessionToken}}" --profile {{access-grants-consumer-access-profile}}
```

次のコマンド例を使用するには、`{{user input placeholders}}` をユーザー自身の情報に置き換えます。

**Example – S3 データを取得する**  
被付与者は、[https://docs.aws.amazon.com/cli/latest/reference/s3api/get-object.html](https://docs.aws.amazon.com/cli/latest/reference/s3api/get-object.html) AWS CLI コマンドを使用して、データにアクセスできます。被付与者は、[https://docs.aws.amazon.com/cli/latest/reference/s3api/put-object.html](https://docs.aws.amazon.com/cli/latest/reference/s3api/put-object.html)、[https://docs.aws.amazon.com/cli/latest/reference/s3/ls.html](https://docs.aws.amazon.com/cli/latest/reference/s3/ls.html)、その他の S3 AWS CLI コマンドも使用できます。  

```
aws s3api get-object \
--bucket {{amzn-s3-demo-bucket1}} \
--key {{myprefix}} \
--region {{us-east-2}} \
--profile {{access-grants-consumer-access-profile}}
```

## AWS SDK の使用
<a name="access-grants-get-data-using-sdk"></a>

このセクションでは、AWS SDK を使用して S3 データにアクセスする方法の例を説明します。

------
#### [ Java ]

次の Java コード例では、S3 バケットからオブジェクトを取得します。作業サンプルの作成およびテストの手順については、「AWS SDK for Java デベロッパーガイド」の「[使用開始](https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/getting-started.html)」を参照してください。**

```
import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.regions.Regions;
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 GetObject2 {

    public static void main(String[] args) throws IOException {
        Regions clientRegion = Regions.DEFAULT_REGION;
        String bucketName = "*** Bucket name ***";
        String key = "*** Object key ***";

        S3Object fullObject = null, objectPortion = null, headerOverrideObject = null;
        try {
            AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                    .withRegion(clientRegion)
                    .withCredentials(new ProfileCredentialsProvider())
                    .build();

            // Get an object and print its contents.
            System.out.println("Downloading an object");
            fullObject = s3Client.getObject(new GetObjectRequest(bucketName, 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(bucketName, 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(bucketName, 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();
    }
}
```

------

## S3 Access Grants でサポートされている S3 アクション
<a name="access-grants-s3-actions"></a>

被付与者は、S3 Access Grants によって提供される一時的な認証情報を使用して、アクセスできる S3 データに対して S3 アクションを実行できます。以下は、被付与者が実行できる S3 アクションのリストです。許可されるアクションは、アクセス許可で付与されるアクセス許可のレベル、`READ`、`WRITE` または `READWRITE` によって異なります。

**注記**  
以下に示す Amazon S3 アクセス許可に加えて、Amazon S3 は AWS Key Management Service (AWS KMS) [Decrypt](https://docs.aws.amazon.com/kms/latest/APIReference/API_Decrypt.html) (`kms:decrypt`) `READ` アクセス許可または AWS KMS [GenerateDataKey](https://docs.aws.amazon.com/kms/latest/APIReference/API_GenerateDataKey.html) (`kms:generateDataKey`) `WRITE` アクセス許可を呼び出すことができます。これらのアクセス許可では、AWS KMS キーへの直接アクセスは許可されません。


****  

| S3 IAM アクション | API アクションとドキュメント | S3 Access Grants アクセス許可 | S3 リソース | 
| --- | --- | --- | --- | 
| s3:GetObject | [GetObject](https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetObject.html) | READ | オブジェクト | 
| s3:GetObjectVersion | [GetObject](https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetObject.html) | READ | オブジェクト | 
| s3:GetObjectAcl | [GetObjectAcl](https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetObjectAcl.html) | READ | オブジェクト | 
| s3:GetObjectVersionAcl | [GetObjectAcl](https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetObjectAcl.html) | READ | オブジェクト | 
| s3:ListMultipartUploads | [ListParts](https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListParts.html) | READ | オブジェクト | 
| s3:PutObject | [PutObject](https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutObject.html)、[CreateMultipartUpload](https://docs.aws.amazon.com/AmazonS3/latest/API/API_CreateMultipartUpload.html)、[UploadPart](https://docs.aws.amazon.com/AmazonS3/latest/API/API_UploadPart.html)、[UploadPartCopy](https://docs.aws.amazon.com/AmazonS3/latest/API/API_UploadPartCopy.html)、[CompleteMultipartUpload](https://docs.aws.amazon.com/AmazonS3/latest/API/API_CompleteMultipartUpload.html) | WRITE | オブジェクト | 
| s3:PutObjectAcl | [PutObjectAcl](https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutObjectAcl.html) | WRITE | オブジェクト | 
| s3:PutObjectVersionAcl | [PutObjectAcl](https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutObjectAcl.html) | WRITE | オブジェクト | 
| s3:DeleteObject | [DeleteObject](https://docs.aws.amazon.com/AmazonS3/latest/API/API_DeleteObject.html) | WRITE | オブジェクト | 
| s3:DeleteObjectVersion | [DeleteObject](https://docs.aws.amazon.com/AmazonS3/latest/API/API_DeleteObject.html) | WRITE | オブジェクト | 
| s3:AbortMultipartUpload | [AbortMultipartUpload](https://docs.aws.amazon.com/AmazonS3/latest/API/API_AbortMultipartUpload.html) | WRITE | オブジェクト | 
| s3:ListBucket | [HeadBucket](https://docs.aws.amazon.com/AmazonS3/latest/API/API_HeadBucket.html)、[ListObjectsV2](https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListObjectsV2.html)、[ListObjects](https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListObjects.html) | READ | バケット | 
| s3:ListBucketVersions | [ListObjectVersions](https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListObjectVersions.html) | READ | バケット | 
| s3:ListBucketMultipartUploads | [ListMultipartUploads](https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListMultipartUploads.html) | READ | バケット | 