prabhat kumar
prabhat kumar

Reputation: 171

Upload File in S3 of AWS

I am uploading file in s3 writing the below code:

String filePath=KeyProvider.localFolerPath + KeyProvider.suffix +KeyProvider.FileNameToUpload; 

// upload file to folder and set it to public
String fileName = folderName + KeyProvider.suffix +KeyProvider.remoteFileName ;
s3client.putObject(
            new PutObjectRequest(bucketName, fileName, new File(filePath))
                    .withCannedAcl(CannedAccessControlList.PublicReadWrite));`

It is throwing error like

com.amazonaws.services.s3.model.AmazonS3Exception: Access Denied

Please suggest me where is the error i have provided bucket name key and secret in a seperate constant file.

Thanks..

Upvotes: 2

Views: 1073

Answers (1)

hiropon
hiropon

Reputation: 1802

did you check Amazon's documents ?

And, be careful. You need to set access_key & access_secret strings to s3 client.

example

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.AmazonServiceException;

// "credentialsProvider" will be created by some classes for example "AWSStaticCredentialsProvider"
//
// BasicAWSCredentials cred = new BasicAWSCredentials("xxxxx", "yyyyy");
// AWSStaticCredentialsProvider credentialsProvider = new AWSStaticCredentialsProvider(cred);
AmazonS3 s3 = AmazonS3ClientBuilder.standard().withCredentials(credentialsProvider).build();

try {
    s3.putObject(bucket_name, key_name, file_path);
} catch (AmazonServiceException e) {
    System.err.println(e.getErrorMessage());
    System.exit(1);
}

EDIT

S3 policy can handle deny/allow access according to credentials or IP addresses. Do you configure such policy ?

Upvotes: 3

Related Questions