Skip to content

Commit

Permalink
add list pagination logic
Browse files Browse the repository at this point in the history
- adjust upload.go accordingly
  • Loading branch information
aaperis committed Feb 28, 2025
1 parent 5e622b3 commit a143d84
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
32 changes: 24 additions & 8 deletions helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ func CheckTokenExpiration(accessToken string) error {
return nil
}

func ListFiles(config Config, prefix string) (result *s3.ListObjectsV2Output, err error) {
func ListFiles(config Config, prefix string) ([]*s3.Object, error) {
sess := session.Must(session.NewSession(&aws.Config{
// The region for the backend is always the specified one
// and not present in the configuration from auth - hardcoded
Expand All @@ -450,15 +450,31 @@ func ListFiles(config Config, prefix string) (result *s3.ListObjectsV2Output, er

svc := s3.New(sess)

result, err = svc.ListObjectsV2(&s3.ListObjectsV2Input{
Bucket: aws.String(config.AccessKey + "/"),
Prefix: aws.String(config.AccessKey + "/" + prefix),
})
if err != nil {
return nil, fmt.Errorf("failed to list objects, reason: %v", err)
var continuationToken *string
var fullResult []*s3.Object

for {
result, err := svc.ListObjectsV2(&s3.ListObjectsV2Input{
Bucket: aws.String(config.AccessKey + "/"),
Prefix: aws.String(config.AccessKey + "/" + prefix),
ContinuationToken: continuationToken,
})
if err != nil {
return nil, fmt.Errorf("failed to list objects, reason: %v", err)
}

if result.Contents != nil {
fullResult = append(fullResult, result.Contents...)
}

if result.NextContinuationToken == nil {
break
}

continuationToken = result.NextContinuationToken
}

return result, nil
return fullResult, nil
}

// Check for invalid characters
Expand Down
7 changes: 3 additions & 4 deletions list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"flag"
"fmt"
"strconv"

"strings"

"github.com/NBISweden/sda-cli/download"
Expand Down Expand Up @@ -100,9 +99,9 @@ func List(args []string, configPath string) error {
return err
}

for i := range result.Contents {
file := *result.Contents[i].Key
fmt.Printf("%s \t %s \n", bytesize.New(float64((*result.Contents[i].Size))), file[strings.Index(file, "/")+1:])
for i := range result {
file := *result[i].Key
fmt.Printf("%s \t %s \n", bytesize.New(float64((*result[i].Size))), file[strings.Index(file, "/")+1:])
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ func uploadFiles(files, outFiles []string, targetDir string, config *helpers.Con
if err != nil {
return fmt.Errorf("listing uploaded files: %s", err.Error())
}
if len(fileExists.Contents) > 0 {
if len(fileExists) > 0 {
if aws.StringValue(
fileExists.Contents[0].Key,
fileExists[0].Key,
) == filepath.Clean(
config.AccessKey+"/"+targetDir+"/"+outFiles[k],
) {
Expand Down

0 comments on commit a143d84

Please sign in to comment.