Accedere ai dati S3 utilizzando le credenziali fornite da S3 Access Grants - Amazon Simple Storage Service

Accedere ai dati S3 utilizzando le credenziali fornite da S3 Access Grants

Dopo aver ottenuto le credenziali temporanee tramite la concessione di accesso, un assegnatario può utilizzare tali credenziali per chiamare le operazioni API di Amazon S3 per accedere ai tuoi dati.

Gli assegnatari possono accedere ai dati S3 utilizzando AWS Command Line Interface (AWS CLI), gli AWS SDK e la REST API di Amazon S3. Inoltre, è possibile utilizzare i plugin AWS Python e Java per chiamare S3 Access Grants

Dopo aver ottenuto le credenziali temporanee da S3 Access Grants, l'assegnatario può configurare un profilo con tali credenziali per richiamare i dati.

Per installare AWS CLI, consulta Installazione dell'AWS CLI nella Guida per l'utente di AWS Command Line Interface.

Per utilizzare i seguenti comandi di esempio, sostituisci user input placeholders con le tue informazioni.

Esempio – Configura un profilo
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

Per utilizzare il seguente comando di esempio, sostituisci user input placeholders con le tue informazioni.

Esempio – Ottieni i dati S3

L'assegnatario può utilizzare il comando get-object dell'AWS CLI per accedere ai dati. L’assegnatario può anche utilizzare put-object, ls e altri comandi AWS CLI S3.

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

Questa sezione fornisce esempi di come gli assegnatari possono accedere ai dati S3 utilizzando gli AWS SDK.

Java

Il seguente esempio di codice Java recupera un oggetto da un bucket S3. Per istruzioni su come creare e testare un campione funzionante, consulta Nozioni di base nella Guida per gli sviluppatori di AWS SDK per Java.

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

Azioni S3 supportate in S3 Access Grants

Un beneficiario può utilizzare la credenziale temporanea fornita da S3 Access Grants per eseguire azioni S3 sui dati S3 a cui ha accesso. Di seguito è riportato un elenco di azioni S3 consentite che un beneficiario può eseguire. Le azioni consentite dipendono dal livello di autorizzazione concesso nella concessione di accesso, READ, WRITE, o READWRITE.

Nota

Oltre alle autorizzazioni di Amazon S3 elencate di seguito, Amazon S3 può richiedere l’autorizzazione AWS Key Management Service (AWS KMS) Decrypt (kms:decrypt) READ o l’autorizzazione AWS KMS GenerateDataKey (kms:generateDataKey) WRITE. Queste autorizzazioni non consentono l'accesso diretto alla chiave AWS KMS.

Azione S3 IAM Azione e documento API Autorizzazione S3 Access Grants Risorsa S3
s3:GetObject GetObject READ Oggetto
s3:GetObjectVersion GetObject READ Oggetto
s3:GetObjectAcl GetObjectAcl READ Oggetto
s3:GetObjectVersionAcl GetObjectAcl READ Oggetto
s3:ListMultipartUploads ListParts READ Oggetto
s3:PutObject PutObject, CreateMultipartUpload, UploadPart, UploadPartCopy, CompleteMultipartUpload WRITE Oggetto
s3:PutObjectAcl PutObjectAcl WRITE Oggetto
s3:PutObjectVersionAcl PutObjectAcl WRITE Oggetto
s3:DeleteObject DeleteObject WRITE Oggetto
s3:DeleteObjectVersion DeleteObject WRITE Oggetto
s3:AbortMultipartUpload AbortMultipartUpload WRITE Oggetto
s3:ListBucket HeadBucket, ListObjectsV2, ListObjects READ Bucket
s3:ListBucketVersions ListObjectVersions READ Bucket
s3:ListBucketMultipartUploads ListMultipartUploads READ Bucket