Skip to content

Commit

Permalink
Merge pull request #6 from andypern/multipledelete
Browse files Browse the repository at this point in the history
added logic for multiple delete
  • Loading branch information
nranchev authored Feb 23, 2017
2 parents bffad6f + 2fd9389 commit 8810b3c
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions s3bench.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import (
const (
opRead = "Read"
opWrite = "Write"
//max that can be deleted at a time via DeleteObjects()
commitSize = 1000

)

var bufferBytes []byte
Expand Down Expand Up @@ -103,20 +106,37 @@ func main() {
// Do cleanup if required
if !*skipCleanup {
fmt.Println()
fmt.Printf("Cleaning up %d objects...", *numSamples)
numDeleted := 0
fmt.Printf("Cleaning up %d objects...\n", *numSamples)
delStartTime := time.Now()
svc := s3.New(session.New(), cfg)
for i := 0; i < *numSamples; i++ {
params := &s3.DeleteObjectInput{
Bucket: aws.String(*bucketName),
Key: aws.String(fmt.Sprintf("%s%d", *objectNamePrefix, i)),
}
_, err := svc.DeleteObject(params)
if err == nil {
numDeleted++

numSuccessfullyDeleted := 0

keyList := make([]*s3.ObjectIdentifier, 0, commitSize)
for i := 0; i < *numSamples; i++{
bar := s3.ObjectIdentifier{
Key: aws.String(fmt.Sprintf("%s%d", *objectNamePrefix, i)),
}
keyList = append(keyList, &bar)
if len(keyList) == commitSize || i == *numSamples-1 {
fmt.Printf("Deleting a batch of %d objects in range {%d, %d}... ", len(keyList), i-len(keyList)+1, i)
params := &s3.DeleteObjectsInput {
Bucket: aws.String(*bucketName),
Delete: &s3.Delete{
Objects: keyList}}
_, err := svc.DeleteObjects(params)
if err == nil {
numSuccessfullyDeleted += len(keyList)
fmt.Printf("Succeeded\n")
} else {
fmt.Printf("Failed (%v)\n", err)
}
//set cursor to 0 so we can move to the next batch.
keyList = keyList[:0]

}
}
fmt.Printf("Done (%d/%d)\n", numDeleted, *numSamples)
fmt.Printf("Successfully deleted %d/%d objects in %s\n", numSuccessfullyDeleted, *numSamples, time.Since(delStartTime))
}
}

Expand Down

0 comments on commit 8810b3c

Please sign in to comment.