Reputation: 47
I have been trying to learn CRUD operations on S3 buckets using golang and localstack
using the below code
import (
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
var (
s3session *s3.S3
)
const (
REGION = "us-east-1"
Bucket_Name string = "sahil1234"
)
func init() {
s3session = s3.New(session.Must(session.NewSession(&aws.Config{
Region: aws.String("us-east-1"),
Endpoint: aws.String("http://localhost:4566/"),
})))
}
func listBuckets() (resp *s3.ListBucketsOutput) {
resp, err := s3session.ListBuckets(&s3.ListBucketsInput{})
if err != nil {
panic(err)
}
return resp
}
func createBucket() (resp *s3.CreateBucketOutput) {
resp, err := s3session.CreateBucket(&s3.CreateBucketInput{
// ACL: aws.String(s3.BucketCannedACLPrivate),
// ACL: aws.String(s3.BucketCannedACLPublicRead),
Bucket: aws.String(Bucket_Name),
CreateBucketConfiguration: &s3.CreateBucketConfiguration{
LocationConstraint: aws.String(REGION),
},
})
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case s3.ErrCodeBucketAlreadyExists:
fmt.Println("Bucket name already in use!")
panic(err)
case s3.ErrCodeBucketAlreadyOwnedByYou:
fmt.Println("Bucket exists and is owned by you!")
default:
panic(err)
}
}
}
return resp
}
func uploadObject(filename string) (resp *s3.PutObjectOutput) {
f, err := os.Open(filename)
if err != nil {
panic(err)
}
fmt.Println("Uploading:", filename)
resp, err = s3session.PutObject(&s3.PutObjectInput{
Body: f,
Bucket: aws.String(Bucket_Name),
Key: aws.String(strings.Split(filename, "/")[1]),
ACL: aws.String(s3.BucketCannedACLPublicRead),
})
if err != nil {
panic(err)
}
return resp
}
func listObjects() (resp *s3.ListObjectsV2Output) {
resp, err := s3session.ListObjectsV2(&s3.ListObjectsV2Input{
Bucket: aws.String(Bucket_Name),
})
if err != nil {
panic(err)
}
return resp
}
func getObject(filename string) {
fmt.Println("Downloading: ", filename)
resp, err := s3session.GetObject(&s3.GetObjectInput{
Bucket: aws.String(Bucket_Name),
Key: aws.String(filename),
})
if err != nil {
panic(err)
}
body, err := ioutil.ReadAll(resp.Body)
err = ioutil.WriteFile(filename, body, 0644)
if err != nil {
panic(err)
}
}
func deleteObject(filename string) (resp *s3.DeleteObjectOutput) {
fmt.Println("Deleting: ", filename)
resp, err := s3session.DeleteObject(&s3.DeleteObjectInput{
Bucket: aws.String(Bucket_Name),
Key: aws.String(filename),
})
if err != nil {
panic(err)
}
return resp
}
func main() {
fmt.Println(listBuckets())
fmt.Println(createBucket())
fmt.Println(listBuckets())
}
even tho the creatBucket returns succesfully, it does not create an actual bucket, it just returns
{
Location: "/None"
}
The bucket also does not show up when i do aws --endpoint-url=http://localhost:4566 s3 ls
Any help would be much appreciated!
output:
{
Buckets: [{
CreationDate: 2021-09-29 07:25:21 +0000 UTC,
Name: "my-bucket1212"
}],
Owner: {
DisplayName: "webfile",
ID: "bcaf1ffd86f41161ca5fb16fd081034f"
}
}
{
Location: "/None"
}
{
Buckets: [{
CreationDate: 2021-09-29 07:25:21 +0000 UTC,
Name: "my-bucket1212"
}],
Owner: {
DisplayName: "webfile",
ID: "bcaf1ffd86f41161ca5fb16fd081034f"
}
}
PS: mybucket1212 is a bucket i created using aws-cli
Upvotes: 3
Views: 1647
Reputation: 1528
the easiest option now is to set S3ForcePathStyle
in your config:
S3ForcePathStyle: aws.Bool(true),
This is the way aws-cli
is doing it.
but this options is being deprecated by AWS even though without a set date.
I'll try and find a way to make it work without this option and update this answer.
Upvotes: 5