S3 Object Lambda allows you to add your own code to S3 GET requests to modify and process data as it is returned to an application. This example receives S3 Object Lambda event data and returns object metadata.
()
| 25 | // S3 Object Lambda allows you to add your own code to S3 GET requests to modify and process data |
| 26 | // as it is returned to an application. This example receives S3 Object Lambda event data and returns object metadata. |
| 27 | func ExampleS3ObjectLambdaEvent() { |
| 28 | lambda.Start(func(ctx context.Context, event *events.S3ObjectLambdaEvent) error { |
| 29 | url := event.GetObjectContext.InputS3URL |
| 30 | resp, err := http.Get(url) |
| 31 | if err != nil { |
| 32 | return err |
| 33 | } |
| 34 | defer resp.Body.Close() |
| 35 | bodyBytes, err := ioutil.ReadAll(resp.Body) |
| 36 | if err != nil { |
| 37 | return err |
| 38 | } |
| 39 | |
| 40 | type Metadata struct { |
| 41 | Length int |
| 42 | Md5 string |
| 43 | } |
| 44 | type TransformedObject struct { |
| 45 | Metadata Metadata |
| 46 | } |
| 47 | |
| 48 | transformedObject := TransformedObject{ |
| 49 | Metadata: Metadata{ |
| 50 | Length: len(bodyBytes), |
| 51 | Md5: toMd5(bodyBytes), |
| 52 | }, |
| 53 | } |
| 54 | jsonData, err := json.Marshal(transformedObject) |
| 55 | if err != nil { |
| 56 | return err |
| 57 | } |
| 58 | |
| 59 | // To complete the example, use the AWS SDK to write the response: |
| 60 | // |
| 61 | // import ( |
| 62 | // "github.com/aws/aws-sdk-go-v2/config" |
| 63 | // "github.com/aws/aws-sdk-go-v2/service/s3" |
| 64 | // ) |
| 65 | // |
| 66 | // cfg, err := config.LoadDefaultConfig(context.TODO()) |
| 67 | // if err != nil { |
| 68 | // return err |
| 69 | // } |
| 70 | // svc := s3.NewFromConfig(cfg) |
| 71 | // input := &s3.WriteGetObjectResponseInput{ |
| 72 | // RequestRoute: &event.GetObjectContext.OutputRoute, |
| 73 | // RequestToken: &event.GetObjectContext.OutputToken, |
| 74 | // Body: strings.NewReader(string(jsonData)), |
| 75 | // } |
| 76 | // res, err := svc.WriteGetObjectResponse(ctx, input) |
| 77 | // if err != nil { |
| 78 | // return err |
| 79 | // } |
| 80 | // fmt.Printf("%v", res) |
| 81 | |
| 82 | fmt.Printf("Transformed object metadata: %s\n", jsonData) |
| 83 | return nil |
| 84 | }) |