DeleteObjects.go
/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
This file is licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License. A copy of
the License is located at
http://aws.amazon.com/apache2.0/
This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
*/
package main
import (
"flag"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"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"
)
// DeleteItems deletes all of the objects in the bucket
// Inputs:
// sess is the current session, which provides configuration for the SDK's service clients
// bucket is the name of the bucket
// Output:
// If success, nil
// Otherwise, an error from the call to NewBatchDeleteWithClient
func DeleteItems(sess *session.Session, bucket *string) error {
svc := s3.New(sess)
iter := s3manager.NewDeleteListIterator(svc, &s3.ListObjectsInput{
Bucket: bucket,
})
err := s3manager.NewBatchDeleteWithClient(svc).Delete(aws.BackgroundContext(), iter)
if err != nil {
return err
}
return nil
}
func main() {
bucket := flag.String("b", "", "The bucket to empty")
flag.Parse()
if *bucket == "" {
fmt.Println("You must supply a bucket to empty (-b BUCKET)")
return
}
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
err := DeleteItems(sess, bucket)
if err != nil {
fmt.Println("Got an error deleting items:")
fmt.Println(err)
return
}
fmt.Println("Deleted object(s) from " + *bucket)
}