Skip to content

How to use io.ReadCloser as Body of LambdaFunctionURLStreamingResponse #590

Open
@phhphc

Description

@phhphc

Hi,

I want to use body from output of s3.Client.GetObject as body of LambdaFunctionURLStreamingResponse.
However, out.Body is io.ReadCloser, while LambdaFunctionURLStreamingResponse.Body is io.Reader.
I know that io.ReadCloser is compatible with io.Reader, but this would leave out.Body without being close.
How can I use body from output of s3 GetObject as body of LambdaFunctionURLStreamingResponse?

Below is the simplify version of my code

func handler(
	ctx context.Context,
	req events.LambdaFunctionURLRequest,
) (res *events.LambdaFunctionURLStreamingResponse, err error) {

	var s3Client *s3.Client
	// ...
	out, err := s3Client.GetObject(context.TODO(), &s3.GetObjectInput{
		Bucket: &bucket,
		Key:    &key,
	})
	if err != nil {
		return nil, err
	}

	resp := &events.LambdaFunctionURLStreamingResponse{
		StatusCode: http.StatusOK,
		Headers:    map[string]string{"Content-Type": "application/x-ndjson"},
	}

	// What is the secure way to do this ?
	resp.Body = out.Body

	return resp, nil
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions