You can use your AWS account or IAM user security credentials to send authenticated
requests to Amazon S3. This section provides examples of how you can send authenticated requests
using the AWS SDK for Java, AWS SDK for .NET, and AWS SDK for PHP. For a list of available AWS SDKs, go to Sample Code and Libraries
Each of these AWS SDKs uses an SDK-specific credentials provider chain to find and use credentials and perform actions on behalf of the credentials owner. What all these credentials provider chains have in common is that they all look for your local AWS credentials file.
For more information, see the topics below:
To create a local AWS credentials file
The easiest way to configure credentials for your AWS SDKs is to use an AWS credentials file. If you use the AWS Command Line Interface (AWS CLI), you may already have a local AWS credentials file configured. Otherwise, use the following procedure to set up a credentials file:
Sign in to the AWS Management Console and open the IAM console at https://console.aws.amazon.com/iam/
. -
Create a new user with permissions limited to the services and actions that you want your code to have access to. For more information about creating a new user, see Creating IAM users (Console), and follow the instructions through step 8.
-
Choose Download .csv to save a local copy of your AWS credentials.
-
On your computer, navigate to your home directory, and create an
.aws
directory. On Unix-based systems, such as Linux or OS X, this is in the following location:~/.aws
On Windows, this is in the following location:
%HOMEPATH%\.aws
-
In the
.aws
directory, create a new file namedcredentials
. -
Open the credentials
.csv
file that you downloaded from the IAM console, and copy its contents into thecredentials
file using the following format:[default] aws_access_key_id = your_access_key_id aws_secret_access_key = your_secret_access_key
-
Save the
credentials
file, and delete the.csv
file that you downloaded in step 3.
Your shared credentials file is now configured on your local computer, and it's ready to be used with the AWS SDKs.
Sending authenticated requests using the AWS SDKs
Use the AWS SDKs to send authenticated requests. For more information about sending authenticated requests, see AWS security credentials or IAM Identity Center Authentication.
To send authenticated requests to Amazon S3 using your AWS account or IAM user credentials, do the following:
-
Use the
AmazonS3ClientBuilder
class to create anAmazonS3Client
instance. -
Run one of the
AmazonS3Client
methods to send requests to Amazon S3. The client generates the necessary signature from the credentials that you provide and includes it in the request.
The following example performs the preceding tasks. For information on creating and testing a working sample, see Getting Started in the AWS SDK for Java Developer Guide.
import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.ListObjectsRequest;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import java.io.IOException;
import java.util.List;
public class MakingRequests {
public static void main(String[] args) throws IOException {
Regions clientRegion = Regions.DEFAULT_REGION;
String bucketName = "*** Bucket name ***";
try {
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
.withCredentials(new ProfileCredentialsProvider())
.withRegion(clientRegion)
.build();
// Get a list of objects in the bucket, two at a time, and
// print the name and size of each object.
ListObjectsRequest listRequest = new ListObjectsRequest().withBucketName(bucketName).withMaxKeys(2);
ObjectListing objects = s3Client.listObjects(listRequest);
while (true) {
List<S3ObjectSummary> summaries = objects.getObjectSummaries();
for (S3ObjectSummary summary : summaries) {
System.out.printf("Object \"%s\" retrieved with size %d\n", summary.getKey(), summary.getSize());
}
if (objects.isTruncated()) {
objects = s3Client.listNextBatchOfObjects(objects);
} else {
break;
}
}
} catch (AmazonServiceException e) {
// The call was transmitted successfully, but Amazon S3 couldn't process
// it, so it returned an error response.
e.printStackTrace();
} catch (SdkClientException e) {
// Amazon S3 couldn't be contacted for a response, or the client
// couldn't parse the response from Amazon S3.
e.printStackTrace();
}
}
}