gpt4 book ai didi

java - 如何通过java sdk在aws中启用服务器访问日志记录

转载 作者:行者123 更新时间:2023-12-02 09:27:00 24 4
gpt4 key购买 nike

我正在尝试使用 java SDK 为我新创建的 S3 存储桶启用服务器访问日志记录

我认为我无法正确设置 URI,我收到的错误是请授予目标存储桶 READ_ACP 和 WRITE 权限

我无法在 S3Grantee 中设置任何 URI 作为选项,而且我也无法设置所有者

请看一下我的代码:b2是实际的桶目标存储桶是我想要发送服务器日志的位置

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());
}
}

关于java - 如何通过java sdk在aws中启用服务器访问日志记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58277519/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com