Golang
This Go program demonstrates how to interact with an S3-compatible service using the AWS SDK for Go. The program lists all available S3 buckets. Please use aws-sdk-go V1 version as follows.
Prerequisites
To run this program, you'll need:
Go installed on your machine (https://golang.org/doc/install).
AWS SDK for Go installed. You can get it using:
Access credentials (Access Key ID and Secret Access Key) for the S3-compatible service you are connecting to.
Code
Explanation
Setting up the S3 Configuration:
The program starts by setting the AWS access key, secret key, and endpoint of the S3-compatible service. The region is also set to
us
.S3ForcePathStyle
is set totrue
to use path-style addressing rather than virtual host style.
Creating a Session:
A new session is created using the configuration parameters. If the session creation fails, the program logs an error and exits.
Listing Buckets:
The program uses the
ListBuckets
API to retrieve all available buckets. If the request is successful, it prints out the names of the buckets.
Running the Program
Save the above code to a file, for example,
main.go
.Run the program using the following command:
The output will display the names of the S3 buckets.
Example Output
Notes
Make sure your AWS credentials (Access Key and Secret Key) are correctly set.
The program will list the buckets available at the specified S3-compatible service endpoint.
Last updated