Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-1106· OpenAIHugging Face Arena Human Preference 55K

write a lambda function in go for handling file uploads to s3. it should operate over http

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Metallama-2-13b-chat’s answer.
  1. User

    write a lambda function in go for handling file uploads to s3. it should operate over http

  2. 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.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.