View a markdown version of this page

将 GetBucketWebsite 与 Amazon SDK 或 CLI 配合使用 - Amazon Simple Storage Service
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

GetBucketWebsite 与 Amazon SDK 或 CLI 配合使用

以下代码示例演示如何使用 GetBucketWebsite

.NET
Amazon SDK for .NET
注意

查看 GitHub,了解更多信息。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

// Get the website configuration. GetBucketWebsiteRequest getRequest = new GetBucketWebsiteRequest() { BucketName = bucketName, }; GetBucketWebsiteResponse getResponse = await client.GetBucketWebsiteAsync(getRequest); Console.WriteLine($"Index document: {getResponse.WebsiteConfiguration.IndexDocumentSuffix}"); Console.WriteLine($"Error document: {getResponse.WebsiteConfiguration.ErrorDocument}");
  • 有关更多信息,请参阅《Amazon SDK for .NET API Reference》中的 GetBucketWebsite

C++
SDK for C++
注意

查看 GitHub,了解更多信息。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

bool AwsDoc::S3::getWebsiteConfig(const Aws::String &bucketName, const Aws::S3::S3ClientConfiguration &clientConfig) { Aws::S3::S3Client s3Client(clientConfig); Aws::S3::Model::GetBucketWebsiteRequest request; request.SetBucket(bucketName); Aws::S3::Model::GetBucketWebsiteOutcome outcome = s3Client.GetBucketWebsite(request); if (!outcome.IsSuccess()) { const Aws::S3::S3Error &err = outcome.GetError(); std::cerr << "Error: GetBucketWebsite: " << err.GetMessage() << std::endl; } else { Aws::S3::Model::GetBucketWebsiteResult websiteResult = outcome.GetResult(); std::cout << "Success: GetBucketWebsite: " << std::endl << std::endl << "For bucket '" << bucketName << "':" << std::endl << "Index page : " << websiteResult.GetIndexDocument().GetSuffix() << std::endl << "Error page: " << websiteResult.GetErrorDocument().GetKey() << std::endl; } return outcome.IsSuccess(); }
  • 有关更多信息,请参阅《Amazon SDK for C++ API Reference》中的 GetBucketWebsite

CLI
Amazon CLI

以下命令检索名为 amzn-s3-demo-bucket 的存储桶的静态网站配置:

aws s3api get-bucket-website --bucket amzn-s3-demo-bucket

输出:

{ "IndexDocument": { "Suffix": "index.html" }, "ErrorDocument": { "Key": "error.html" } }
  • 有关 API 详细信息,请参阅《Amazon CLI 命令参考》中的 GetBucketWebsite

JavaScript
SDK for JavaScript (v3)
注意

查看 GitHub,了解更多信息。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

获取网站配置。

import { GetBucketWebsiteCommand, S3Client, S3ServiceException, } from "@aws-sdk/client-s3"; /** * Log the website configuration for a bucket. * @param {{ bucketName }} */ export const main = async ({ bucketName }) => { const client = new S3Client({}); try { const response = await client.send( new GetBucketWebsiteCommand({ Bucket: bucketName, }), ); console.log( `Your bucket is set up to host a website with the following configuration:\n${JSON.stringify(response, null, 2)}`, ); } catch (caught) { if ( caught instanceof S3ServiceException && caught.name === "NoSuchWebsiteConfiguration" ) { console.error( `Error from S3 while getting website configuration for ${bucketName}. The bucket isn't configured as a website.`, ); } else if (caught instanceof S3ServiceException) { console.error( `Error from S3 while getting website configuration for ${bucketName}. ${caught.name}: ${caught.message}`, ); } else { throw caught; } } };
  • 有关更多信息,请参阅《Amazon SDK for JavaScript API Reference》中的 GetBucketWebsite

PowerShell
适用于 PowerShell V4 的工具

示例 1:此命令返回给定 S3 存储桶的静态网站配置的详细信息。

Get-S3BucketWebsite -BucketName 'amzn-s3-demo-bucket'
  • 有关 API 详细信息,请参阅《Amazon Tools for PowerShell Cmdlet Reference (V4)》中的 GetBucketWebsite

适用于 PowerShell V5 的工具

示例 1:此命令返回给定 S3 存储桶的静态网站配置的详细信息。

Get-S3BucketWebsite -BucketName 'amzn-s3-demo-bucket'
  • 有关 API 详细信息,请参阅《Amazon Tools for PowerShell Cmdlet Reference (V5)》中的 GetBucketWebsite

有关 Amazon SDK 开发人员指南和代码示例的完整列表,请参阅 使用 Amazon SDK 通过 Amazon S3 进行开发。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。