搭GetBucketCors配 AWS SDK或使用 CLI - Amazon Simple Storage Service

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

GetBucketCors配 AWS SDK或使用 CLI

下列程式碼範例會示範如何使用GetBucketCors

.NET
AWS SDK for .NET
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在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; }
  • 如需詳API細資訊,請參閱AWS SDK for .NET API參考GetBucketCors中的。

CLI
AWS CLI

下列命令會擷取名my-bucket為的值區的跨來源資源共用設定:

aws s3api get-bucket-cors --bucket my-bucket

輸出:

{ "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": [ "*" ] } ] }
  • 如需詳API細資訊,請參閱AWS CLI 指令參考GetBucketCors中的。

JavaScript
SDK對於 JavaScript (3)
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫

取得值區的CORS政策。

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); } };
Python
SDK對於 Python(肉毒桿菌 3)
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在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
  • 如需詳API細資訊,請參閱GetBucketCorsAWS SDK的《Python (博多 3) API 參考資料》。

Ruby
SDK對於紅寶石
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在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
  • 如需詳API細資訊,請參閱AWS SDK for Ruby API參考GetBucketCors中的。

如需 AWS SDK開發人員指南和程式碼範例的完整清單,請參閱搭配 AWS SDK 使用此服務。本主題也包含有關入門的資訊以及舊SDK版的詳細資訊。