- Navigation GuideYou are on a Command (operation) page with structural examples. Use the navigation breadcrumb if you would like to return to the Client landing page.
PutBucketCorsCommand
This operation is not supported for directory buckets.
Sets the cors
configuration for your bucket. If the configuration exists, Amazon S3 replaces it.
To use this operation, you must be allowed to perform the s3:PutBucketCORS
action. By default, the bucket owner has this permission and can grant it to others.
You set this configuration on a bucket so that the bucket can service cross-origin requests. For example, you might want to enable a request whose origin is http://www.example.com
to access your Amazon S3 bucket at my.example.bucket.com
by using the browser's XMLHttpRequest
capability.
To enable cross-origin resource sharing (CORS) on a bucket, you add the cors
subresource to the bucket. The cors
subresource is an XML document in which you configure rules that identify origins and the HTTP methods that can be executed on your bucket. The document is limited to 64 KB in size.
When Amazon S3 receives a cross-origin request (or a pre-flight OPTIONS request) against a bucket, it evaluates the cors
configuration on the bucket and uses the first CORSRule
rule that matches the incoming browser request to enable a cross-origin request. For a rule to match, the following conditions must be met:
-
The request's
Origin
header must matchAllowedOrigin
elements. -
The request method (for example, GET, PUT, HEAD, and so on) or the
Access-Control-Request-Method
header in case of a pre-flightOPTIONS
request must be one of theAllowedMethod
elements. -
Every header specified in the
Access-Control-Request-Headers
request header of a pre-flight request must match anAllowedHeader
element.
For more information about CORS, go to Enabling Cross-Origin Resource Sharing in the Amazon S3 User Guide.
The following operations are related to PutBucketCors
:
Example Syntax
Use a bare-bones client and the command you need to make an API call.
import { S3Client, PutBucketCorsCommand } from "@aws-sdk/client-s3"; // ES Modules import
// const { S3Client, PutBucketCorsCommand } = require("@aws-sdk/client-s3"); // CommonJS import
const client = new S3Client(config);
const input = { // PutBucketCorsRequest
Bucket: "STRING_VALUE", // required
CORSConfiguration: { // CORSConfiguration
CORSRules: [ // CORSRules // required
{ // CORSRule
ID: "STRING_VALUE",
AllowedHeaders: [ // AllowedHeaders
"STRING_VALUE",
],
AllowedMethods: [ // AllowedMethods // required
"STRING_VALUE",
],
AllowedOrigins: [ // AllowedOrigins // required
"STRING_VALUE",
],
ExposeHeaders: [ // ExposeHeaders
"STRING_VALUE",
],
MaxAgeSeconds: Number("int"),
},
],
},
ContentMD5: "STRING_VALUE",
ChecksumAlgorithm: "CRC32" || "CRC32C" || "SHA1" || "SHA256" || "CRC64NVME",
ExpectedBucketOwner: "STRING_VALUE",
};
const command = new PutBucketCorsCommand(input);
const response = await client.send(command);
// {};
Example Usage
PutBucketCorsCommand Input
Parameter | Type | Description |
---|
Parameter | Type | Description |
---|---|---|
Bucket Required | string | undefined | Specifies the bucket impacted by the Note: To supply the Multi-region Access Point (MRAP) to Bucket, you need to install the "aws-sdk/signature-v4-crt" package to your project dependencies. For more information, please go to https://github.com/aws/aws-sdk-js-v3#known-issues |
CORSConfiguration Required | CORSConfiguration | undefined | Describes the cross-origin access configuration for objects in an Amazon S3 bucket. For more information, see Enabling Cross-Origin Resource Sharing in the Amazon S3 User Guide. |
ChecksumAlgorithm | ChecksumAlgorithm | undefined | Indicates the algorithm used to create the checksum for the request when you use the SDK. This header will not provide any additional functionality if you don't use the SDK. When you send this header, there must be a corresponding If you provide an individual checksum, Amazon S3 ignores any provided |
ContentMD5 | string | undefined | The Base64 encoded 128-bit For requests made using the Amazon Web Services Command Line Interface (CLI) or Amazon Web Services SDKs, this field is calculated automatically. |
ExpectedBucketOwner | string | undefined | The account ID of the expected bucket owner. If the account ID that you provide does not match the actual owner of the bucket, the request fails with the HTTP status code |
PutBucketCorsCommand Output
Parameter | Type | Description |
---|
Parameter | Type | Description |
---|---|---|
$metadata Required | ResponseMetadata | Metadata pertaining to this request. |
Throws
Name | Fault | Details |
---|
Name | Fault | Details |
---|---|---|
S3ServiceException | Base exception class for all service exceptions from S3 service. |