Skip to content

Commit

Permalink
included option to url decode the key name
Browse files Browse the repository at this point in the history
  • Loading branch information
rem7 committed Jan 3, 2024
1 parent d992300 commit 184709c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
10 changes: 9 additions & 1 deletion cmd/s3tar/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func run(args []string) error {
var sizeLimit int64
var maxAttempts int
var concatInMemory bool
var urlDecode bool

cli.VersionFlag = &cli.BoolFlag{
Name: "print-version",
Expand Down Expand Up @@ -198,6 +199,12 @@ func run(args []string) error {
Usage: "create the tar object in ram; to use with small files and concatenate the part",
Destination: &concatInMemory,
},
&cli.BoolFlag{
Name: "urldecode",
Value: false,
Usage: "url decode the key value from the manifest",
Destination: &urlDecode,
},
},
Action: func(cCtx *cli.Context) error {
logLevel := parseLogLevel(cCtx.Count("verbose"))
Expand Down Expand Up @@ -242,6 +249,7 @@ func run(args []string) error {
Region: region,
EndpointUrl: endpointUrl,
ConcatInMemory: concatInMemory,
UrlDecode: urlDecode,
}
s3opts.DstBucket, s3opts.DstKey = s3tar.ExtractBucketAndPath(archiveFile)
s3opts.DstPrefix = filepath.Dir(s3opts.DstKey)
Expand All @@ -257,7 +265,7 @@ func run(args []string) error {
var estimatedSize int64
var err error
if s3opts.SrcManifest != "" {
objectList, estimatedSize, err = loadCSV(ctx, svc, s3opts.SrcManifest, s3opts.SkipManifestHeader)
objectList, estimatedSize, err = loadCSV(ctx, svc, s3opts.SrcManifest, s3opts.SkipManifestHeader, s3opts.UrlDecode)
} else {
objectList, estimatedSize, err = listAllObjects(ctx, svc, s3opts.SrcBucket, s3opts.SrcPrefix)
}
Expand Down
17 changes: 13 additions & 4 deletions input.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ import (
"github.com/aws/aws-sdk-go-v2/service/s3"
"io"
"log"
"net/url"
"strconv"
)

func LoadCSV(ctx context.Context, svc *s3.Client, fpath string, skipHeader bool) ([]*S3Obj, int64, error) {
func LoadCSV(ctx context.Context, svc *s3.Client, fpath string, skipHeader, urlDecode bool) ([]*S3Obj, int64, error) {
r, err := loadFile(ctx, svc, fpath)
if err != nil {
return nil, 0, err
}
defer r.Close()
return parseCSV(r, skipHeader)
return parseCSV(r, skipHeader, urlDecode)
}

func parseCSV(f io.Reader, skipHeader bool) ([]*S3Obj, int64, error) {
func parseCSV(f io.Reader, skipHeader bool, urlDecode bool) ([]*S3Obj, int64, error) {

var data []*S3Obj
var accum int64
Expand All @@ -48,8 +49,16 @@ func parseCSV(f io.Reader, skipHeader bool) ([]*S3Obj, int64, error) {
size = 0
}

key := record[1]
if urlDecode {
key, err = url.QueryUnescape(key)
if err != nil {
key = record[1]
}
}

opts := []func(*S3Obj){
WithBucketAndKey(record[0], record[1]),
WithBucketAndKey(record[0], key),
WithSize(size),
}

Expand Down
2 changes: 1 addition & 1 deletion s3tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func ServerSideTar(ctx context.Context, svc *s3.Client, opts *S3TarS3Options) er
var err error
if opts.SrcManifest != "" {
Infof(ctx, "using manifest file %s", opts.SrcManifest)
objectList, _, err = LoadCSV(ctx, svc, opts.SrcManifest, opts.SkipManifestHeader)
objectList, _, err = LoadCSV(ctx, svc, opts.SrcManifest, opts.SkipManifestHeader, opts.UrlDecode)
} else if opts.SrcBucket != "" {
Infof(ctx, "using source bucket '%s' and prefix '%s'", opts.SrcBucket, opts.SrcPrefix)
objectList, _, err = ListAllObjects(ctx, svc, opts.SrcBucket, opts.SrcPrefix)
Expand Down
1 change: 1 addition & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type S3TarS3Options struct {
storageClass types.StorageClass
extractPrefix string
ConcatInMemory bool
UrlDecode bool
}

func (o *S3TarS3Options) Copy() S3TarS3Options {
Expand Down

0 comments on commit 184709c

Please sign in to comment.