Configure a bucket to host a static website. Once configured, requests to the website endpoint serve content from the bucket and automatically redirect to the specified default homepage and default 404 page.
This page covers:
Configure static website hosting using PutBucketWebsite
View the static website hosting configuration using GetBucketWebsite
Delete the static website hosting configuration using DeleteBucketWebsite
Prerequisites
Before you begin, ensure that you have:
The
oss:PutBucketWebsitepermission to configure static website hostingThe
oss:GetBucketWebsitepermission to view the configurationThe
oss:DeleteBucketWebsitepermission to delete the configuration
For more information about RAM (Resource Access Management) policies, see Common examples of RAM policies.
Note: Before using the SDK for static website configurations, call the PutBucketWebsite API operation to set up the static website. For more information, see PutBucketWebsite.
Usage notes
The examples in this topic use the public endpoint for the China (Hangzhou) region. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For supported regions and endpoints, see Regions and endpoints.
All examples create an OSSClient instance using an OSS endpoint. To create an OSSClient instance using custom domain names or Security Token Service (STS), see Create an OSSClient instance.
All examples use
EnvironmentVariableCredentialsProviderto load credentials from environment variables. SetOSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRETbefore running the code.
Configure static website hosting
Imports
<?php
if (is_file(__DIR__ . '/../autoload.php')) {
require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
require_once __DIR__ . '/../vendor/autoload.php';
}
use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\CoreOssException;
use OSS\Model\WebsiteConfig;Sample code
$provider = new EnvironmentVariableCredentialsProvider();
// Specify the endpoint. This example uses the China (Hangzhou) region.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the bucket name.
$bucket = "examplebucket";
// Set the default homepage to index.html and the default 404 page to error.html.
$websiteConfig = new WebsiteConfig("index.html", "error.html");
try {
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
"region" => "cn-hangzhou"
);
$ossClient = new OssClient($config);
$ossClient->putBucketWebsite($bucket, $websiteConfig);
echo "Static website hosting configured successfully for bucket: " . $bucket . "\n";
} catch (OssException $e) {
// Output the error message if the operation fails.
echo $e->getMessage() . "\n";
}View the static website hosting configuration
Imports
<?php
if (is_file(__DIR__ . '/../autoload.php')) {
require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
require_once __DIR__ . '/../vendor/autoload.php';
}
use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\CoreOssException;Sample code
$provider = new EnvironmentVariableCredentialsProvider();
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
$bucket = "examplebucket";
try {
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
"region" => "cn-hangzhou"
);
$ossClient = new OssClient($config);
$websiteConfig = $ossClient->getBucketWebsite($bucket);
echo "Retrieved website configuration for bucket: " . $bucket . "\n";
echo $websiteConfig->serializeToXml() . "\n";
} catch (OssException $e) {
// Output the error message if the operation fails.
echo $e->getMessage() . "\n";
}Delete the static website hosting configuration
Imports
<?php
if (is_file(__DIR__ . '/../autoload.php')) {
require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
require_once __DIR__ . '/../vendor/autoload.php';
}
use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\CoreOssException;Sample code
$provider = new EnvironmentVariableCredentialsProvider();
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
$bucket = "examplebucket";
try {
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
"region" => "cn-hangzhou"
);
$ossClient = new OssClient($config);
$ossClient->deleteBucketWebsite($bucket);
echo "Static website hosting configuration deleted for bucket: " . $bucket . "\n";
} catch (OssException $e) {
// Output the error message if the operation fails.
echo $e->getMessage() . "\n";
}References
For the complete sample code, see GitHub examples.
For the PutBucketWebsite API reference, see PutBucketWebsite.
For the GetBucketWebsite API reference, see GetBucketWebsite.
For the DeleteBucketWebsite API reference, see DeleteBucketWebsite.