Created
October 24, 2020 07:59
-
-
Save unknwon/f13c3c378fe03b484a7f83ea46500b4f to your computer and use it in GitHub Desktop.
Move s3 files between directories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"os" | |
"path" | |
"github.com/aws/aws-sdk-go/aws" | |
"github.com/aws/aws-sdk-go/aws/credentials" | |
"github.com/aws/aws-sdk-go/aws/session" | |
"github.com/aws/aws-sdk-go/service/s3" | |
) | |
func main() { | |
bucket := os.Getenv("AWS_BUCKET") | |
sess := session.Must(session.NewSession( | |
&aws.Config{ | |
Region: aws.String(os.Getenv("AWS_REGION")), | |
Credentials: credentials.NewStaticCredentialsFromCreds( | |
credentials.Value{ | |
AccessKeyID: os.Getenv("AWS_ACCESS_KEY"), | |
SecretAccessKey: os.Getenv("AWS_ACCESS_SECRET"), | |
}, | |
), | |
}, | |
)) | |
svc := s3.New(sess) | |
var marker *string | |
for { | |
res, err := svc.ListObjects( | |
&s3.ListObjectsInput{ | |
Bucket: aws.String(bucket), | |
Delimiter: aws.String("/"), | |
Marker: marker, | |
Prefix: aws.String("<path under bucket>/"), // e.g. Must end with a "/" for a directory | |
}, | |
) | |
if err != nil { | |
log.Fatal("Failed to list objects", err) | |
} | |
fmt.Println(len(res.Contents), *res.IsTruncated, res.NextMarker) | |
for _, obj := range res.Contents { | |
srcKey := "/" + bucket + "/" + *obj.Key | |
destKey := "/<path under bucket>/" + path.Base(*obj.Key) | |
_, err = svc.CopyObject( | |
&s3.CopyObjectInput{ | |
Bucket: aws.String(bucket), | |
CopySource: aws.String(srcKey), | |
Key: aws.String(destKey), | |
}, | |
) | |
fmt.Println(srcKey, destKey) | |
if err != nil { | |
log.Printf("Failed to copy object: %v", err) | |
continue | |
} | |
_, _ = svc.DeleteObject( | |
&s3.DeleteObjectInput{ | |
Bucket: aws.String(bucket), | |
Key: obj.Key, | |
}, | |
) | |
} | |
marker = res.NextMarker | |
if marker == nil { | |
break | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment