[英]Why is AWS lambda function not returning jpg/png images when executed from netlify dev server?
I am creating a serverless function that accepts a URL as a query string and returns the image located at that URL.我正在创建一个无服务器 function,它接受 URL 作为查询字符串并返回位于该 URL 的图像。
package main
import (
"context"
"encoding/base64"
"fmt"
"io/ioutil"
"net/http"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)
func handler(ctx context.Context, request events.APIGatewayProxyRequest) (*events.APIGatewayProxyResponse, error) {
// Store query parameters in a variable
url := request.QueryStringParameters["url"]
// download image as save it as imageBytes
response, err := http.Get(url)
if err != nil {
return nil, fmt.Errorf("Could not connect to the provided url.")
}
defer response.Body.Close()
imageBytes, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, fmt.Errorf("Error occured.")
}
// convert to base64 string
imageBase64 := base64.StdEncoding.EncodeToString(imageBytes)
// send back response
return &events.APIGatewayProxyResponse{
StatusCode: http.StatusOK,
Headers: map[string]string{
"Content-Type": "image/png",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "Content-Type",
},
Body: imageBase64,
IsBase64Encoded: true,
}, nil
}
func main() {
lambda.Start(handler)
}
Additionally, I attempted to send the image bytes as a normal string without encoding it with base64, but that did not work either.此外,我尝试将图像字节作为普通字符串发送,而不使用 base64 对其进行编码,但这也不起作用。 Instead, all I am seeing is a white rectangular box.
相反,我所看到的只是一个白色的矩形框。 I'm using
netlify dev
command to execute this function.我正在使用
netlify dev
命令来执行这个 function。
What am I missing?我错过了什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.