python-boto3/docs/source/guide/s3-example-static-web-host.rst

66 lines
1.9 KiB
ReStructuredText
Raw Normal View History

2019-10-20 18:51:09 +02:00
.. Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2018-07-11 07:39:36 +02:00
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0
International License (the "License"). You may not use this file except in compliance with the
License. A copy of the License is located at http://creativecommons.org/licenses/by-nc-sa/4.0/.
This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
either express or implied. See the License for the specific language governing permissions and
limitations under the License.
2019-10-20 18:51:09 +02:00
2018-07-11 07:39:36 +02:00
##############################################
2021-09-22 18:34:33 +02:00
Using an Amazon S3 bucket as a static web host
2018-07-11 07:39:36 +02:00
##############################################
2019-10-20 18:51:09 +02:00
An S3 bucket can be configured to host a static website.
2018-07-11 07:39:36 +02:00
2021-09-22 18:34:33 +02:00
Retrieve a website configuration
2019-10-20 18:51:09 +02:00
================================
2018-07-11 07:39:36 +02:00
2019-10-20 18:51:09 +02:00
Retrieve a bucket's website configuration by calling the AWS SDK for Python
``get_bucket_website`` method.
2018-07-11 07:39:36 +02:00
.. code-block:: python
import boto3
2019-10-20 18:51:09 +02:00
# Retrieve the website configuration
2018-07-11 07:39:36 +02:00
s3 = boto3.client('s3')
2019-10-20 18:51:09 +02:00
result = s3.get_bucket_website(Bucket='BUCKET_NAME')
2018-07-11 07:39:36 +02:00
2021-09-22 18:34:33 +02:00
Set a website configuration
2019-10-20 18:51:09 +02:00
===========================
2018-07-11 07:39:36 +02:00
2019-10-20 18:51:09 +02:00
A bucket's website configuration can be set by calling the
``put_bucket_website`` method.
2018-07-11 07:39:36 +02:00
2019-10-20 18:51:09 +02:00
.. code-block:: python
2018-07-11 07:39:36 +02:00
2019-10-20 18:51:09 +02:00
# Define the website configuration
2018-07-11 07:39:36 +02:00
website_configuration = {
'ErrorDocument': {'Key': 'error.html'},
'IndexDocument': {'Suffix': 'index.html'},
}
2019-10-20 18:51:09 +02:00
# Set the website configuration
s3 = boto3.client('s3')
s3.put_bucket_website(Bucket='BUCKET_NAME',
WebsiteConfiguration=website_configuration)
2018-07-11 07:39:36 +02:00
2021-09-22 18:34:33 +02:00
Delete a website configuration
2019-10-20 18:51:09 +02:00
==============================
2018-07-11 07:39:36 +02:00
2019-10-20 18:51:09 +02:00
A bucket's website configuration can be deleted by calling the
``delete_bucket_website`` method.
2018-07-11 07:39:36 +02:00
2019-10-20 18:51:09 +02:00
.. code-block:: python
2018-07-11 07:39:36 +02:00
2019-10-20 18:51:09 +02:00
# Delete the website configuration
2018-07-11 07:39:36 +02:00
s3 = boto3.client('s3')
2019-10-20 18:51:09 +02:00
s3.delete_bucket_website(Bucket='BUCKET_NAME')