Delete Files From AWS S3 using Go
development golang aws- Uploading Files to AWS S3 using Go
- Listing Files in AWS S3 using Go
- Delete Files From AWS S3 using Go
In order to work with AWS S3 using Go, we will use github.com/aws/aws-sdk-go/aws
. In this gist, we will use the aws-sdk-go package to delete all files under a given directory in a given bucket. The complete source code for the snippet is available here https://github.com/abvarun226/blog-source-code/tree/master/delete-files-in-aws-s3-using-go
In order to test the snippet, we will use localstack to run s3 emulator in a docker container on the laptop. The container exposes s3 endpoint on port 4566
. The snippet will then create a new bucket work-with-s3
in s3. The snippet will then delete all files from blog
directory under work-with-s3
bucket.
In order to start the localstack
container, run:
$ docker-compose up --build --remove-orphans -d
In order to stop the container, run:
$ docker-compose down --remove-orphans
package main
import (
"context"
"log"
"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"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
)
func main() {
ctx := context.Background()
bucket := "work-with-s3"
conf := aws.NewConfig().
WithRegion("us-west-2").
WithEndpoint("http://127.0.0.1:4566").
WithS3ForcePathStyle(true)
sess, err := session.NewSession(conf)
if err != nil {
log.Fatalf("failed to create a new aws session: %v", sess)
}
s3client := s3.New(sess)
// create a new s3 bucket.
if _, err := s3client.CreateBucket(&s3.CreateBucketInput{Bucket: aws.String(bucket)}); err != nil {
if aerr, ok := err.(awserr.Error); ok && aerr.Code() != "Conflict" && aerr.Code() != "BucketAlreadyOwnedByYou" {
log.Fatalf("failed to create a new s3 bucket: %v", err)
}
}
// iterator to delete all files under `blog` directory.
iter := s3manager.NewDeleteListIterator(s3client, &s3.ListObjectsInput{
Bucket: aws.String(bucket),
Prefix: aws.String("blog/"),
})
// use the iterator to delete the files.
if err := s3manager.NewBatchDeleteWithClient(s3client).Delete(ctx, iter); err != nil {
log.Fatalf("failed to delete files under given directory: %v", err)
}
}