Vous pouvez configurer un bucket pour l'hébergement de site web statique et définir des règles de redirection (RoutingRule) pour la récupération par miroir. Une fois l'hébergement activé, les requêtes adressées au site web équivalent à des requêtes vers le bucket. Vous pouvez également configurer des redirections automatiques vers une page d'index et une page d'erreur spécifiques. Les règles de redirection pour la récupération par miroir facilitent la migration transparente de vos données vers Object Storage Service (OSS).
Hébergement de site web statique
Un site web statique ne contient que du contenu statique, y compris des scripts côté client tels que JavaScript. Vous pouvez héberger un site web statique sur un bucket et y accéder via l'endpoint du bucket.
-
Configurer l'hébergement de site web statique
Configurez l'hébergement de site web statique :
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(); -
Consulter la configuration de l'hébergement de site web statique
Consultez la configuration de l'hébergement de site web statique :
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(); -
Supprimer la configuration de l'hébergement de site web statique
Supprimez la configuration de l'hébergement de site web statique :
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();
Récupération par miroir
La récupération par miroir sert principalement à migrer des données vers OSS en toute transparence. Par exemple, votre service peut s'exécuter sur votre propre serveur d'origine ou sur un autre produit cloud. Si vous souhaitez migrer le service vers OSS pour développer votre activité, vous devez garantir sa continuité pendant la migration. Durant cette phase, utilisez des règles de récupération par miroir pour obtenir les données qui n'ont pas encore été migrées vers OSS. Cela garantit le bon fonctionnement de votre service.
-
Configurer la récupération par miroir
Lorsqu'un objet demandé n'existe pas dans le bucket, vous pouvez configurer des règles de récupération pour l'obtenir depuis un serveur d'origine. Par exemple, si un bucket nommé examplebucket dans la région Chine (Hangzhou) reçoit une requête pour un objet inexistant dans le répertoire examplefolder, vous pouvez configurer une règle pour récupérer l'objet depuis le répertoire examplefolder de https://www.example.com/.
Configurez les règles de récupération par miroir pour ce scénario :
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(); -
Récupérer la configuration de la récupération par miroir
Récupérez la configuration de la récupération par miroir :
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(); -
Supprimer la configuration de la récupération par miroir
Supprimez la configuration de la récupération par miroir :
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();
Références
Pour plus d'informations sur l'opération API utilisée pour configurer l'hébergement de site web statique ou la récupération par miroir, consultez PutBucketWebsite.
Pour plus d'informations sur l'opération API utilisée pour récupérer la configuration de l'hébergement de site web statique ou de la récupération par miroir, consultez GetBucketWebsite.
Pour plus d'informations sur l'opération API utilisée pour supprimer la configuration de l'hébergement de site web statique ou de la récupération par miroir, consultez DeleteBucketWebsite.