How to Apply CORS Policies to Vultr Object Storage Subscription Buckets

Updated on November 27, 2024

Vultr Object Storage subscription allows users to configure Cross-Origin Resource Sharing (CORS) policies to control which external origins can access resources within their buckets. By setting appropriate CORS rules, users can specify which domains are permitted to interact with their stored data, along with allowed HTTP methods and headers. This feature is essential for web applications that require secure access to assets from multiple domains while ensuring data protection. Configuring CORS policies helps manage resource access while maintaining security standards.

Follow this guide to apply CORS policies to Vultr Object Storage subscription buckets using s3cmd and AWS CLI.

  • S3cmd
  • AWS CLI
  1. Deploy Vultr Object Storage subscription and create a bucket.

  2. Configure s3cmd with Vultr Object Storage subscription.

    console
    $ s3cmd --configure
    

    Follow the prompts and provide bucket credentials like Access Key, Secret Key, Default Region and S3 Endpoint. You can retrieve these credentials from the Vultr Object Storage subscription Overview page.

  3. Enter the DNS-style template. For example, if you choose the New Jersey location, use %(bucket)s.ewr1.vultrobjects.com.

    DNS-style bucket+hostname:port template for accessing a 
    bucket [%(bucket)s.s3.amazonaws.com]: %(bucket)s.ewr1.vultrobjects.com
  4. Upload a file in the bucket.

    console
    $ s3cmd put /<local-file-location> s3://<your-bucket-name>/
    
  5. Create a new xml file, such as cors_rules.xml to set up a CORS bucket policy.

    console
    $ nano cors_rules.xml
    
  6. Copy the content below and paste it into the xml file.

    xml
    <CORSConfiguration>
    <CORSRule>
        <AllowedOrigin>*</AllowedOrigin>
        <AllowedMethod>GET</AllowedMethod>
        <AllowedMethod>POST</AllowedMethod>
        <AllowedMethod>PUT</AllowedMethod>
        <MaxAgeSeconds>3000</MaxAgeSeconds>
        <AllowedHeader>*</AllowedHeader>
    </CORSRule>
    </CORSConfiguration>
    

    Save and close the file.

  7. Apply the CORS policy.

    console
    $ s3cmd setcors cors_rules.xml s3:///<your-bucket-name>
    
  8. Verify the CORS policy if needed.

    console
    $ s3cmd getcors s3://<your-bucket-name>
    
  1. Deploy Vultr Object Storage and create a bucket.

  2. Configure AWS CLI with Vultr Object Storage subscription.

    console
    $ aws configure --profile my-config
    

    Follow the prompts and provide bucket credentials like Access Key, Secret Key, and Default Region. These credentials can be retrieved from the overview page of the Vultr Object Storage subscription.

  3. Upload a file in the bucket.

    console
    $ aws --endpoint-url https://<your-hostname> --profile my-config s3api put-object --bucket <your-bucket-name> --key <remote-filename> --body <local-file-location>
    
  4. Create a new json file to set up a CORS bucket policy.

    console
    $ nano cors_rules.json
    
  5. Copy and paste the below content into the file.

    json
    [
    {
        "AllowedOrigins": ["*"],
        "AllowedMethods": ["GET", "POST", "PUT"],
        "AllowedHeaders": ["*"],
        "MaxAgeSeconds": 3000
    }
    ]
    

    Save and close the file.

  6. Set the policy for the bucket.

    console
    $ aws --endpoint-url https://<your-hostname> s3api put-bucket-cors --bucket <your-bucket-name> --cors-configuration file://cors_rules.json --profile my-config
    
  7. Check the policy applied to the bucket if needed.

    console
    $ aws --endpoint-url https://<your-hostname> s3api get-bucket-cors --bucket <your-bucket-name> --profile my-config