简体   繁体   中英

AWS S3 Java Access Denied Service: Amazon S3; Status Code: 403; Error Code: AccessDenied;

New to AWS S3, trying to integrate AWS Java SDK for S3 storage. Granted permission to to my user as follows

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "Read Access",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::9123...123:root"
            },
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::go_bucket/*"
        }
    ]
}

Still in Java code getting

Received error response: com.amazonaws.services.s3.model.AmazonS3Exception: Access Denied (Service: Amazon S3; Status Code: 403; Error Code: AccessDenied; Request ID: 6DRTT

Java code

        final AmazonS3 s3 = AmazonS3ClientBuilder.standard().withRegion("us-east-1").build();
        try {
            S3Object o = s3.getObject(bucket_name, key_name);
            S3ObjectInputStream s3is = o.getObjectContent();
            FileOutputStream fos = new FileOutputStream(new File(key_name));
            byte[] read_buf = new byte[1024];
            int read_len = 0;
            while ((read_len = s3is.read(read_buf)) > 0) {
                fos.write(read_buf, 0, read_len);
            }
            s3is.close();
            fos.close();
        }

But, when I am using "Principal": "*" it is working

AWS SDK is not reading credentials from C:\Users\myuser\.aws\credentials in Windows

As of now, below has solved the problem

BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials("aws_access_key_id",
                "aws_secret_access_key");

        final AmazonS3 s3 = AmazonS3ClientBuilder.standard()
                .withCredentials(new AWSStaticCredentialsProvider(basicAWSCredentials))
                .withRegion("us-east-1")
                .build();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM