emptygif
語言: 繁體中文

所有語言和框架

AWS Lambda (Go) Go

適用於 AWS Lambda (Go) 的 Go 程式碼片段,以空 GIF 回應對 /pixel.gif 的請求,GIF 在啟動時只解碼一次。

AWS Lambda (Go) Go

安裝
go get github.com/aws/aws-lambda-go
檔案
main.go
// Behind an API Gateway HTTP API route "GET /pixel.gif" (payload format 2.0) or a function URL.
package main

import (
	"context"

	"github.com/aws/aws-lambda-go/events"
	"github.com/aws/aws-lambda-go/lambda"
)

// Binary bodies travel base64-encoded, so the GIF stays in base64 here.
const gifBase64 = "R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="

func handler(ctx context.Context, req events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {
	return events.APIGatewayV2HTTPResponse{
		StatusCode:      200,
		Headers:         map[string]string{"Content-Type": "image/gif", "Cache-Control": "no-store"},
		Body:            gifBase64,
		IsBase64Encoded: true,
	}, nil
}

func main() {
	lambda.Start(handler)
}

我們執行了這段程式碼,並比對了它回傳的位元組。

驗證

curl -sI http://localhost:8080/pixel.gif

官方網站: AWS Lambda (Go)

更多 Go 範例