D'autres exemples de AWS SDK sont disponibles dans le référentiel AWS Doc SDK Examples
Les traductions sont fournies par des outils de traduction automatique. En cas de conflit entre le contenu d'une traduction et celui de la version originale en anglais, la version anglaise prévaudra.
L'exemple de code suivant montre comment utiliserGetBucketPolicy
.
- SDK pour Java 2.x
-
Note
Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS
. Obtenez la politique d'un bucket de répertoire.
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.GetBucketPolicyRequest; import software.amazon.awssdk.services.s3.model.GetBucketPolicyResponse; import software.amazon.awssdk.services.s3.model.S3Exception; import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.createS3Client; import static com.example.s3.util.S3DirectoryBucketUtils.deleteDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.getAwsAccountId; import static com.example.s3.util.S3DirectoryBucketUtils.putDirectoryBucketPolicy; /** * Retrieves the bucket policy for the specified S3 directory bucket. * * @param s3Client The S3 client used to interact with S3 * @param bucketName The name of the directory bucket * @return The bucket policy text */ public static String getDirectoryBucketPolicy(S3Client s3Client, String bucketName) { logger.info("Getting policy for bucket: {}", bucketName); try { // Create a GetBucketPolicyRequest GetBucketPolicyRequest policyReq = GetBucketPolicyRequest.builder() .bucket(bucketName) .build(); // Retrieve the bucket policy GetBucketPolicyResponse response = s3Client.getBucketPolicy(policyReq); // Print and return the policy text String policyText = response.policy(); logger.info("Bucket policy: {}", policyText); return policyText; } catch (S3Exception e) { logger.error("Failed to get bucket policy: {} - Error code: {}", e.awsErrorDetails().errorMessage(), e.awsErrorDetails().errorCode(), e); throw e; } }
-
Pour plus de détails sur l'API, reportez-vous GetBucketPolicyà la section Référence des AWS SDK for Java 2.x API.
-