与 AWS SDK或DeleteBucketWebsite一起使用 CLI - AWS SDK代码示例

AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

与 AWS SDK或DeleteBucketWebsite一起使用 CLI

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

C++
SDK对于 C++
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

bool AwsDoc::S3::deleteBucketWebsite(const Aws::String &bucketName, const Aws::S3::S3ClientConfiguration &clientConfig) { Aws::S3::S3Client client(clientConfig); Aws::S3::Model::DeleteBucketWebsiteRequest request; request.SetBucket(bucketName); Aws::S3::Model::DeleteBucketWebsiteOutcome outcome = client.DeleteBucketWebsite(request); if (!outcome.IsSuccess()) { auto err = outcome.GetError(); std::cerr << "Error: deleteBucketWebsite: " << err.GetExceptionName() << ": " << err.GetMessage() << std::endl; } else { std::cout << "Website configuration was removed." << std::endl; } return outcome.IsSuccess(); }
  • 有关API详细信息,请参阅 “AWS SDK for C++ API参考 DeleteBucketWebsite” 中的。

CLI
AWS CLI

以下命令从名为 my-bucket 的存储桶中删除网站配置:

aws s3api delete-bucket-website --bucket my-bucket
Java
SDK适用于 Java 2.x
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.DeleteBucketWebsiteRequest; import software.amazon.awssdk.services.s3.model.S3Exception; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * <p> * For more information, see the following documentation topic: * <p> * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class DeleteWebsiteConfiguration { public static void main(String[] args) { final String usage = """ Usage: <bucketName> Where: bucketName - The Amazon S3 bucket to delete the website configuration from. """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String bucketName = args[0]; System.out.format("Deleting website configuration for Amazon S3 bucket: %s\n", bucketName); Region region = Region.US_EAST_1; S3Client s3 = S3Client.builder() .region(region) .build(); deleteBucketWebsiteConfig(s3, bucketName); System.out.println("Done!"); s3.close(); } /** * Deletes the website configuration for an Amazon S3 bucket. * * @param s3 The {@link S3Client} instance used to interact with Amazon S3. * @param bucketName The name of the S3 bucket for which the website configuration should be deleted. * @throws S3Exception If an error occurs while deleting the website configuration. */ public static void deleteBucketWebsiteConfig(S3Client s3, String bucketName) { DeleteBucketWebsiteRequest delReq = DeleteBucketWebsiteRequest.builder() .bucket(bucketName) .build(); try { s3.deleteBucketWebsite(delReq); } catch (S3Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.out.println("Failed to delete website configuration!"); System.exit(1); } } }
  • 有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 DeleteBucketWebsite” 中的。

JavaScript
SDK对于 JavaScript (v3)
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

从存储桶中删除网站配置。

import { DeleteBucketWebsiteCommand, S3Client, S3ServiceException, } from "@aws-sdk/client-s3"; /** * Remove the website configuration for a bucket. * @param {{ bucketName: string }} */ export const main = async ({ bucketName }) => { const client = new S3Client({}); try { await client.send( new DeleteBucketWebsiteCommand({ Bucket: bucketName, }), ); // The response code will be successful for both removed configurations and // configurations that did not exist in the first place. console.log( `The bucket "${bucketName}" is not longer configured as a website, or it never was.`, ); } catch (caught) { if ( caught instanceof S3ServiceException && caught.name === "NoSuchBucket" ) { console.error( `Error from S3 while removing website configuration from ${bucketName}. The bucket doesn't exist.`, ); } else if (caught instanceof S3ServiceException) { console.error( `Error from S3 while removing website configuration from ${bucketName}. ${caught.name}: ${caught.message}`, ); } else { throw caught; } } };
PowerShell
用于 PowerShell

示例 1:此命令禁用给定 S3 存储桶的静态网站托管属性。

Remove-S3BucketWebsite -BucketName 'amzn-s3-demo-bucket'

输出:

Confirm Are you sure you want to perform this action? Performing the operation "Remove-S3BucketWebsite (DeleteBucketWebsite)" on target "s3testbucket". [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): Y
  • 有关API详细信息,请参阅 AWS Tools for PowerShell Cmdlet 参考DeleteBucketWebsite中的。