Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix trimming of S3_PATH prefix when walking S3 bucket content #407

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions pkg/new_storage/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package new_storage
import (
"context"
"crypto/tls"
"github.com/AlexAkulov/clickhouse-backup/pkg/config"
"io"
"net/http"
"os"
"path"
"strings"
"time"

"github.com/AlexAkulov/clickhouse-backup/pkg/config"

"golang.org/x/sync/errgroup"

"github.com/apex/log"
Expand Down Expand Up @@ -210,14 +211,14 @@ func (s *S3) Walk(s3Path string, recursive bool, process func(r RemoteFile) erro
return s.remotePager(path.Join(s.Config.Path, s3Path), recursive, func(page *s3.ListObjectsV2Output) {
for _, cp := range page.CommonPrefixes {
s3Files <- &s3File{
name: strings.TrimPrefix(*cp.Prefix, path.Join(s.Config.Path, s3Path)),
name: s.trimS3Prefix(s3Path, *cp.Prefix),
}
}
for _, c := range page.Contents {
s3Files <- &s3File{
*c.Size,
*c.LastModified,
strings.TrimPrefix(*c.Key, path.Join(s.Config.Path, s3Path)),
size: *c.Size,
lastModified: *c.LastModified,
name: s.trimS3Prefix(*c.Key, s3Path),
}
}
})
Expand Down Expand Up @@ -254,6 +255,17 @@ func (s *S3) remotePager(s3Path string, recursive bool, pager func(page *s3.List
return s3.New(s.session).ListObjectsV2Pages(params, wrapper)
}

func (s *S3) trimS3Prefix(s3Path, key string) string {
toTrim := path.Join(s.Config.Path, s3Path)
if toTrim == "" {
return key
}
if strings.HasPrefix(key, "/") {
return strings.TrimPrefix(key, toTrim)
}
return strings.TrimPrefix(key, toTrim[1:])
}

type s3File struct {
size int64
lastModified time.Time
Expand Down