aws-sdk-go-v2 icon indicating copy to clipboard operation
aws-sdk-go-v2 copied to clipboard

Blank PDF download via aws-sdk-go

Open hippiepaws opened this issue 2 years ago • 0 comments

Describe the bug

I'm trying to download a pdf saved on s3, serving it via a lambda written in go, with API Gateway on top of it. While the file on S3 has 15 pages and a lot of content, files being downloaded are coming out blank. The number of pages are recorded correctly, but the content is missing. The code looks like this:

func GetMenu(req events.APIGatewayProxyRequest, tableName string, dynaClient dynamodbiface.DynamoDBAPI, downloader *s3manager.Downloader) (
	*events.APIGatewayProxyResponse, error,
) {

bucket := "my_bucket"
	item := "file.pdf"
	file := aws.NewWriteAtBuffer([]byte{})
	_, err := downloader.Download(file,
		&s3.GetObjectInput{
			Bucket: aws.String(bucket),
			Key:    aws.String(item),
		})
	if err != nil {
		log.Fatalf("Unable to download item %q, %v", item, err)
	}
	file_bytes := file.Bytes()
	resp := events.APIGatewayProxyResponse{Headers: map[string]string{
		"Content-Type": "application/pdf", "Content-Length": strconv.Itoa(len(file_bytes)), "Content-disposition": "inline", "filename": item,
	}}
	resp.StatusCode = http.StatusOK
	resp.IsBase64Encoded = true

	resp.Body = base64.RawStdEncoding.EncodeToString(file.Bytes())
	return &resp, nil
}

Expected Behavior

The file should come with all the correct content

Current Behavior

File is either coming out to be an empty file or in a not supported PDF format when encoding it to base64, while keeping the API Gateway Binaries on.

Reproduction Steps

Put a file on S3 Write the above code for lambda and upload .zip file. Attach an API Gateway on top of it to trigger lambda. Hit the API Gateway url via a browser.

Possible Solution

Unable to figure it out

Additional Information/Context

No response

SDK version used

both v2 and v1

Environment details (Version of Go (go version)? OS name and version, etc.)

1.18

hippiepaws avatar Jan 23 '23 05:01 hippiepaws