• Object Storage Service

obs
  1. Help Center
  2. Object Storage Service
  3. Developer Guide (PHP SDK)
  4. Static Website Hosting
  5. Setting Website Hosting

Setting Website Hosting

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

Configuring the Default Homepage and Error Pages

Sample code:

// Import the third-party open source libraries.
require 'vendor/autoload.php';
// Import the SDK code library.
require 'obs-autoloader.php';
// Declare the namespace.
use Obs\S3\ObsClient;
// Create an instance of ObsClient.
$obsClient = new ObsClient ( [ 
       'key' => '*** Provide your Access Key ***',
       'secret' => '*** Provide your Secret Key ***',
       'endpoint' => 'https://yourdomainname'
] );

$resp = $obsClient->setBucketWebsiteConfiguration( [ 
       '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 third-party open source libraries.
require 'vendor/autoload.php';
// Import the SDK code library.
require 'obs-autoloader.php';
// Declare the namespace.
use Obs\S3\ObsClient;
// Create an instance of ObsClient.
$obsClient = new ObsClient ( [ 
       'key' => '*** Provide your Access Key ***',
       'secret' => '*** Provide your Secret Key ***',
       'endpoint' => 'https://yourdomainname'
] );

$resp = $obsClient->setBucketWebsiteConfiguration( [ 
       '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'] );
NOTE:

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

Configuring Redirection for All Requests

Sample code:

// Import the third-party open source libraries.
require 'vendor/autoload.php';
// Import the SDK code library.
require 'obs-autoloader.php';
// Declare the namespace.
use Obs\S3\ObsClient;
// Create an instance of ObsClient.
$obsClient = new ObsClient ( [ 
       'key' => '*** Provide your Access Key ***',
       'secret' => '*** Provide your Secret Key ***',
       'endpoint' => 'https://yourdomainname'
] );

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

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