Menghapus kebijakan dari bucket Amazon S3 menggunakan SDK AWS - AWSContoh Kode SDK

Ada lebih banyak contoh AWS SDK yang tersedia di repo Contoh SDK AWS Doc. GitHub

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Menghapus kebijakan dari bucket Amazon S3 menggunakan SDK AWS

Contoh kode berikut ini menunjukkan cara menghapus kebijakan dari bucket S3.

C++
SDK for C++
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di Repositori Contoh Kode AWS.

bool AwsDoc::S3::DeleteBucketPolicy(const Aws::String &bucketName, const Aws::Client::ClientConfiguration &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(); }
CLI
AWS CLI

Perintah berikut menghapus kebijakan bucket dari bucket bernamamy-bucket:

aws s3api delete-bucket-policy --bucket my-bucket
Java
SDK for Java 2.x
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di Repositori Contoh Kode 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(); } // Delete 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); } } }
JavaScript
SDK untuk JavaScript (v3)
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di Repositori Contoh Kode AWS.

Hapus kebijakan bucket.

import { DeleteBucketPolicyCommand, S3Client } from "@aws-sdk/client-s3"; const client = new S3Client({}); // This will remove the policy from the bucket. export const main = async () => { const command = new DeleteBucketPolicyCommand({ Bucket: "test-bucket", }); try { const response = await client.send(command); console.log(response); } catch (err) { console.error(err); } };
Kotlin
SDK for Kotlin
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di Repositori Contoh Kode AWS.

suspend fun deleteS3BucketPolicy(bucketName: String?) { val request = DeleteBucketPolicyRequest { bucket = bucketName } S3Client { region = "us-east-1" }.use { s3 -> s3.deleteBucketPolicy(request) println("Done!") } }
Python
SDK for Python (Boto3)
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di Repositori Contoh Kode 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
Ruby
SDK for Ruby
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di Repositori Contoh Kode 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