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

feat(storage/transfermanager): add option to StripPrefix on directory download #10894

Merged
merged 6 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
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
16 changes: 14 additions & 2 deletions storage/transfermanager/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"math"
"os"
"path/filepath"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -175,8 +176,13 @@ func (d *Downloader) DownloadDirectory(ctx context.Context, input *DownloadDirec
inputs := make([]DownloadObjectInput, 0, len(objectsToQueue))

for _, object := range objectsToQueue {
objDirectory := filepath.Join(input.LocalDirectory, filepath.Dir(object))
filePath := filepath.Join(input.LocalDirectory, object)
objectWithoutPrefix := object
if len(input.StripPrefix) > 0 {
objectWithoutPrefix, _ = strings.CutPrefix(object, input.StripPrefix)
}

objDirectory := filepath.Join(input.LocalDirectory, filepath.Dir(objectWithoutPrefix))
filePath := filepath.Join(input.LocalDirectory, objectWithoutPrefix)

// Make sure all directories in the object path exist.
err := os.MkdirAll(objDirectory, fs.ModeDir|fs.ModePerm)
Expand Down Expand Up @@ -750,6 +756,12 @@ type DownloadDirectoryInput struct {
// The directory will be created if it does not already exist. Required.
LocalDirectory string

// StripPrefix is a prefix to be removed from the path of the local file on
// download from GCS. For example, if you have an object in GCS called
// "mydirectory/a/file", and StripPrefix is set to "mydirectory/", the file
// will be downloaded to "{LocalDirectory}/a/file". Optional.
StripPrefix string

// Prefix is the prefix filter to download objects whose names begin with this.
// Optional.
Prefix string
Expand Down
9 changes: 7 additions & 2 deletions storage/transfermanager/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"math/rand"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -100,6 +101,7 @@ func TestIntegration_DownloadDirectory(t *testing.T) {
if err := d.DownloadDirectory(ctx, &DownloadDirectoryInput{
Bucket: tb.bucket,
LocalDirectory: localDir,
StripPrefix: "dir/",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you know that dir/ is part of the object name to test removal?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind!

// Create initializes the downloadTestBucket, creating a bucket and populating
// objects in it. All objects are of the same size but with different contents
// and can be mapped to their respective crc32c hash in contentHashes.
func (tb *downloadTestBucket) Create(prefix string) error {

OnObjectDownload: func(got *DownloadOutput) {
callbackMu.Lock()
numCallbacks++
Expand All @@ -113,7 +115,9 @@ func TestIntegration_DownloadDirectory(t *testing.T) {
t.Errorf("expected object size %d, got %d", want, got)
}

path := filepath.Join(localDir, got.Object)
objectWithoutPrefix, _ := strings.CutPrefix(got.Object, "dir/")
path := filepath.Join(localDir, objectWithoutPrefix)

f, err := os.Open(path)
if err != nil {
t.Errorf("os.Open(%q): %v", path, err)
Expand Down Expand Up @@ -156,7 +160,8 @@ func TestIntegration_DownloadDirectory(t *testing.T) {
t.Errorf("expected object size %d, got %d", want, got)
}

path := filepath.Join(localDir, got.Object)
objectWithoutPrefix, _ := strings.CutPrefix(got.Object, "dir/")
path := filepath.Join(localDir, objectWithoutPrefix)
f, err := os.Open(path)
if err != nil {
t.Errorf("os.Open(%q): %v", path, err)
Expand Down
Loading