如何通过java sdk在aws中启用服务器访问日志记录
我正在尝试使用JavaSDK为新创建的S3存储桶启用服务器访问日志记录 我无法正确设置URI,我想,我得到的错误是请向目标存储桶授予读取和写入权限 我无法将S3中的任何URI设置为选项,也无法设置所有者 请看一下我的代码: b2是实际铲斗 目标存储桶是我想要发送服务器日志的地方如何通过java sdk在aws中启用服务器访问日志记录,java,amazon-web-services,amazon-s3,aws-sdk,Java,Amazon Web Services,Amazon S3,Aws Sdk,我正在尝试使用JavaSDK为新创建的S3存储桶启用服务器访问日志记录 我无法正确设置URI,我想,我得到的错误是请向目标存储桶授予读取和写入权限 我无法将S3中的任何URI设置为选项,也无法设置所有者 请看一下我的代码: b2是实际铲斗 目标存储桶是我想要发送服务器日志的地方 try { // Step 1 - Grant Log Delivery group permission to write log to the target // bucket.
try {
// Step 1 - Grant Log Delivery group permission to write log to the target
// bucket.
GrantPermissionsToWriteLogsAsync(s3client, b2);
// Step 2 - Enable logging on the source bucket.
EnableDisableLoggingAsync(s3client, b2);
} catch (AmazonS3Exception e) {
logger.error("Error encountered on server " + e.getErrorMessage());
} catch (Exception ex) {
logger.error("Unknown encountered on server", ex.getMessage());
}
}
private void EnableDisableLoggingAsync(AmazonS3 s3Client, Bucket b2) {
// TODO Auto-generated method stub
BucketLoggingConfiguration bucketLoggingConfiguration = new BucketLoggingConfiguration();
bucketLoggingConfiguration.setDestinationBucketName("destination-bucket");
bucketLoggingConfiguration.setLogFilePrefix("s3access/");
SetBucketLoggingConfigurationRequest setBucketLoggingConfigurationRequest = new SetBucketLoggingConfigurationRequest(
b2.getName(), bucketLoggingConfiguration);
s3Client.setBucketLoggingConfiguration(setBucketLoggingConfigurationRequest);
}
private void GrantPermissionsToWriteLogsAsync(AmazonS3 s3Client, Bucket b2) {
try {
S3AccessControlList bucketACL = new S3AccessControlList();
AccessControlList aclResponse = s3Client
.getBucketAcl((new GetBucketAclRequest("destination-bucket")));
Owner owner = aclResponse.getOwner();
// aclResponse.setOwner(owner);
// bucketACL.setOwner(owner);
// Create a collection of grants to add to the bucket.
ArrayList<Grant> grantCollection = new ArrayList<Grant>();
// Grant the LogDelivery group permission to write to the bucket.
Grant grant2 = new Grant(GroupGrantee.LogDelivery, Permission.Write);
grantCollection.add(grant2);
Collection<S3Grant> grants = new ArrayList<S3Grant>();
S3Grant grant1 = new S3Grant();
grant1.withPermission(S3Permission.READ_ACP);
S3Grantee grantee = new S3Grantee();
grantee.setIdentifier("http://acs.amazonaws.com/groups/s3/LogDelivery");
grant1.withGrantee(grantee);
S3Grant grant3 = new S3Grant();
grant3.withPermission(S3Permission.WRITE);
S3Grantee grantee2 = new S3Grantee();
grantee.setIdentifier("http://acs.amazonaws.com/groups/s3/LogDelivery");
grant3.withGrantee(grantee2);
grants.add(grant1);
grants.add(grant3);
bucketACL.setGrants(grants);
// s3Client.setB
// s3Client.setBucketAcl("destination-bucket", bucketACL);
SetBucketAclRequest setBucketAclRequest = new SetBucketAclRequest("destination-bucket", aclResponse);
s3Client.setBucketAcl(setBucketAclRequest);
} catch (AmazonS3Exception ex) {
logger.error("error :: " + ex.getMessage());
}
}
问题在于GrantPermissionsToWriteLogsAsync方法,不确定您在做什么,但应该是这样的:
private static void GrantPermissionsToWriteLogsAsync(AmazonS3 s3Client, Bucket b2) {
try {
AccessControlList bucketACL = s3Client.getBucketAcl((new GetBucketAclRequest(LOGGING_BUCKET)));
// Grant the LogDelivery group permission to write to the bucket.
Grant grant2 = new Grant(GroupGrantee.LogDelivery, Permission.Write);
// Grant the LogDelivery group permission to read ACP to the bucket.
Grant grant3 = new Grant(GroupGrantee.LogDelivery, Permission.ReadAcp);
bucketACL.grantAllPermissions(grant2, grant3);
SetBucketAclRequest setBucketAclRequest = new SetBucketAclRequest(LOGGING_BUCKET, bucketACL);
s3Client.setBucketAcl(setBucketAclRequest);
} catch (AmazonS3Exception ex) {
logger.severe("error :: " + ex.getMessage());
}
}