Configurez un bucket pour héberger un site web statique. Une fois la configuration effectuée, les requêtes adressées à l'endpoint du site web diffusent le contenu du bucket. La redirection vers la page d'accueil par défaut et la page 404 par défaut spécifiées s'effectue automatiquement.
Notes
Cette rubrique utilise l'endpoint public de la région Chine (Hangzhou). Pour accéder à OSS depuis d'autres services Alibaba Cloud situés dans la même région, privilégiez un endpoint interne. Pour plus d'informations sur les régions et les endpoints OSS, consultez Régions et endpoints.
Les exemples ci-dessous créent une instance
OSSClientvia un endpoint OSS. Pour d'autres configurations, comme l'utilisation d'un domaine personnalisé ou l'authentification par identifiants temporaires (STS), reportez-vous à la section Initialisation (SDK C# V1).La configuration de l'hébergement de site web statique requiert l'autorisation
oss:PutBucketWebsite. Sa consultation nécessite l'autorisationoss:GetBucketWebsite, et sa suppression l'autorisationoss:DeleteBucketWebsite. Pour plus de détails, consultez la rubrique Accorder des politiques d'accès personnalisées à un utilisateur RAM.
Configurer l'hébergement de site web statique
Le code suivant configure l'hébergement de site web statique :
using Aliyun.OSS;
using Aliyun.OSS.Common;
// Replace yourEndpoint with the Endpoint of the region where the bucket is located. For example, for the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var endpoint = "yourEndpoint";
// 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.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the bucket name.
var bucketName = "examplebucket";
// Specify the region where the bucket is located. For example, for the China (Hangzhou) region, set the region to cn-hangzhou.
const string region = "cn-hangzhou";
// Create a ClientConfiguration instance and modify the default parameters as needed.
var conf = new ClientConfiguration();
// Use Signature V4.
conf.SignatureVersion = SignatureVersion.V4;
// Create an OssClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
try
{
// Set the default homepage to index.html and the default 404 page to error.html for static website hosting.
var request = new SetBucketWebsiteRequest(bucketName, "index.html", "error.html");
client.SetBucketWebsite(request);
Console.WriteLine("Set bucket:{0} Website succeeded ", bucketName);
}
catch (OssException ex)
{
Console.WriteLine("Failed with error code: {0}; Error message: {1}. \nRequestID:{2}\tHostID:{3}",
ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error message: {0}", ex.Message);
}
Consulter la configuration d'hébergement de site web statique
Le code suivant permet de consulter la configuration d'hébergement de site web statique :
using Aliyun.OSS;
using Aliyun.OSS.Common;
// Replace yourEndpoint with the Endpoint of the region where the bucket is located. For example, for the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var endpoint = "yourEndpoint";
// 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.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the bucket name.
var bucketName = "examplebucket";
// Specify the region where the bucket is located. For example, for the China (Hangzhou) region, set the region to cn-hangzhou.
const string region = "cn-hangzhou";
// Create a ClientConfiguration instance and modify the default parameters as needed.
var conf = new ClientConfiguration();
// Use Signature V4.
conf.SignatureVersion = SignatureVersion.V4;
// Create an OssClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
try
{
// View the static website hosting configuration.
var result = client.GetBucketWebsite(bucketName);
Console.WriteLine("Get bucket:{0} Website succeeded, index doc:{1}, error doc:{2}",
bucketName, result.IndexDocument, result.ErrorDocument);
}
catch (OssException ex)
{
Console.WriteLine("Failed with error code: {0}; Error message: {1}. \nRequestID:{2}\tHostID:{3}",
ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error message: {0}", ex.Message);
}
Supprimer la configuration d'hébergement de site web statique
Le code suivant supprime la configuration d'hébergement de site web statique :
using Aliyun.OSS;
using Aliyun.OSS.Common;
// Replace yourEndpoint with the Endpoint of the region where the bucket is located. For example, for the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var endpoint = "yourEndpoint";
// 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.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the bucket name.
var bucketName = "examplebucket";
// Specify the region where the bucket is located. For example, for the China (Hangzhou) region, set the region to cn-hangzhou.
const string region = "cn-hangzhou";
// Create a ClientConfiguration instance and modify the default parameters as needed.
var conf = new ClientConfiguration();
// Use Signature V4.
conf.SignatureVersion = SignatureVersion.V4;
// Create an OssClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
try
{
// Delete the static website hosting configuration.
client.DeleteBucketWebsite(bucketName);
Console.WriteLine("Delete bucket:{0} Website succeeded ", bucketName);
}
catch (OssException ex)
{
Console.WriteLine("Failed with error code: {0}; Error message: {1}. \nRequestID:{2}\tHostID:{3}",
ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error message: {0}", ex.Message);
}
Références
Pour l'exemple de code complet relatif à l'hébergement de site web statique, consultez l'exemple GitHub.
Pour plus d'informations sur l'opération API de configuration de l'hébergement de site web statique, consultez PutBucketWebsite.
Pour plus d'informations sur l'opération API de consultation de la configuration d'hébergement de site web statique, consultez GetBucketWebsite.
Pour plus d'informations sur l'opération API de suppression de la configuration d'hébergement de site web statique, consultez DeleteBucketWebsite.