Object Storage Service (OSS) lets you host a static website directly from a bucket. After you configure static website hosting, visitors access your site through the bucket endpoint. You can set a default homepage and a default error page for the hosted site. OSS also supports mirroring-based back-to-origin, which retrieves objects from an origin server when they are not found in the bucket, enabling seamless data migration to OSS without service interruptions.
Prerequisites
Node.js 8.0 or later is installed.
The
ali-ossSDK is installed. Run the following command to install it:npm install ali-ossThe
OSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRETenvironment variables are configured with your AccessKey ID and AccessKey secret. For more information about how to obtain an AccessKey pair, see Create an AccessKey pair.
Static website hosting
A static website consists of webpages with static content, including client-side scripts such as JavaScript. You can host a static website on a bucket and use the bucket endpoint to access the site.
Set up static website hosting
Call putBucketWebsite to configure the default homepage and default error page for a bucket.
const OSS = require('ali-oss');
const client = new OSS({
// Specify the region. Example: oss-cn-hangzhou.
region: 'yourRegion',
// Obtain the AccessKey ID and AccessKey secret from environment variables.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the bucket name.
bucket: 'yourBucketName',
});
async function putBucketWebsite() {
try {
const result = await client.putBucketWebsite('examplebucket', {
// Specify the default homepage.
index: 'index.html',
// Specify the default error page.
error: 'error.html',
});
console.log(result);
} catch (e) {
console.log(e);
}
}
putBucketWebsite();View the static website hosting configuration
Call getBucketWebsite to query the default homepage and default error page configured for a bucket.
const OSS = require('ali-oss');
const client = new OSS({
// Specify the region. Example: oss-cn-hangzhou.
region: 'yourRegion',
// Obtain the AccessKey ID and AccessKey secret from environment variables.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the bucket name.
bucket: 'yourBucketName',
});
async function getBucketWebsite() {
try {
const result = await client.getBucketWebsite('examplebucket');
console.log(result);
} catch (e) {
console.log(e);
}
}
getBucketWebsite();Delete the static website hosting configuration
Call deleteBucketWebsite to remove the static website hosting configuration from a bucket.
const OSS = require('ali-oss');
const client = new OSS({
// Specify the region. Example: oss-cn-hangzhou.
region: 'yourRegion',
// Obtain the AccessKey ID and AccessKey secret from environment variables.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the bucket name.
bucket: 'yourBucketName',
});
async function deleteBucketWebsite() {
try {
const result = await client.deleteBucketWebsite('examplebucket');
console.log(result);
} catch (e) {
console.log(e);
}
}
deleteBucketWebsite();Mirroring-based back-to-origin
Mirroring-based back-to-origin is primarily used for seamless data migration to OSS. For example, a service runs on a self-managed origin server or on another cloud product. You need to migrate the service to OSS without service interruptions. During the migration, mirroring-based back-to-origin rules retrieve data that is not yet migrated to OSS to ensure business continuity.
Set up mirroring-based back-to-origin
When a requester accesses an object that does not exist in the bucket, configure back-to-origin conditions and an origin URL so that OSS retrieves the object from the origin server.
The following example configures a mirroring-based back-to-origin rule for a bucket named examplebucket in the China (Hangzhou) region. If a requested object does not exist in the examplefolder/ directory of the bucket, OSS retrieves the object from the examplefolder/ directory of https://www.example.com/.
const OSS = require('ali-oss');
const client = new OSS({
// Specify the region. Example: oss-cn-hangzhou.
region: 'yourRegion',
// Obtain the AccessKey ID and AccessKey secret from environment variables.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the bucket name.
bucket: 'yourBucketName',
});
async function putBucketWebsite() {
try {
const result = await client.putBucketWebsite('examplebucket', {
// Specify the default homepage.
index: 'index.html',
// Specify the default error page.
error: 'error.html',
// Specify whether to redirect to the default homepage in a subdirectory when a subdirectory is accessed.
// supportSubDir: true,
// Specify the behavior for requests for non-existent objects whose names do not end with a forward slash (/). Active only when supportSubDir is true.
// type: 0,
routingRules: [
{
RuleNumber: 1,
Condition: {
// Apply this rule only to objects with this prefix.
KeyPrefixEquals: 'examplefolder/',
// Trigger this rule when a 404 status code is returned.
HttpErrorCodeReturnedEquals: 404,
},
Redirect: {
// Set the redirect type to Mirror for mirroring-based back-to-origin.
RedirectType: 'Mirror',
// Include request parameters during redirection.
PassQueryString: true,
// Specify the origin URL for mirroring-based back-to-origin.
MirrorURL: 'http://example.com/',
// Same as PassQueryString but takes higher priority. Active only when RedirectType is Mirror.
MirrorPassQueryString: true,
// Follow 3xx redirects from the origin server. Active only when RedirectType is Mirror.
MirrorFollowRedirect: true,
// Specify whether to check the MD5 hash of the back-to-origin response body.
MirrorCheckMd5: false,
MirrorHeaders: {
// Pass all headers to the origin server.
PassAll: true,
// Specify headers to pass to the origin server.
Pass: ['myheader-key1', 'myheader-key2'],
// Specify headers to exclude from passing to the origin server.
Remove: ['myheader-key3', 'myheader-key4'],
},
},
},
],
});
console.log(result);
} catch (e) {
console.log(e);
}
}
putBucketWebsite();The following table describes the routing rule parameters.
Parameter | Type | Description |
|
|
Number |
Sequence number of the rule. |
|
|
String |
Object name prefix that triggers the rule. |
|
|
Number |
HTTP status code that triggers the rule. Example: |
|
|
String |
Redirect type. Set to |
|
|
Boolean |
Specifies whether to include request query parameters during redirection. |
|
|
String |
Origin URL for mirroring-based back-to-origin. |
|
|
Boolean |
Same effect as |
|
|
Boolean |
Specifies whether to follow 3xx redirects from the origin server. Active only when |
|
|
Boolean |
Specifies whether to check the MD5 hash of the back-to-origin response body. |
|
|
Boolean |
Specifies whether to pass all request headers to the origin server. |
|
|
Array |
Headers to pass to the origin server. |
|
|
Array |
Headers to exclude from passing to the origin server. |
Retrieve the mirroring-based back-to-origin configuration
Call getBucketWebsite to query the mirroring-based back-to-origin configuration of a bucket.
const OSS = require('ali-oss');
const client = new OSS({
// Specify the region. Example: oss-cn-hangzhou.
region: 'yourRegion',
// Obtain the AccessKey ID and AccessKey secret from environment variables.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the bucket name.
bucket: 'yourBucketName',
});
async function getBucketWebsite() {
try {
const result = await client.getBucketWebsite('examplebucket');
console.log(result);
} catch (e) {
console.log(e);
}
}
getBucketWebsite();Delete the mirroring-based back-to-origin configuration
Call deleteBucketWebsite to remove the mirroring-based back-to-origin configuration from a bucket.
const OSS = require('ali-oss');
const client = new OSS({
// Specify the region. Example: oss-cn-hangzhou.
region: 'yourRegion',
// Obtain the AccessKey ID and AccessKey secret from environment variables.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the bucket name.
bucket: 'yourBucketName',
});
async function deleteBucketWebsite() {
try {
const result = await client.deleteBucketWebsite('examplebucket');
console.log(result);
} catch (e) {
console.log(e);
}
}
deleteBucketWebsite();References
PutBucketWebsite: Configure static website hosting or mirroring-based back-to-origin.
GetBucketWebsite: Query static website hosting or mirroring-based back-to-origin configurations.
DeleteBucketWebsite: Delete static website hosting or mirroring-based back-to-origin configurations.