Updated on 2023-11-09 GMT+08:00

Setting Website Hosting

If you have any questions during development, post them on the Issues page of GitHub. For details about parameters and usage of each API, see API Reference.

You can call ObsClient->setBucketWebsite to set website hosting for a bucket.

Configuring the Default Homepage and Error Pages

Sample code:

// Import the dependency library.
require 'vendor/autoload.php';
// Import the SDK code library during source code installation.
// require 'obs-autoloader.php';
// Declare the namespace.
use Obs\ObsClient;
// Create an instance of ObsClient.
$obsClient = new ObsClient ( [ 
      //Obtain an AK/SK pair using environment variables or import the AK/SK pair in other ways. Using hard coding may result in leakage.
      //Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.      'key' => getenv('ACCESS_KEY_ID'),
      'secret' => getenv('SECRET_ACCESS_KEY'),
      'endpoint' => 'https://your-endpoint'
] );

$resp = $obsClient->setBucketWebsite( [ 
       'Bucket' => 'bucketname',
       // Configure the default homepage.
       'IndexDocument' => ['Suffix' => 'index.html'],       
       // Configure the error pages.
       'ErrorDocument' => ['Key' => 'error.html']
] );
printf ( "RequestId:%s\n", $resp ['RequestId'] );

Configuring Redirection Rules

Sample code:

// Import the dependency library.
require 'vendor/autoload.php';
// Import the SDK code library during source code installation.
// require 'obs-autoloader.php';
// Declare the namespace.
use Obs\ObsClient;
// Create an instance of ObsClient.
$obsClient = new ObsClient ( [ 
      //Obtain an AK/SK pair using environment variables or import the AK/SK pair in other ways. Using hard coding may result in leakage.
      //Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.      'key' => getenv('ACCESS_KEY_ID'),
      'secret' => getenv('SECRET_ACCESS_KEY'),
      'endpoint' => 'https://your-endpoint'
] );

$resp = $obsClient->setBucketWebsite( [ 
       'Bucket' => 'bucketname',
       // Configure the default homepage.
       'IndexDocument' => ['Suffix' => 'index.html'],       
       // Configure the error pages.
       'ErrorDocument' => ['Key' => 'error.html'],
       // Set redirection rules.
       'RoutingRules' => [                                    
                     ['Condition' => ['HttpErrorCodeReturnedEquals' => 404], 'Redirect' => ['Protocol' => 'http', 'ReplaceKeyWith' => 'NotFound.html']],                     
                     ['Condition' => ['HttpErrorCodeReturnedEquals' => 404], 'Redirect' => ['Protocol' => 'https', 'ReplaceKeyWith' => 'test.html']]
       ]
] );
printf ( "RequestId:%s\n", $resp ['RequestId'] );

Use the RoutingRules parameter to specify redirection rules for a bucket.

Configuring Redirection for All Requests

Sample code:

// Import the dependency library.
require 'vendor/autoload.php';
// Import the SDK code library during source code installation.
// require 'obs-autoloader.php';
// Declare the namespace.
use Obs\ObsClient;
// Create an instance of ObsClient.
$obsClient = new ObsClient ( [ 
      //Obtain an AK/SK pair using environment variables or import the AK/SK pair in other ways. Using hard coding may result in leakage.
      //Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.      'key' => getenv('ACCESS_KEY_ID'),
      'secret' => getenv('SECRET_ACCESS_KEY'),
      'endpoint' => 'https://your-endpoint'
] );

$resp = $obsClient->setBucketWebsite( [ 
       'Bucket' => 'bucketname',
       'RedirectAllRequestsTo' => ['HostName' => 'www.example.com', 'Protocol' => 'http']
] );
printf ( "RequestId:%s\n", $resp ['RequestId'] );

Use the RedirectAllRequestsTo parameter to set redirection rules for all requests for accessing a bucket.