diff --git a/README.md b/README.md index dc546e9..e4e22de 100644 --- a/README.md +++ b/README.md @@ -28,11 +28,12 @@ A source webhook which uploads SorCam image to Soracom Harvest Files. curl \ -XGET \ -H "x-api-key:${apikey}" \ -"https://${hostname}/v1/source/soracam_image?device_id=${device_id}" +"https://${hostname}/v1/source/soracam_image?device_id=${device_id}&upload_directory=${directory}" ``` Query parameters * device_id: Soracam ID +* upload_directory: Upload target directory under base path. You will need SAM user with permissions below diff --git a/lambda/soracam-image-source.test.ts b/lambda/soracam-image-source.test.ts index d95dc1e..25a7dab 100644 --- a/lambda/soracam-image-source.test.ts +++ b/lambda/soracam-image-source.test.ts @@ -42,12 +42,17 @@ describe("Lambda handler", () => { expect(result.statusCode).toBe(400); expect(JSON.parse(result.body).message).toBe( - "device is required in querystring parameters" + "device_id and upload_directory are required in querystring parameters" ); }); it("should successfully process the request", async () => { - const event = { queryStringParameters: { device_id: "testDevice" } }; + const event = { + queryStringParameters: { + device_id: "testDevice", + upload_directory: "testDirectory", + }, + }; // Mock the Secrets Manager response secretsManagerMock.on(GetSecretValueCommand).resolves({ @@ -87,7 +92,7 @@ describe("Lambda handler", () => { soracomHttpClientMock .onPut( - /https:\/\/api.soracom.io\/v1\/files\/private\/test\/path\/testDevice-\d{14}.jpg/ + /https:\/\/api.soracom.io\/v1\/files\/private\/test\/path\/testDirectory\/testDevice-\d{14}.jpg/ ) .reply(201); diff --git a/lambda/soracam-image-source.ts b/lambda/soracam-image-source.ts index 8793f17..5eaa21b 100644 --- a/lambda/soracam-image-source.ts +++ b/lambda/soracam-image-source.ts @@ -4,18 +4,23 @@ import { getSoracomClient, setGetSoracomClient } from "./lib/utils"; export { setGetSoracomClient }; export const handler = async (event: any = {}): Promise => { - if (!event.queryStringParameters || !event.queryStringParameters.device_id) { + if ( + !event.queryStringParameters || + !event.queryStringParameters.device_id || + !event.queryStringParameters.upload_directory + ) { return { statusCode: 400, body: JSON.stringify({ - message: "device is required in querystring parameters", + message: + "device_id and upload_directory are required in querystring parameters", }), }; } const harvestfilesPath = process.env.HARVEST_FILES_PATH!; - const deviceId = event.queryStringParameters.device_id; + const uploadDirectory = event.queryStringParameters.upload_directory; const soracomClient = await getSoracomClient(); const time = Date.now() - 1000 * 10; @@ -42,7 +47,7 @@ export const handler = async (event: any = {}): Promise => { } await new Promise((resolve) => setTimeout(resolve, 1000)); } - let path = `${harvestfilesPath}/${deviceId}-${dayjs().format( + let path = `${harvestfilesPath}/${uploadDirectory}/${deviceId}-${dayjs().format( "YYYYMMDDHHmmss" )}.jpg`;