You can configure a bucket for static website hosting and set redirection rules (RoutingRule) for mirroring-based back-to-origin. After you enable static website hosting, requests to the website are equivalent to requests to the bucket. You can also configure automatic redirects to a specified index page and error page. Redirection rules for mirroring-based back-to-origin help you seamlessly migrate data to Object Storage Service (OSS).
Static website hosting
A static website contains only static content, including client-side scripts such as JavaScript. You can host a static website on a bucket and access it through the bucket endpoint.
-
Set up static website hosting
Set up static website hosting:
const OSS = require('ali-oss') const client = new OSS({ // Set region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou. region: 'yourRegion', // Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. accessKeyId: process.env.OSS_ACCESS_KEY_ID, accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET, authorizationV4: true, // Set bucket to the name of your bucket. bucket: 'yourBucketName', }); // Set up static website hosting. async function putBucketWebsite () { try { // Specify the bucket name, for example, examplebucket. const result = await client.putBucketWebsite('examplebucket', { // Set the default homepage for static website hosting. index: 'index.html', // Set the default 404 page for static website hosting. error: 'error.html' }); console.log(result); } catch (e) { console.log(e); } } putBucketWebsite(); -
View the static website hosting configuration
View the static website hosting configuration:
const OSS = require('ali-oss') const client = new OSS({ // Set region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou. region: 'yourRegion', // Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. accessKeyId: process.env.OSS_ACCESS_KEY_ID, accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET, authorizationV4: true, // Set bucket to the name of your bucket. bucket: 'yourBucketName', }); // View the default homepage and default 404 page of the static website hosting configuration. async function getBucketWebsite () { try { // Specify the bucket name, for example, examplebucket. const result = await client.getBucketWebsite('examplebucket'); console.log(result); } catch (e) { console.log(e); } } getBucketWebsite(); -
Delete the static website hosting configuration
Delete the static website hosting configuration:
const OSS = require('ali-oss') const client = new OSS({ // Set region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou. region: 'yourRegion', // Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. accessKeyId: process.env.OSS_ACCESS_KEY_ID, accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET, authorizationV4: true, // Set bucket to the name of your bucket. bucket: 'yourBucketName', }); // Delete the static website hosting configuration. async function deleteBucketWebsite() { try { // Specify the bucket name, for example, examplebucket. 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 mainly used for seamless data migration to OSS. For example, your service might be running on your own origin server or on another cloud product. If you want to migrate the service to OSS for business development, you must ensure that the service continues to run during the migration. During the migration, you can use mirroring-based back-to-origin rules to retrieve data that has not been migrated to OSS. This ensures that your service runs as expected.
-
Set up mirroring-based back-to-origin
When a requested object does not exist in the bucket, you can configure back-to-origin rules to retrieve it from an origin server. For example, if a bucket named examplebucket in the China (Hangzhou) region receives a request for a nonexistent object in the examplefolder directory, you can configure a rule to fetch the object from the examplefolder directory of https://www.example.com/.
Configure mirroring-based back-to-origin rules for this scenario:
const OSS = require('ali-oss') constpath=require("path") const client = new OSS({ // Set region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou. region: 'yourRegion', // Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. accessKeyId: process.env.OSS_ACCESS_KEY_ID, accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET, authorizationV4: true, // Set bucket to the name of your bucket. bucket: 'yourBucketName', }); async function putBucketWebsite() { try { // Specify the bucket name, for example, examplebucket. const result = await client.putBucketWebsite("examplebucket", { // Set the default homepage for static website hosting. index: "index.html", // Set the default 404 page for static website hosting. error: "error.html", // Specify whether to redirect to the default homepage in a subdirectory when a subdirectory is accessed. // supportSubDir:true , // After a default homepage is set, specify the behavior for requests for non-existent objects whose names do not end with a forward slash (/). This parameter is active only if SupportSubDir is true. // type: 0 , routingRules: [ { RuleNumber: 1, // The rule applies only to objects with this prefix. Condition: { KeyPrefixEquals: "examplefolder/" , // Match this rule only if a 404 status code is returned when the specified object is accessed. HttpErrorCodeReturnedEquals: 404 }, // Specify the redirection type. Redirect: { RedirectType: "Mirror", // Specify whether to include request parameters when a redirection or mirroring-based back-to-origin rule is executed. PassQueryString: true, // Specify the origin URL for mirroring-based back-to-origin. MirrorURL: 'http://example.com/', // This has the same effect as PassQueryString but with a higher priority. This parameter is active only if RedirectType is set to Mirror. MirrorPassQueryString:true, // If the back-to-origin request returns a 3xx response, specify whether to follow the redirect to the specified location to fetch data. This parameter is active only if RedirectType is set to Mirror. If set to true, OSS continues to request the address that corresponds to the location. MirrorFollowRedirect:true, // Specify whether to check the MD5 hash of the back-to-origin response body. MirrorCheckMd5:false, // Specify the headers to carry for mirroring-based back-to-origin. // Specify whether to pass through all headers to the origin server. MirrorHeaders:{ PassAll: true, // Pass the headers specified in Pass to the origin server. Pass:'myheader-key1', Pass:'myheader-key2', // Prohibit the headers specified in Remove from being passed through to the origin server. Remove:'myheader-key3', Remove:'myheader-key4'} }} ] }); console.log(result); } catch (e) { console.log(e); } } putBucketWebsite(); -
Retrieve the mirroring-based back-to-origin configuration
Retrieve the mirroring-based back-to-origin configuration:
const OSS = require('ali-oss') constpath=require("path") const client = new OSS({ // Set region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou. region: 'yourRegion', // Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. accessKeyId: process.env.OSS_ACCESS_KEY_ID, accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET, authorizationV4: true, // Set bucket to the name of your bucket. bucket: 'yourBucketName', }); // Get the mirroring-based back-to-origin configuration. async function getBucketWebsite () { try { // Specify the bucket name, for example, examplebucket. const result = await client.getBucketWebsite('examplebucket'); console.log(result); } catch (e) { console.log(e); } } getBucketWebsite(); -
Delete the mirroring-based back-to-origin configuration
Delete the mirroring-based back-to-origin configuration:
const OSS = require('ali-oss') constpath=require("path") const client = new OSS({ // Set region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou. region: 'yourRegion', // Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. accessKeyId: process.env.OSS_ACCESS_KEY_ID, accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET, authorizationV4: true, // Set bucket to the name of your bucket. bucket: 'yourBucketName', }); // Delete the mirroring-based back-to-origin configuration. async function deleteBucketWebsite() { try { // Specify the bucket name, for example, examplebucket. const result = await client.deleteBucketWebsite('examplebucket'); console.log(result); } catch (e) { console.log(e); } } deleteBucketWebsite();
References
-
For more information about the API operation used to configure static website hosting or mirroring-based back-to-origin, see PutBucketWebsite.
-
For more information about the API operation used to retrieve the configuration of static website hosting or mirroring-based back-to-origin, see GetBucketWebsite.
-
For more information about the API operation used to delete the configuration of static website hosting or mirroring-based back-to-origin, see DeleteBucketWebsite.