Java
This example demonstrates how to connect to an S3 compatible service and list all buckets using the AWS SDK for Java.
Prerequisites
Before running the code, ensure you have the following:
Java Development Kit (JDK) installed.
AWS SDK for Java added as a dependency.
Replace
YOUR_ACCESS_KEY
andYOUR_SECRET_KEY
with your actual AWS credentials.
Maven Dependency
If you're using Maven, you need to add the following dependency in your pom.xml
:
Java Code
Explanation
Steps in the Code
Credentials: The program uses
BasicAWSCredentials
to provide the access key and secret key. Make sure to replaceYOUR_ACCESS_KEY
andYOUR_SECRET_KEY
with your actual credentials.Client Configuration: The
AmazonS3ClientBuilder
is configured with the endpoint and region of the S3-compatible service. The.withPathStyleAccessEnabled(true)
ensures path-style access to the buckets.List Buckets: The
listBuckets
method is called on theAmazonS3
client to retrieve all the available buckets. These buckets are then printed to the console.Error Handling: If an error occurs during the process, it is caught in the
catch
block, and the error message is printed.
Run the Code
To run the program, you can compile and execute it as a standard Java application. Make sure to have the AWS SDK for Java dependency correctly added to your project.
Note: Ensure that you replace the placeholders for access key and secret key with your own credentials. These credentials should be securely stored and not hardcoded in production applications.
Last updated