Jayesh Thanki
Jayesh Thanki

Reputation: 2077

AWS S3 Bucket Issue

Can anyone help me to find how to access AWS S3 Bucket resources using client Access key ID and Secret access key? Or Is this deprecated AWS API? I find many solutions but all uses identityPoolId which is i don't want to use.

Upvotes: 1

Views: 373

Answers (1)

SkyWalker
SkyWalker

Reputation: 29150

You can login using 2 ways.

  1. By using credentials within your code.
  2. Keep your credentials in a file.

By using credentials within your code.

// credentials object identifying user for authentication
// user must have AWSConnector and AmazonS3FullAccess for 
// this example to work
 AWSCredentials credentials = new BasicAWSCredentials("YourAccessKeyID", "YourSecretAccessKey");

// create a client connection based on credentials
AmazonS3 s3client = new AmazonS3Client(credentials);

Keep your credentials in a file:

/*
 * Create your credentials file at ~/.aws/credentials (C:\Users\USER_NAME\.aws\credentials for Windows users) 
 * and save the following lines after replacing the underlined values with your own.
 *
 * [default]
 * aws_access_key_id = YOUR_ACCESS_KEY_ID
 * aws_secret_access_key = YOUR_SECRET_ACCESS_KEY
 */

AWSCredentials credentials = new ProfileCredentialsProvider().getCredentials();
AmazonS3 s3 = new AmazonS3Client(credentials);

For crud operation, you can go through this tutorial: https://github.com/aws/aws-sdk-java/blob/master/src/samples/AmazonS3/S3Sample.java

    // Create a bucket
    System.out.println("Creating bucket " + bucketName + "\n");
    s3.createBucket(bucketName);

    /*
     * List the buckets in your account
     */
    System.out.println("Listing buckets");
    for (Bucket bucket : s3.listBuckets()) {
        System.out.println(" - " + bucket.getName());
    }
    /*
     * Delete an object - Unless versioning has been turned on for your bucket,
     * there is no way to undelete an object, so use caution when deleting objects.
     */
    System.out.println("Deleting an object\n");
    s3.deleteObject(bucketName, key);

    /*
     * Delete a bucket - A bucket must be completely empty before it can be
     * deleted, so remember to delete any objects from your buckets before
     * you try to delete them.
     */
    System.out.println("Deleting bucket " + bucketName + "\n");
    s3.deleteBucket(bucketName);

For giving permissions in a specific bucket, please check the picture:

enter image description here

Add Permission:

{
    "Version": "2008-10-17",
    "Statement": [
        {
            "Sid": "AllowPublicRead",
            "Effect": "Allow",
            "Principal": {
                "AWS": "*"
            },
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::riz-bucket001/*"
        }
    ]
}

N.B: This bucket policy make everything in the bucket publicly readable. So be careful to use it. If you use study purpose, then OK. But in business purpose, don't use it.

Thanks a lot Michael - sqlbot

You can check more policies here as your necessity: Specifying Permissions in a Policy

Upvotes: 1

Related Questions