简体   繁体   English

为什么 AWS lambda function 在从 .netlify 开发服务器执行时不返回 jpg/png 图像?

[英]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?我错过了什么?

This is a known bug on netlify and it works AWS-Lambda.这是netlify上的一个已知错误,它有效 AWS-Lambda。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 返回音频文件作为来自 AWS 的响应 Lambda function - Returning an audio file as response from AWS Lambda function Selenium 仅在从 AWS 运行时返回“NoSuchElementException” lambda function(尽管路径有效) - Selenium returning "NoSuchElementException" ONLY when run from the AWS lambda function (and despite valid path) 从 AWS CodePipeline 调用 AWS Lambda function 时权限被拒绝 - Permission denied when calling AWS Lambda function from AWS CodePipeline TypeGraphQl:使用 Netlify 函数/AWS Lambda - TypeGraphQl: Usage with Netlify Functions/AWS Lambda AWS textract:PNG 和 JPG 图像的 UnsupportedDocumentException。 错误仅发生在生产中而不是本地 - AWS textract: UnsupportedDocumentException for PNG and JPG images. Error occurs only in production and not locally 为什么在通过 API 网关调用时,Java 中的 AWS Lambda 代码返回“内部服务器错误”? - why does this AWS Lambda code in Java return "internal server error" when invoked via an API gateway? 从 aws cli 部署到 lambda edge function - Deploy to lambda edge function from aws cli 来自 Lambda 的 AWS Secrets Manager 调用返回 null/none - AWS Secrets Manager call from Lambda returning null/none AWS Lambda 与 API 网关集成返回 200 status_code 即使调用 SageMaker 端点在 lambda 中没有返回响应 function - AWS Lambda with API Gateway Integration returning 200 status_code even when calling SageMaker Endpoint doesn't return a response in lambda function function 在从 aws 中的 s3 读取文件后将数据传递给 aws cognito 时退出 lambda 在 nodejs 中 - function exits when passing data to aws cognito after reading file from s3 in aws lambda in nodejs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM