DeleteBucketPolicy 搭配 AWS SDK或 使用 CLI - AWS SDK 程式碼範例

文件範例儲存庫中有更多 AWS SDK可用的範例。 AWS SDK GitHub

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

DeleteBucketPolicy 搭配 AWS SDK或 使用 CLI

下列程式碼範例示範如何使用 DeleteBucketPolicy

C++
SDK 適用於 C++
注意

還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

bool AwsDoc::S3::deleteBucketPolicy(const Aws::String &bucketName, const Aws::S3::S3ClientConfiguration &clientConfig) { Aws::S3::S3Client client(clientConfig); Aws::S3::Model::DeleteBucketPolicyRequest request; request.SetBucket(bucketName); Aws::S3::Model::DeleteBucketPolicyOutcome outcome = client.DeleteBucketPolicy(request); if (!outcome.IsSuccess()) { const Aws::S3::S3Error &err = outcome.GetError(); std::cerr << "Error: deleteBucketPolicy: " << err.GetExceptionName() << ": " << err.GetMessage() << std::endl; } else { std::cout << "Policy was deleted from the bucket." << std::endl; } return outcome.IsSuccess(); }
  • 如需API詳細資訊,請參閱 參考 DeleteBucketPolicy中的 。 AWS SDK for C++ API

CLI
AWS CLI

下列命令會從名為 的儲存貯體中刪除儲存貯體政策my-bucket

aws s3api delete-bucket-policy --bucket my-bucket
Java
SDK 適用於 Java 2.x
注意

還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

import software.amazon.awssdk.services.s3.model.S3Exception; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.DeleteBucketPolicyRequest; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class DeleteBucketPolicy { public static void main(String[] args) { final String usage = """ Usage: <bucketName> Where: bucketName - The Amazon S3 bucket to delete the policy from (for example, bucket1)."""; if (args.length != 1) { System.out.println(usage); System.exit(1); } String bucketName = args[0]; System.out.format("Deleting policy from bucket: \"%s\"\n\n", bucketName); Region region = Region.US_EAST_1; S3Client s3 = S3Client.builder() .region(region) .build(); deleteS3BucketPolicy(s3, bucketName); s3.close(); } /** * Deletes the S3 bucket policy for the specified bucket. * * @param s3 the {@link S3Client} instance to use for the operation * @param bucketName the name of the S3 bucket for which the policy should be deleted * * @throws S3Exception if there is an error deleting the bucket policy */ public static void deleteS3BucketPolicy(S3Client s3, String bucketName) { DeleteBucketPolicyRequest delReq = DeleteBucketPolicyRequest.builder() .bucket(bucketName) .build(); try { s3.deleteBucketPolicy(delReq); System.out.println("Done!"); } catch (S3Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • 如需API詳細資訊,請參閱 參考 DeleteBucketPolicy中的 。 AWS SDK for Java 2.x API

JavaScript
SDK 適用於 JavaScript (v3)
注意

還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

刪除儲存貯體政策。

import { DeleteBucketPolicyCommand, S3Client, S3ServiceException, } from "@aws-sdk/client-s3"; /** * Remove the policy from an Amazon S3 bucket. * @param {{ bucketName: string }} */ export const main = async ({ bucketName }) => { const client = new S3Client({}); try { await client.send( new DeleteBucketPolicyCommand({ Bucket: bucketName, }), ); console.log(`Bucket policy deleted from "${bucketName}".`); } catch (caught) { if ( caught instanceof S3ServiceException && caught.name === "NoSuchBucket" ) { console.error( `Error from S3 while deleting policy from ${bucketName}. The bucket doesn't exist.`, ); } else if (caught instanceof S3ServiceException) { console.error( `Error from S3 while deleting policy from ${bucketName}. ${caught.name}: ${caught.message}`, ); } else { throw caught; } } };
Kotlin
SDK 適用於 Kotlin
注意

還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

suspend fun deleteS3BucketPolicy(bucketName: String?) { val request = DeleteBucketPolicyRequest { bucket = bucketName } S3Client { region = "us-east-1" }.use { s3 -> s3.deleteBucketPolicy(request) println("Done!") } }
  • 如需API詳細資訊,請參閱DeleteBucketPolicy中的 AWS SDK for Kotlin API參考

PowerShell
適用於 的工具 PowerShell

範例 1:命令會移除與指定 S3 儲存貯體相關聯的儲存貯體政策。

Remove-S3BucketPolicy -BucketName 'amzn-s3-demo-bucket'
  • 如需API詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考 DeleteBucketPolicy中的 。

Python
SDK for Python (Boto3)
注意

還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

class BucketWrapper: """Encapsulates S3 bucket actions.""" def __init__(self, bucket): """ :param bucket: A Boto3 Bucket resource. This is a high-level resource in Boto3 that wraps bucket actions in a class-like structure. """ self.bucket = bucket self.name = bucket.name def delete_policy(self): """ Delete the security policy from the bucket. """ try: self.bucket.Policy().delete() logger.info("Deleted policy for bucket '%s'.", self.bucket.name) except ClientError: logger.exception( "Couldn't delete policy for bucket '%s'.", self.bucket.name ) raise
  • 如需API詳細資訊,請參閱 DeleteBucketPolicy 中的 AWS SDK for Python (Boto3) API參考

Ruby
SDK 適用於 Ruby
注意

還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

# Wraps an Amazon S3 bucket policy. class BucketPolicyWrapper attr_reader :bucket_policy # @param bucket_policy [Aws::S3::BucketPolicy] A bucket policy object configured with an existing bucket. def initialize(bucket_policy) @bucket_policy = bucket_policy end def delete_policy @bucket_policy.delete true rescue Aws::Errors::ServiceError => e puts "Couldn't delete the policy from #{@bucket_policy.bucket.name}. Here's why: #{e.message}" false end end
  • 如需API詳細資訊,請參閱 參考 DeleteBucketPolicy中的 。 AWS SDK for Ruby API