write a lambda function in go for handling file uploads to s3. it should operate over http
- User
write a lambda function in go for handling file uploads to s3. it should operate over http
- Assistant
Certainly! Here's an example of a Lambda function in Go that handles file uploads to Amazon S3 over HTTP:
package main import ( "bytes" "context" "encoding/base64" "fmt" "net/http" "os" "github.com/aws/aws-lambda-go/events" "github.com/aws/aws-lambda-go/lambda" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/s3" ) func Handler(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) { fileData, err := base64.StdEncoding.DecodeString(request.Body) if err != nil { return events.APIGatewayProxyResponse{StatusCode: http.StatusBadRequest, Body: "Error decoding file data"}, nil } sess, err := session.NewSession(&aws.Config{ Region: aws.String(os.Getenv("AWS_REGION")), }) if err != nil { return events.APIGatewayProxyResponse{StatusCode: http.StatusInternalServerError, Body: "Error creating AWS session"}, nil } svc := s3.New(sess) input := &s3.PutObjectInput{ Bucket: aws.String("your-s3-bucket-name"), Key: aws.String("your-file-name"), Body: bytes.NewReader(fileData), } _, err = svc.PutObject(input) if err != nil { return events.APIGatewayProxyResponse{StatusCode: http.StatusInternalServerError, Body: "Error uploading file to S3"}, nil } return events.APIGatewayProxyResponse{StatusCode: http.StatusOK, Body: "File uploaded successfully"}, nil } func main() { lambda.Start(Handler) }In this example, the Lambda function takes an HTTP request with a base64-encoded file in the request body, decodes the file data, and then uploads it to the specified S3 bucket using the AWS SDK for Go. The function is triggered by an API Gateway proxy integration, allowing it to handle HTTP requests. Note that you will need to replace "your-s3-bucket-name" and "your-file-name" with your actual S3 bucket name and the desired file name.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.