دریافت وضعیت سیاستگذاری صندوقچه
مولفهها
- کلیدهای احراز هویت
- نام صندوقچه
- NET.
- PHP
- Python
- Javascript
- GO
using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Reflection;
using Newtonsoft.Json;
namespace GetBucketPolicyStatus
{
class GetBucketPolicyStatus
{
private const string bucketName = "<BUCKET_NAME>";
private static IAmazonS3 _s3Client;
public static void Main()
{
var awsCredentials = new Amazon.Runtime.BasicAWSCredentials("<ACCESS-KEY>", "<SECRET-KEY>");
var config = new AmazonS3Config { ServiceURL = "<ENDPOINT>" };
_s3Client = new AmazonS3Client(awsCredentials, config);
GetBucketPolicyStatusAsync().Wait();
}
private static async Task GetBucketPolicyStatusAsync()
{
try
{
GetBucketPolicyStatusResponse response = await _s3Client.GetBucketPolicyStatusAsync(new GetBucketPolicyStatusRequest
{
BucketName = bucketName,
});
Console.WriteLine(JsonConvert.SerializeObject(response, Formatting.Indented));
}
catch (AmazonS3Exception amazonS3Exception)
{
Console.WriteLine("An AmazonS3Exception was thrown. Exception: " + amazonS3Exception.ToString());
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.ToString());
}
}
}
}
<?php
require('client.php');
$bucket = '<BUCKET_NAME>';
try {
$result = $client->getBucketPolicyStatus([
'Bucket' => $bucket, // REQUIRED
]);
var_dump($result);
} catch (AwsException $e) {
// Display error message
echo $e->getMessage();
echo "\n";
}
import boto3
import logging
from botocore.exceptions import ClientError
logging.basicConfig(level=logging.INFO)
try:
s3_client = boto3.client(
's3',
endpoint_url='<ENDPOINT>',
aws_access_key_id='<ACCESS-KEY>',
aws_secret_access_key='<SECRET-KEY>'
)
except Exception as exc:
logging.error(exc)
else:
try:
response = s3_client.delete_bucket_tagging(
Bucket='<BUCKET_NAME>',
# ExpectedBucketOwner='string'
)
logging.info(response)
except ClientError as exc:
logging.error(exc)
// Import required AWS SDK clients and commands for Node.js
const {
S3Client,
GetBucketPolicyStatusCommand,
} = require("@aws-sdk/client-s3");
// Create an S3 client service object
const s3 = new S3Client({
region: "default",
endpoint: "endpoint_url",
credentials: {
accessKeyId: "access_key",
secretAccessKey:
"secret_key",
},
});
const BUCKET_NAME = "sample_bucket";
const run = async () => {
try {
const response = await s3.send(
new GetBucketPolicyStatusCommand({
Bucket: BUCKET_NAME,
})
);
console.log("Success", response);
} catch (err) {
console.log("Error", err);
}
};
run();
package main
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"fmt"
"os"
)
// Retrieve status of bucket policy (is public?)
//
// Usage:
// go run s3_get_bucket_policy_status.go BUCKET
func main() {
if len(os.Args) != 2 {
exitErrorf("Bucket name required\nUsage: go run", os.Args[0], "BUCKET")
}
bucket := os.Args[1]
sess, err := session.NewSession(&aws.Config{
Credentials: credentials.NewStaticCredentials("<ACCESS-KEY>", "<SECRET-KEY>", ""),
})
svc := s3.New(sess, &aws.Config{
Region: aws.String("default"),
Endpoint: aws.String("<ENDPOINT>"),
})
result, err := svc.GetBucketPolicyStatus(&s3.GetBucketPolicyStatusInput{Bucket: &bucket})
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
return
}
fmt.Println(result)
}
func exitErrorf(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, msg+"\n", args...)
os.Exit(1)
}
برای اجرای قطعه کد بالا با فرض نامگذاری فایل کد به s3_get_bucket_policy_status.go می توان از دستور زیر استفاده کرد:
go run s3_get_bucket_policy_status.go BUCKET