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. Vous êtes automatiquement redirigé vers la page d'accueil par défaut et la page d'erreur 404 spécifiées.
Notes
Cette rubrique utilise l'endpoint public de la région Chine (Hangzhou). Si vous accédez à OSS depuis d'autres services Alibaba Cloud situés dans la même région, utilisez un endpoint interne. Pour plus d'informations sur les régions et les endpoints OSS, consultez Régions et endpoints.
Cette rubrique explique comment créer une instance OSSClient avec un endpoint OSS. Pour d'autres configurations, comme l'utilisation d'un nom de domaine personnalisé ou l'authentification via des identifiants du service Security Token Service (STS), consultez Créer une instance OssClient.
Pour configurer l'hébergement de site web statique, vous devez disposer de l'autorisation
oss:PutBucketWebsite. Pour récupérer cette configuration, vous devez disposer de l'autorisationoss:GetBucketWebsite. Pour la supprimer, vous devez disposer de l'autorisationoss:DeleteBucketWebsite. Pour plus d'informations, consultez Accorder des politiques d'accès personnalisées à un utilisateur RAM.
Configurer l'hébergement de site web statique
Le code suivant montre comment configurer l'hébergement de site web statique :
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize OSS account information. */
/* Set yourEndpoint to the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the bucket name, for example, examplebucket. */
std::string BucketName = "examplebucket";
/* Initialize network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* Set static website hosting. */
SetBucketWebsiteRequest request(BucketName);
/* Set the default homepage for static website hosting to index.html. */
request.setIndexDocument("index.html");
/* Set the default 404 page for static website hosting to error.html. */
request.setErrorDocument("error.html");
auto outcome = client.SetBucketWebsite(request);
if (!outcome.isSuccess()) {
/* Handle exceptions. */
std::cout << "SetBucketWebsite fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
/* Release network resources. */
ShutdownSdk();
return 0;
}
Consulter la configuration de l'hébergement de site web statique
Le code suivant montre comment consulter la configuration de l'hébergement de site web statique :
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize OSS account information. */
/* Set yourEndpoint to the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the bucket name, for example, examplebucket. */
std::string BucketName = "examplebucket";
/* Initialize network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* View the static website hosting configuration. */
GetBucketWebsiteRequest request(BucketName);
auto outcome = client.GetBucketWebsite(request);
if (outcome.isSuccess()) {
std::cout << "GetBucketWebsite success,IndexDocument: " << outcome.result().IndexDocument() <<
" ,ErrorDocument: " << outcome.result().ErrorDocument() << std::endl;
}
else {
/* Handle exceptions. */
std::cout << "GetBucketWebsite fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
/* Release network resources. */
ShutdownSdk();
return 0;
}
Supprimer la configuration de l'hébergement de site web statique
Le code suivant montre comment supprimer la configuration de l'hébergement de site web statique :
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize OSS account information. */
/* Set yourEndpoint to the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the bucket name, for example, examplebucket. */
std::string BucketName = "examplebucket";
/* Initialize network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* Delete the static website hosting configuration. */
DeleteBucketWebsiteRequest request(BucketName);
auto outcome = client.DeleteBucketWebsite(request);
if (!outcome.isSuccess()) {
/* Handle exceptions. */
std::cout << "DeleteBucketWebsite fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
/* Release network resources. */
ShutdownSdk();
return 0;
}
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 permettant de configurer l'hébergement de site web statique, consultez PutBucketWebsite.
Pour plus d'informations sur l'opération API permettant de consulter la configuration de l'hébergement de site web statique, consultez GetBucketWebsite.
Pour plus d'informations sur l'opération API permettant de supprimer la configuration de l'hébergement de site web statique, consultez DeleteBucketWebsite.