Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.
Ottieni le regole CORS per un bucket Amazon S3 utilizzando un SDK AWS
Gli esempi di codice seguenti mostrano come recuperare le regole CORS (Cross-Origin Resource Sharing, condivisione delle risorse multiorigine) per un bucket S3.
- .NET
-
- AWS SDK for .NET
-
Nota
C'è altro su. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS
. /// <summary> /// Retrieve the CORS configuration applied to the Amazon S3 bucket. /// </summary> /// <param name="client">The initialized Amazon S3 client object used /// to retrieve the CORS configuration.</param> /// <returns>The created CORS configuration object.</returns> private static async Task<CORSConfiguration> RetrieveCORSConfigurationAsync(AmazonS3Client client) { GetCORSConfigurationRequest request = new GetCORSConfigurationRequest() { BucketName = BucketName, }; var response = await client.GetCORSConfigurationAsync(request); var configuration = response.Configuration; PrintCORSRules(configuration); return configuration; }
-
Per i dettagli sull'API, GetBucketCorsconsulta AWS SDK for .NET API Reference.
-
- CLI
-
- AWS CLI
-
Il comando seguente recupera la configurazione Cross-Origin Resource Sharing per un bucket denominato:
my-bucket
aws s3api get-bucket-cors --bucket my-bucket
Output:
{ "CORSRules": [ { "AllowedHeaders": [ "*" ], "ExposeHeaders": [ "x-amz-server-side-encryption" ], "AllowedMethods": [ "PUT", "POST", "DELETE" ], "MaxAgeSeconds": 3000, "AllowedOrigins": [ "http://www.example.com" ] }, { "AllowedHeaders": [ "Authorization" ], "MaxAgeSeconds": 3000, "AllowedMethods": [ "GET" ], "AllowedOrigins": [ "*" ] } ] }
-
Per i dettagli sull'API, vedere GetBucketCors
in AWS CLI Command Reference.
-
- JavaScript
-
- SDK per JavaScript (v3)
-
Nota
C'è altro da fare. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS
. Recupera la policy CORS per il bucket.
import { GetBucketCorsCommand, S3Client } from "@aws-sdk/client-s3"; const client = new S3Client({}); export const main = async () => { const command = new GetBucketCorsCommand({ Bucket: "test-bucket", }); try { const { CORSRules } = await client.send(command); CORSRules.forEach((cr, i) => { console.log( `\nCORSRule ${i + 1}`, `\n${"-".repeat(10)}`, `\nAllowedHeaders: ${cr.AllowedHeaders.join(" ")}`, `\nAllowedMethods: ${cr.AllowedMethods.join(" ")}`, `\nAllowedOrigins: ${cr.AllowedOrigins.join(" ")}`, `\nExposeHeaders: ${cr.ExposeHeaders.join(" ")}`, `\nMaxAgeSeconds: ${cr.MaxAgeSeconds}`, ); }); } catch (err) { console.error(err); } };
-
Per ulteriori informazioni, consulta la Guida per sviluppatori di AWS SDK for JavaScript.
-
Per i dettagli sull'API, GetBucketCorsconsulta AWS SDK for JavaScript API Reference.
-
- Python
-
- SDK per Python (Boto3)
-
Nota
C'è altro su GitHub. Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS
. class BucketWrapper: """Encapsulates S3 bucket actions.""" def __init__(self, bucket): """ :param bucket: A Boto3 Bucket resource. This is a high-level resource in Boto3 that wraps bucket actions in a class-like structure. """ self.bucket = bucket self.name = bucket.name def get_cors(self): """ Get the CORS rules for the bucket. :return The CORS rules for the specified bucket. """ try: cors = self.bucket.Cors() logger.info( "Got CORS rules %s for bucket '%s'.", cors.cors_rules, self.bucket.name ) except ClientError: logger.exception(("Couldn't get CORS for bucket %s.", self.bucket.name)) raise else: return cors
-
Per i dettagli sull'API, consulta GetBucketCors AWSSDK for Python (Boto3) API Reference.
-
- Ruby
-
- SDK per Ruby
-
Nota
C'è di più su. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS
. require "aws-sdk-s3" # Wraps Amazon S3 bucket CORS configuration. class BucketCorsWrapper attr_reader :bucket_cors # @param bucket_cors [Aws::S3::BucketCors] A bucket CORS object configured with an existing bucket. def initialize(bucket_cors) @bucket_cors = bucket_cors end # Gets the CORS configuration of a bucket. # # @return [Aws::S3::Type::GetBucketCorsOutput, nil] The current CORS configuration for the bucket. def get_cors @bucket_cors.data rescue Aws::Errors::ServiceError => e puts "Couldn't get CORS configuration for #{@bucket_cors.bucket.name}. Here's why: #{e.message}" nil end end
-
Per i dettagli sull'API, GetBucketCorsconsulta AWS SDK for Ruby API Reference.
-
Per un elenco completo delle guide per sviluppatori AWS SDK e degli esempi di codice, consultaUtilizzo del servizio con un SDK AWS. Questo argomento include anche informazioni su come iniziare e dettagli sulle versioni precedenti dell'SDK.