|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftAWSLambdaRuntime open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2024 Apple Inc. and the SwiftAWSLambdaRuntime project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import AWSLambdaEvents |
| 16 | +import AWSLambdaRuntime |
| 17 | +import AsyncHTTPClient |
| 18 | + |
| 19 | +#if canImport(FoundationEssentials) |
| 20 | +import FoundationEssentials |
| 21 | +#else |
| 22 | +import Foundation |
| 23 | +#endif |
| 24 | + |
| 25 | +let httpClient = HTTPClient.shared |
| 26 | + |
| 27 | +enum LambdaError: Error { |
| 28 | + case noNotificationRecord |
| 29 | + case missingEnvVar(name: String) |
| 30 | + |
| 31 | + var description: String { |
| 32 | + switch self { |
| 33 | + case .noNotificationRecord: |
| 34 | + "No notification record in S3 event" |
| 35 | + case .missingEnvVar(let name): |
| 36 | + "Missing env var named \(name)" |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +let runtime = LambdaRuntime { (event: S3Event, context: LambdaContext) async throws -> APIGatewayV2Response in |
| 42 | + do { |
| 43 | + context.logger.debug("Received S3 event: \(event)") |
| 44 | + |
| 45 | + guard let s3NotificationRecord = event.records.first else { |
| 46 | + throw LambdaError.noNotificationRecord |
| 47 | + } |
| 48 | + |
| 49 | + let bucket = s3NotificationRecord.s3.bucket.name |
| 50 | + let key = s3NotificationRecord.s3.object.key.replacingOccurrences(of: "+", with: " ") |
| 51 | + |
| 52 | + guard let apiURL = ProcessInfo.processInfo.environment["API_URL"] else { |
| 53 | + throw LambdaError.missingEnvVar(name: "API_URL") |
| 54 | + } |
| 55 | + |
| 56 | + let body = """ |
| 57 | + { |
| 58 | + "bucket": "\(bucket)", |
| 59 | + "key": "\(key)" |
| 60 | + } |
| 61 | + """ |
| 62 | + |
| 63 | + context.logger.debug("Sending request to \(apiURL) with body \(body)") |
| 64 | + |
| 65 | + var request = HTTPClientRequest(url: "\(apiURL)/upload-complete/") |
| 66 | + request.method = .POST |
| 67 | + request.headers = [ |
| 68 | + "Content-Type": "application/json" |
| 69 | + ] |
| 70 | + request.body = .bytes(.init(string: body)) |
| 71 | + |
| 72 | + let response = try await httpClient.execute(request, timeout: .seconds(30)) |
| 73 | + return APIGatewayV2Response( |
| 74 | + statusCode: .ok, |
| 75 | + body: "Lambda terminated successfully. API responded with: Status: \(response.status), Body: \(response.body)" |
| 76 | + ) |
| 77 | + } catch let error as LambdaError { |
| 78 | + context.logger.error("\(error.description)") |
| 79 | + return APIGatewayV2Response(statusCode: .internalServerError, body: "[ERROR] \(error.description)") |
| 80 | + } catch { |
| 81 | + context.logger.error("\(error)") |
| 82 | + return APIGatewayV2Response(statusCode: .internalServerError, body: "[ERROR] \(error)") |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +try await runtime.run() |
0 commit comments