-
Notifications
You must be signed in to change notification settings - Fork 1
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
Support S3 Objects List ETag map #126
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ import ( | |
type AwsOps interface { | ||
BucketDirs(bucket, s3Prefix string) ([]string, error) | ||
BucketObjects(bucket, s3Prefix string, concurrency int, recursive bool, reg *regexp.Regexp) ([]string, error) | ||
BucketObjectsETagMap(bucket, s3Prefix string, concurrency int, recursive bool, reg *regexp.Regexp) (map[string]string, error) | ||
} | ||
|
||
type DefaultAwsOps struct { | ||
|
@@ -157,6 +158,114 @@ func (a *DefaultAwsOps) BucketObjects(bucket, s3Prefix string, concurrency int, | |
} | ||
} | ||
|
||
func (a *DefaultAwsOps) BucketObjectsETagMap(bucket, s3Prefix string, concurrency int, recursive bool, reg *regexp.Regexp) (map[string]string, error) { | ||
svc := s3.New(a.sess) | ||
|
||
nextMarkerChan := make(chan string, 100) | ||
nextMarkerChan <- "" | ||
defer close(nextMarkerChan) | ||
|
||
binaryMeta := make(map[string]string) | ||
binaryL := sync.Mutex{} | ||
|
||
wg := sync.WaitGroup{} | ||
waitCh := make(chan struct{}) | ||
wg.Add(1) | ||
|
||
var ops uint64 | ||
var total uint64 | ||
|
||
errCh := make(chan error) | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
// If recursive is not true, include a delimiter so we only list the contents | ||
// of the directory indicated by `s3Prefix`. Otherwise, leave the delimiter nil | ||
// so we list everything recursively. | ||
var delimiter *string | ||
if !recursive { | ||
delimiter = aws.String("/") | ||
} | ||
|
||
go func() { | ||
for i := 0; i < concurrency; i++ { | ||
go func() { | ||
for nextMarker := range nextMarkerChan { | ||
wg.Add(1) | ||
|
||
query := &s3.ListObjectsInput{ | ||
Bucket: aws.String(bucket), | ||
Prefix: aws.String(s3Prefix), | ||
Delimiter: delimiter, | ||
} | ||
|
||
if nextMarker != "" { | ||
query.SetMarker(nextMarker) | ||
} | ||
|
||
resp, err := svc.ListObjectsWithContext(ctx, query) | ||
if err != nil { | ||
errCh <- fmt.Errorf("something went wrong listing objects: %s", err) | ||
return | ||
} | ||
|
||
nm := "" | ||
|
||
if resp.NextMarker != nil { | ||
nm = *resp.NextMarker | ||
nextMarkerChan <- nm | ||
} | ||
|
||
// When there are no contents, we need to return | ||
// early. | ||
if len(resp.Contents) == 0 { | ||
wg.Done() | ||
// TODO: `nm` may always be blank when there are no | ||
// contents, so this conditional may be unnecessary. | ||
if nm == "" { | ||
wg.Done() | ||
} | ||
return | ||
} | ||
|
||
bm := getObjectsAllMap(resp, s3Prefix, reg) | ||
|
||
binaryL.Lock() | ||
for key, val := range bm { | ||
binaryMeta[key] = val | ||
} | ||
binaryL.Unlock() | ||
|
||
wg.Done() | ||
atomic.AddUint64(&ops, uint64(len(bm))) | ||
if ops > 1000 { | ||
atomic.AddUint64(&total, atomic.LoadUint64(&ops)) | ||
log.Printf("For S3 prefix %s parsed %d files", s3Prefix, atomic.LoadUint64(&total)) | ||
atomic.SwapUint64(&ops, 0) | ||
} | ||
|
||
if nm == "" { | ||
wg.Done() | ||
break | ||
} | ||
} | ||
}() | ||
} | ||
|
||
wg.Wait() | ||
close(waitCh) | ||
}() | ||
|
||
// Block until the wait group is done or we err | ||
select { | ||
case <-waitCh: | ||
return binaryMeta, nil | ||
case err := <-errCh: | ||
cancel() | ||
return nil, err | ||
} | ||
} | ||
|
||
var BinaryReg = regexp.MustCompile(`(.+)(\.tar\.gz|\.zip)$`) | ||
|
||
func getObjectsAll(bucketObjectsList *s3.ListObjectsOutput, s3Prefix string, reg *regexp.Regexp) []string { | ||
|
@@ -176,3 +285,21 @@ func getObjectsAll(bucketObjectsList *s3.ListObjectsOutput, s3Prefix string, reg | |
|
||
return binaryMeta | ||
} | ||
|
||
func getObjectsAllMap(bucketObjectsList *s3.ListObjectsOutput, s3Prefix string, reg *regexp.Regexp) map[string]string { | ||
binaryMeta := make(map[string]string) | ||
|
||
for _, key := range bucketObjectsList.Contents { | ||
|
||
if reg != nil { | ||
if s := reg.FindStringSubmatch(*key.Key); len(s) > 1 { | ||
binaryMeta[strings.TrimPrefix(s[1], s3Prefix)] = *key.ETag | ||
} | ||
} else { | ||
binaryMeta[strings.TrimPrefix(*key.Key, s3Prefix)] = *key.ETag | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method is nearly identical to the existing |
||
} | ||
|
||
} | ||
|
||
return binaryMeta | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the only line that differs materially from
BucketObjects
. It calls the newgetObjectsAllMap
method instead ofgetObjectsAll
.