local clean
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/aws"
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||
)
|
||||
|
||||
func listAllBuckets(ctx context.Context, client *s3.Client) ([]string, error) {
|
||||
out, err := client.ListBuckets(ctx, &s3.ListBucketsInput{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var buckets []string
|
||||
for _, b := range out.Buckets {
|
||||
if b.Name != nil {
|
||||
buckets = append(buckets, *b.Name)
|
||||
}
|
||||
}
|
||||
return buckets, nil
|
||||
}
|
||||
|
||||
func listBucket(ctx context.Context, client *s3.Client, s3c CloudS3) ([]S3Item, error) {
|
||||
var items []S3Item
|
||||
var continuation *string
|
||||
|
||||
for {
|
||||
input := &s3.ListObjectsV2Input{
|
||||
Bucket: aws.String(s3c.Bucket),
|
||||
}
|
||||
if s3c.Folder != nil {
|
||||
input.Prefix = s3c.Folder
|
||||
}
|
||||
if continuation != nil {
|
||||
input.ContinuationToken = continuation
|
||||
}
|
||||
|
||||
out, err := client.ListObjectsV2(ctx, input)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, prefix := range out.CommonPrefixes {
|
||||
if prefix.Prefix != nil {
|
||||
p := *prefix.Prefix
|
||||
items = append(items, S3Item{
|
||||
Key: p,
|
||||
IsFolder: true,
|
||||
Size: nil,
|
||||
FolderName: &p,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
for _, obj := range out.Contents {
|
||||
if obj.Key == nil {
|
||||
continue
|
||||
}
|
||||
key := *obj.Key
|
||||
if strings.HasSuffix(key, "/") {
|
||||
continue
|
||||
}
|
||||
var folderName *string
|
||||
if idx := strings.LastIndex(key, "/"); idx >= 0 {
|
||||
f := key[:idx]
|
||||
folderName = &f
|
||||
}
|
||||
|
||||
var sizePtr *int64
|
||||
if obj.Size != nil {
|
||||
s := *obj.Size
|
||||
sizePtr = &s
|
||||
}
|
||||
|
||||
var lastModifiedPtr *string
|
||||
if obj.LastModified != nil {
|
||||
lm := obj.LastModified.UTC().Format(time.RFC3339)
|
||||
lastModifiedPtr = &lm
|
||||
}
|
||||
|
||||
items = append(items, S3Item{
|
||||
Key: key,
|
||||
IsFolder: false,
|
||||
FolderName: folderName,
|
||||
Size: sizePtr,
|
||||
LastModified: lastModifiedPtr,
|
||||
})
|
||||
}
|
||||
|
||||
if out.NextContinuationToken != nil {
|
||||
continuation = out.NextContinuationToken
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Printf("Contents count: %d\n", len(items))
|
||||
return items, nil
|
||||
}
|
||||
Reference in New Issue
Block a user