0
votes

Hey I am trying to upload an image to s3 bucket my code is:

public static void addObjectToBucketFromStream(AmazonS3 conn,
        String bucketName, String keyName, InputStream inputStream,
        Map<String, String> metadataMap,String contentType) throws IOException {
    try {
        ObjectMetadata metadata = new ObjectMetadata();
        if (metadataMap != null) {
            for (Iterator iterator = metadataMap.keySet().iterator(); iterator
                    .hasNext();) {
                String key = (String) iterator.next();
                String value = metadataMap.get(key);
                metadata.addUserMetadata(key, value);
            }
        }
        metadata.setContentLength(inputStream.available());
        if (contentType!=null && !"".equals(contentType)){
            metadata.setContentType(contentType);
        }
        PutObjectRequest por = new PutObjectRequest(bucketName, keyName,
                inputStream, metadata);
        por.setCannedAcl(CannedAccessControlList.PublicRead);
        conn.putObject(por);
        int a;
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                // ignore
            }
        }
    }
}

In this code putObject() throws com.amazonaws.services.s3.model.PutObjectRequest throws com.amazonaws.services.s3.model.AmazonS3Exception

In log it is showing

com.amazonaws.services.s3.model.AmazonS3Exception: Status Code: 403, AWS Service: Amazon S3, AWS Request ID: 8C576CDFEC92534C, AWS Error Code: AccessDenied, AWS Error Message: Access Denied, S3 Extended Request ID: q7Rs1xrUbnJgptnJKS5T5qhPc6Y3NT66qaGGA5or6as0i0JloLAYODiHcoztD+seWyExpVHM4ls=

1
I would verify your bucket and key names and, if they are ok, then your credentials used to create the AmazonS3 object. You don't have permission to do the putObject but the question is why. Do you perhaps have an S3 read only user and a read/write user?stdunbar
403 indicates a credentials problem, and that code and IAM policy isn't given.tedder42

1 Answers

0
votes

A 403 error usually indicates a problem with your AWS credentials. What credentials are you using with your AmazonS3 object? By default, they'll be located in ~/.aws/credentials.

You can verify credentials are correct with the aws command-line tool. If you haven't used it before, run aws configure and enter your creds. Then you can attempt to upload a file with aws s3 cp somefile s3://mybucket.

If that succeeds, you can enable the Java SDK logging as documented here to get more information.