예제 #1
0
파일: uninstall.go 프로젝트: convox/rack
func deleteBucket(bucket string, wg *sync.WaitGroup, S3 *s3.S3) error {
	keyMarkers := []Obj{}
	versionIdMarkers := []Obj{}

	nextKeyMarker := aws.String("")
	nextVersionIdMarker := aws.String("")

	req := &s3.ListObjectVersionsInput{
		Bucket: aws.String(bucket),
	}

	res, err := S3.ListObjectVersions(req)
	if err != nil {
		wg.Done()
		return err
	}

	for _, d := range res.DeleteMarkers {
		keyMarkers = append(keyMarkers, Obj{key: *d.Key, id: *d.VersionId})
	}

	for _, v := range res.Versions {
		versionIdMarkers = append(versionIdMarkers, Obj{key: *v.Key, id: *v.VersionId})
	}

	nextKeyMarker = res.NextKeyMarker
	nextVersionIdMarker = res.NextVersionIdMarker

	for nextKeyMarker != nil && nextVersionIdMarker != nil {
		req.KeyMarker = nextKeyMarker
		req.VersionIdMarker = nextVersionIdMarker

		res, err := S3.ListObjectVersions(req)
		if err != nil {
			wg.Done()
			return err
		}

		for _, d := range res.DeleteMarkers {
			keyMarkers = append(keyMarkers, Obj{key: *d.Key, id: *d.VersionId})
		}

		for _, v := range res.Versions {
			versionIdMarkers = append(versionIdMarkers, Obj{key: *v.Key, id: *v.VersionId})
		}

		nextKeyMarker = res.NextKeyMarker
		nextVersionIdMarker = res.NextVersionIdMarker
	}

	fmt.Printf("Emptying S3 Bucket %s...\n", bucket)

	owg := new(sync.WaitGroup)
	owg.Add(len(keyMarkers))
	owg.Add(len(versionIdMarkers))
	go deleteObjects(bucket, keyMarkers, owg, S3)
	go deleteObjects(bucket, versionIdMarkers, owg, S3)
	owg.Wait()

	fmt.Printf("Deleting S3 Bucket %s...\n", bucket)

	_, err = S3.DeleteBucket(&s3.DeleteBucketInput{
		Bucket: aws.String(bucket),
	})
	if err != nil {
		fmt.Printf("Failed: %s\n", err)
	}

	wg.Done()
	return nil
}