Skip to content

Commit

Permalink
Add a flag to save downloaded S3 resource to a file (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop authored May 14, 2022
1 parent 41d0f59 commit 9bdbfe2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
4 changes: 4 additions & 0 deletions s3/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type Flags struct {
Bucket string
Key string
PathStyle bool

Save string
}

// AddCommand registers curl command into CLI app.
Expand All @@ -37,6 +39,8 @@ func AddCommand(lf *loadgen.Flags) {
s3.Flag("path-style", "To use path-style addressing, i.e., `http://s3.amazonaws.com/BUCKET/KEY`.").
BoolVar(&f.PathStyle)

s3.Flag("save", "Path to local file to save the entry.").StringVar(&f.Save)

s3.Action(func(kp *kingpin.ParseContext) error {
return run(*lf, f)
})
Expand Down
22 changes: 20 additions & 2 deletions s3/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package s3

import (
"fmt"
"io"
"os"
"strings"
"sync/atomic"
"time"
Expand Down Expand Up @@ -70,10 +72,26 @@ func (nopWriterAt) WriteAt(p []byte, off int64) (n int, err error) {
return len(p), nil
}

func (j *jobProducer) Job(_ int) (time.Duration, error) {
func (j *jobProducer) Job(i int) (time.Duration, error) {
start := time.Now()
w := io.WriterAt(nopWriterAt{})

n, err := j.dl.Download(nopWriterAt{}, &s3.GetObjectInput{
if i == 0 && j.f.Save != "" {
f, err := os.Create(j.f.Save)
if err != nil {
return 0, fmt.Errorf("failed to create file to save S3 result: %w", err)
}

w = f

defer func() {
if err := f.Close(); err != nil {
println("failed to close file:", err)
}
}()
}

n, err := j.dl.Download(w, &s3.GetObjectInput{
Bucket: aws.String(j.f.Bucket),
Key: aws.String(j.f.Key),
})
Expand Down

0 comments on commit 9bdbfe2

Please sign in to comment.