Amazon Web Services Bootcamp
上QQ阅读APP看书,第一时间看更新

Creating a policy

The following code creates a customer-managed policy under your AWS account:

String policyName = "AmazonS3FullAccess";
String description = "S3 Full Access On my-bucket";
String policyDocument = readFromFile("PolicyDocument.json");
String policyARN =
createPolicy(policyName, description, policyDocument);

........

public String createPolicy(
String policyName,
String description,
String policyDocument) {

CreatePolicyRequest createPolicyRequest =
new CreatePolicyRequest()
.withPolicyName(policyName)
.withDescription(description)
.withPolicyDocument(policyDocument);

CreatePolicyResult createPolicyResult =
amazonIdentityManagement
.createPolicy(createPolicyRequest);

return createPolicyResult.getPolicy().getArn();
}

The following is the policy document used to create the policy PolicyDocument.json:

{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": [
"s3:*"
],
"Resource": "arn:aws:s3:::my-bucket"
}]
}