Skip to content

Commit

Permalink
Fix S3 object requests
Browse files Browse the repository at this point in the history
The S3 bytes range is inclusive, so a request for byte range 0-8 will
return 9 bytes. This was not obvious. Update so that a request for N
bytes only returns N bytes.

Fixes puppetlabs-toy-chest#777.

Signed-off-by: Michael Smith <[email protected]>
  • Loading branch information
MikaelSmith committed Jun 17, 2020
1 parent 6c6c7f6 commit bef8676
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion plugin/aws/s3Object.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,16 @@ func (o *s3Object) Metadata(ctx context.Context) (plugin.JSONObject, error) {
}

func (o *s3Object) Read(ctx context.Context, size int64, offset int64) ([]byte, error) {
// Because bytes request is inclusive, short-circuit requests for 0 bytes.
if size <= 0 || offset < 0 {
return []byte{}, nil
}

// Bytes range is inclusive, so for N bytes get byte range 0-(N-1).
request := &s3Client.GetObjectInput{
Bucket: awsSDK.String(o.bucket),
Key: awsSDK.String(o.key),
Range: awsSDK.String("bytes=" + strconv.FormatInt(offset, 10) + "-" + strconv.FormatInt(offset+size, 10)),
Range: awsSDK.String("bytes=" + strconv.FormatInt(offset, 10) + "-" + strconv.FormatInt(offset+size-1, 10)),
}

resp, err := o.client.GetObjectWithContext(ctx, request)
Expand Down

0 comments on commit bef8676

Please sign in to comment.