Skip to content

Commit be03e1d

Browse files
Fix: S3 wrap PutObject into ReadSeeker
1 parent 246ebde commit be03e1d

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

pkg/handler/forwarder/handler.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,28 @@ func (h *Handler) WriteSQS(ctx context.Context, r io.Reader) error {
109109
key = strings.Trim(h.DestinationURI.Path, "/") + "/" + key
110110
}
111111

112+
// Convert to seekable reader to allow AWS SDK to retry S3 PutObject operations
113+
// by rewinding the stream. If the reader is already a bytes.Buffer, convert it
114+
// directly to bytes.Reader to avoid an extra copy.
115+
var body io.ReadSeeker
116+
if r != nil {
117+
// Check if r is a *bytes.Buffer to avoid unnecessary copy
118+
if buf, ok := r.(*bytes.Buffer); ok {
119+
body = bytes.NewReader(buf.Bytes())
120+
} else {
121+
// For other io.Reader types, read into memory
122+
data, err := io.ReadAll(r)
123+
if err != nil {
124+
return fmt.Errorf("failed to read message data: %w", err)
125+
}
126+
body = bytes.NewReader(data)
127+
}
128+
}
129+
112130
_, err = h.S3Client.PutObject(ctx, &s3.PutObjectInput{
113131
Bucket: aws.String(h.DestinationURI.Host),
114132
Key: &key,
115-
Body: r,
133+
Body: body,
116134
ContentType: aws.String("application/x-aws-sqs"),
117135
})
118136
if err != nil {

0 commit comments

Comments
 (0)