You can enable static website hosting for a bucket and configure redirection rules (RoutingRule) for mirroring-based back-to-origin. After static website hosting is configured, you can access the website hosted on the bucket, and you are automatically redirected to the specified index page or error page. After the redirection rules for mirroring-based back-to-origin take effect, you can use this feature to seamlessly migrate data to OSS.
Static website hosting
A static website consists of webpages with static content. This includes client-side scripts, such as JavaScript. You can host your static website on a bucket and use the bucket's endpoint to access the site.
Set up static website hosting
The following code shows how to 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
The following code shows how to 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
The following code shows how to 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 primarily used for seamless data migration to OSS. For example, a service is running on a self-managed origin server or on other cloud products. Because of business growth, you may need to migrate the service to OSS without service interruptions. During the migration, you can use mirroring-based back-to-origin rules to retrieve data that is not yet migrated to OSS to ensure business continuity.
Set up mirroring-based back-to-origin
For example, if a requester tries to access a file that does not exist in the destination bucket, you can specify back-to-origin conditions and an origin URL. OSS then retrieves the object file from the origin server. For example, you have a bucket named examplebucket in the China (Hangzhou) region. If a requester tries to access a file that does not exist in the examplefolder directory of the bucket, you can configure a rule to retrieve the file from the examplefolder directory of the https://www.example.com/ site.
The following code shows how to configure mirroring-based back-to-origin rules for the preceding 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
The following code shows how to 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
The following code shows how to 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 to configure static website hosting or mirroring-based back-to-origin, see PutBucketWebsite.
For more information about the API operation to query static website hosting or mirroring-based back-to-origin configurations, see GetBucketWebsite.
For more information about the API operation to delete static website hosting or mirroring-based back-to-origin configurations, see DeleteBucketWebsite.