質問編集履歴

9

コード修正

2023/10/19 01:29

投稿

akira777
akira777

スコア8

test CHANGED
File without changes
test CHANGED
@@ -22,19 +22,35 @@
22
22
 
23
23
  -----------------------------------------------------------------------
24
24
  V3のコード
25
- import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
25
+ import { DynamoDBClient, ScanCommand } from "@aws-sdk/client-dynamodb";
26
+ import { marshall, unmarshall } from "@aws-sdk/util-dynamodb";
26
- const client = new DynamoDBClient({ region: 'ap-northeast-1' })
27
+ const region = "ap-northeast-1";
27
28
  const tableName = 'PhoneNumberTable';
28
29
 
30
+ const dynamodbClient = new DynamoDBClient({ region: region });
31
+
29
32
  export const handler = async (event) => {
30
- // 発信者番号
33
+ // 発信者番号
31
- const phone = event.Details.ContactData.CustomerEndpoint.Address;
34
+ const phone = event.Details.ContactData.CustomerEndpoint.Address;
35
+
32
- // DynamoDBの一覧取得
36
+ // DynamoDBの一覧取得
33
- const db = await DynamoDBClient.scan({TableName: tableName});
34
- // 発信者番号が一覧に存在するかどうかの確認
35
- const result = db.Items.some( item => {
37
+ const scanParams = {
38
+ TableName: tableName,
39
+ };
36
40
 
41
+ try {
42
+ const scanCommand = new ScanCommand(scanParams);
43
+ const data = await dynamodbClient.send(scanCommand);
44
+ const db = unmarshall(data.Items);
45
+
46
+ // 発信者番号が一覧に存在するかどうかの確認
47
+ const result = db.some(item => {
37
- return (item.PhoneNumber == phone);
48
+ return (item.PhoneNumber === phone);
38
- });
49
+ });
50
+
39
- return { result : result };
51
+ return { result: result };
52
+ } catch (error) {
53
+ console.error("Error scanning DynamoDB table:", error);
54
+ throw error;
55
+ }
40
56
  };

8

コード修正

2023/10/10 07:21

投稿

akira777
akira777

スコア8

test CHANGED
File without changes
test CHANGED
@@ -1,152 +1,40 @@
1
1
  Lambdaで下記のV2からV3のコードに変更しましたが正常に動作しません。
2
2
  ご教示いただければ幸いです。
3
3
 
4
- エラー内容は下記となります。
5
- 2023-09-21T05:51:46.332Z undefined ERROR Uncaught Exception {
6
- "errorType": "Runtime.UserCodeSyntaxError",
7
- "errorMessage": "SyntaxError: Named export 'createRequire' not found. The requested module '@aws-sdk/client-s3' is a CommonJS module, which may not support all module.exports as named exports.\nCommonJS modules can always be imported via the default export, for example using:\n\nimport pkg from '@aws-sdk/client-s3';\nconst { createRequire } = pkg;\n",
8
- "stack": [
9
- "Runtime.UserCodeSyntaxError: SyntaxError: Named export 'createRequire' not found. The requested module '@aws-sdk/client-s3' is a CommonJS module, which may not support all module.exports as named exports.",
10
- "CommonJS modules can always be imported via the default export, for example using:",
11
- "",
12
- "import pkg from '@aws-sdk/client-s3';",
13
- "const { createRequire } = pkg;",
14
- "",
15
- " at _loadUserApp (file:///var/runtime/index.mjs:1058:17)",
16
- " at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1093:21)",
17
- " at async start (file:///var/runtime/index.mjs:1256:23)",
18
- " at async file:///var/runtime/index.mjs:1262:1"
19
- ]
20
- }
21
4
  -----------------------------------------------------------------------
22
5
  V2のコード
23
- // AWSのSDKを使う宣言
24
- const aws_sdk = require('aws-sdk');
6
+ const AWS = require("aws-sdk");
25
- // S3を使う宣言
26
- const s3 = new aws_sdk.S3();
27
- const bucket = 'abcd-test01-system';
28
- // フォルダが階層の場合は"/"で連結 例:folder1/folder2/folder3/
7
+ const DynamoDB = new AWS.DynamoDB.DocumentClient({ region: "ap-northeast-1" });
29
- // 最後は"/"で終了する
30
- const key = 'calendar/';
8
+ const tableName = 'PhoneNumberTable';
31
9
 
32
- exports.handler = async function(event, context) {
10
+ exports.handler = async (event) => {
11
+ // 発信者番号
12
+ const phone = event.Details.ContactData.CustomerEndpoint.Address;
13
+ // DynamoDBの一覧取得
14
+ const db = await DynamoDB.scan({TableName: tableName}).promise();
15
+ // 発信者番号が一覧に存在するかどうかの確認
16
+ const result = db.Items.some( item => {
33
17
 
34
- console.log(JSON.stringify(event))
35
-
36
- // オペレーション時間の取得
37
- // objectNameはAmazon Connectで指定した値を受け取ります。
38
- // 拡張子も含めて指定 例:operation-time.txt
39
- // s3にGetObject関数でアクセスするのでこの権限をLambdaに付与する必要がある。
40
- const data = await s3.getObject({Bucket: bucket,Key: key + event.Details.Parameters.objectName}).promise();
41
- const operationTime = data.Body.toString();
42
- var lines = operationTime.split('\n');
43
-
44
- // コメント削除及び、余分な空白削除
45
- lines = lines.map( line => {
18
+ return (item.PhoneNumber == phone);
46
- return line.replace(/#[\s\S]*$/g, '').replace(/\s+$/g, '');
47
- });
19
+ });
48
-
49
- // 無効(空白)行の削除
50
- lines = lines.filter( line => {
51
- return line != '';
20
+ return { result : result };
52
- });
21
+ };
53
-
54
- // 時間内かどうかのチェック
55
- const IsOperationTime = CheckInTime(lines);
56
-
57
- return { IsOperationTime: IsOperationTime };
58
- }
59
-
60
- function CheckInTime(lines) {
61
- // 現在時間
62
- const now = new Date();
63
- const month = now.getMonth() + 1;
64
- const day = now.getDate();
65
-
66
- // 祝日指定の抽出
67
- const holidays = lines.filter(line => {
68
- return line.split(',')[0].split('/').length == 2;
69
- });
70
-
71
- // 祝日チェック
72
- let flg = true;
73
- holidays.forEach( line => {
74
- const tmp = line.split(',');
75
- const date = tmp[0].split('/');
76
- if(date.length == 2){
77
- if(month == date[0] && day == date[1]){
78
- flg = false;
79
- }
80
- }
81
- })
82
- return flg;
83
- }
84
22
 
85
23
  -----------------------------------------------------------------------
86
24
  V3のコード
87
- import { createRequire } from '@aws-sdk/client-s3';
25
+ import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
26
+ const client = new DynamoDBClient({ region: 'ap-northeast-1' })
88
- const require = createRequire(import.meta.url);
27
+ const tableName = 'PhoneNumberTable';
89
28
 
90
- // AWSのSDKを使う宣言
91
- const {S3} = require('@aws-sdk/client-s3');
29
+ export const handler = async (event) => {
92
- // S3を使う宣言
30
+ // 発信者番号
31
+ const phone = event.Details.ContactData.CustomerEndpoint.Address;
93
- const s3Client = new S3({});
32
+ // DynamoDBの一覧取得
33
+ const db = await DynamoDBClient.scan({TableName: tableName});
34
+ // 発信者番号が一覧に存在するかどうかの確認
94
- const bucket = 'abcd-test01-system';
35
+ const result = db.Items.some( item => {
95
- // フォルダが階層の場合は"/"で連結 例:folder1/folder2/folder3/
96
- // 最後は"/"で終了する
97
- const key = 'calendar/';
98
36
 
99
- export const handler = async (event, context) => {
100
-
101
- console.log(JSON.stringify(event))
102
-
103
- // オペレーション時間の取得
104
- // objectNameはAmazon Connectで指定した値を受け取ります。
105
- // 拡張子も含めて指定 例:operation-time.txt
106
- // s3にGetObject関数でアクセスするのでこの権限をLambdaに付与する必要がある。
107
- const data = await s3.send(new GetObjectCommand({
108
- Bucket: bucket,Key: key + event.Details.Parameters.objectName
109
- }));
110
- const operationTime = data.Body.toString();
111
- var lines = operationTime.split('\n');
112
-
113
- // コメント削除及び、余分な空白削除
114
- lines = lines.map( line => {
37
+ return (item.PhoneNumber == phone);
115
- return line.replace(/#[\s\S]*$/g, '').replace(/\s+$/g, '');
116
- });
38
+ });
117
-
118
- // 無効(空白)行の削除
119
- lines = lines.filter( line => {
120
- return line != '';
39
+ return { result : result };
121
- });
40
+ };
122
-
123
- // 時間内かどうかのチェック
124
- const IsOperationTime = CheckInTime(lines);
125
-
126
- return { IsOperationTime: IsOperationTime };
127
- }
128
-
129
- function CheckInTime(lines) {
130
- // 現在時間
131
- const now = new Date();
132
- const month = now.getMonth() + 1;
133
- const day = now.getDate();
134
-
135
- // 祝日指定の抽出
136
- const holidays = lines.filter(line => {
137
- return line.split(',')[0].split('/').length == 2;
138
- });
139
-
140
- // 祝日チェック
141
- let flg = true;
142
- holidays.forEach( line => {
143
- const tmp = line.split(',');
144
- const date = tmp[0].split('/');
145
- if(date.length == 2){
146
- if(month == date[0] && day == date[1]){
147
- flg = false;
148
- }
149
- }
150
- })
151
- return flg;
152
- }

7

コードの変更

2023/09/21 08:18

投稿

akira777
akira777

スコア8

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,4 @@
1
- Lambdaで下記のV3のコードに変更しましたが正常に動作しません。
1
+ Lambdaで下記のV2からV3のコードに変更しましたが正常に動作しません。
2
2
  ご教示いただければ幸いです。
3
3
 
4
4
  エラー内容は下記となります。

6

コードの修正

2023/09/21 08:15

投稿

akira777
akira777

スコア8

test CHANGED
File without changes
test CHANGED
@@ -2,14 +2,21 @@
2
2
  ご教示いただければ幸いです。
3
3
 
4
4
  エラー内容は下記となります。
5
- 2023-08-31T01:31:53.751Z undefined ERROR Uncaught Exception {
6
- "errorType": "ReferenceError",
7
- "errorMessage": "require is not defined in ES module scope, you can use import instead",
8
- "stack": [
9
- "ReferenceError: require is not defined in ES module scope, you can use import instead",
10
- " at file:///var/task/index.mjs:2:14",
11
- " at ModuleJob.run (node:internal/modules/esm/module_job:194:25)"
12
- ]
5
+ 2023-09-21T05:51:46.332Z undefined ERROR Uncaught Exception {
6
+ "errorType": "Runtime.UserCodeSyntaxError",
7
+ "errorMessage": "SyntaxError: Named export 'createRequire' not found. The requested module '@aws-sdk/client-s3' is a CommonJS module, which may not support all module.exports as named exports.\nCommonJS modules can always be imported via the default export, for example using:\n\nimport pkg from '@aws-sdk/client-s3';\nconst { createRequire } = pkg;\n",
8
+ "stack": [
9
+ "Runtime.UserCodeSyntaxError: SyntaxError: Named export 'createRequire' not found. The requested module '@aws-sdk/client-s3' is a CommonJS module, which may not support all module.exports as named exports.",
10
+ "CommonJS modules can always be imported via the default export, for example using:",
11
+ "",
12
+ "import pkg from '@aws-sdk/client-s3';",
13
+ "const { createRequire } = pkg;",
14
+ "",
15
+ " at _loadUserApp (file:///var/runtime/index.mjs:1058:17)",
16
+ " at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1093:21)",
17
+ " at async start (file:///var/runtime/index.mjs:1256:23)",
18
+ " at async file:///var/runtime/index.mjs:1262:1"
19
+ ]
13
20
  }
14
21
  -----------------------------------------------------------------------
15
22
  V2のコード
@@ -77,7 +84,7 @@
77
84
 
78
85
  -----------------------------------------------------------------------
79
86
  V3のコード
80
- import { createRequire } from 'module';
87
+ import { createRequire } from '@aws-sdk/client-s3';
81
88
  const require = createRequire(import.meta.url);
82
89
 
83
90
  // AWSのSDKを使う宣言
@@ -89,7 +96,7 @@
89
96
  // 最後は"/"で終了する
90
97
  const key = 'calendar/';
91
98
 
92
- exports.handler = async function(event, context) {
99
+ export const handler = async (event, context) => {
93
100
 
94
101
  console.log(JSON.stringify(event))
95
102
 

5

コードの追加

2023/09/19 08:01

投稿

akira777
akira777

スコア8

test CHANGED
File without changes
test CHANGED
@@ -77,6 +77,9 @@
77
77
 
78
78
  -----------------------------------------------------------------------
79
79
  V3のコード
80
+ import { createRequire } from 'module';
81
+ const require = createRequire(import.meta.url);
82
+
80
83
  // AWSのSDKを使う宣言
81
84
  const {S3} = require('@aws-sdk/client-s3');
82
85
  // S3を使う宣言

4

コード修正

2023/09/08 02:24

投稿

akira777
akira777

スコア8

test CHANGED
File without changes
test CHANGED
@@ -94,7 +94,9 @@
94
94
  // objectNameはAmazon Connectで指定した値を受け取ります。
95
95
  // 拡張子も含めて指定 例:operation-time.txt
96
96
  // s3にGetObject関数でアクセスするのでこの権限をLambdaに付与する必要がある。
97
+ const data = await s3.send(new GetObjectCommand({
97
- const data = await s3Client.getObject({Bucket: bucket,Key: key + event.Details.Parameters.objectName}).promise();
98
+ Bucket: bucket,Key: key + event.Details.Parameters.objectName
99
+ }));
98
100
  const operationTime = data.Body.toString();
99
101
  var lines = operationTime.split('\n');
100
102
 

3

タグを追加

2023/08/31 02:02

投稿

akira777
akira777

スコア8

test CHANGED
File without changes
test CHANGED
File without changes

2

エラーとコード全文追加

2023/08/31 01:59

投稿

akira777
akira777

スコア8

test CHANGED
File without changes
test CHANGED
@@ -1,15 +1,140 @@
1
1
  Lambdaで下記のV3のコードに変更しましたが正常に動作しません。
2
2
  ご教示いただければ幸いです。
3
3
 
4
+ エラー内容は下記となります。
5
+ 2023-08-31T01:31:53.751Z undefined ERROR Uncaught Exception {
6
+ "errorType": "ReferenceError",
7
+ "errorMessage": "require is not defined in ES module scope, you can use import instead",
8
+ "stack": [
9
+ "ReferenceError: require is not defined in ES module scope, you can use import instead",
10
+ " at file:///var/task/index.mjs:2:14",
11
+ " at ModuleJob.run (node:internal/modules/esm/module_job:194:25)"
12
+ ]
13
+ }
14
+ -----------------------------------------------------------------------
4
- V2
15
+ V2のコード
5
16
  // AWSのSDKを使う宣言
6
17
  const aws_sdk = require('aws-sdk');
7
18
  // S3を使う宣言
8
19
  const s3 = new aws_sdk.S3();
20
+ const bucket = 'abcd-test01-system';
21
+ // フォルダが階層の場合は"/"で連結 例:folder1/folder2/folder3/
22
+ // 最後は"/"で終了する
23
+ const key = 'calendar/';
9
24
 
25
+ exports.handler = async function(event, context) {
10
26
 
27
+ console.log(JSON.stringify(event))
28
+
29
+ // オペレーション時間の取得
30
+ // objectNameはAmazon Connectで指定した値を受け取ります。
31
+ // 拡張子も含めて指定 例:operation-time.txt
32
+ // s3にGetObject関数でアクセスするのでこの権限をLambdaに付与する必要がある。
33
+ const data = await s3.getObject({Bucket: bucket,Key: key + event.Details.Parameters.objectName}).promise();
34
+ const operationTime = data.Body.toString();
35
+ var lines = operationTime.split('\n');
36
+
37
+ // コメント削除及び、余分な空白削除
38
+ lines = lines.map( line => {
39
+ return line.replace(/#[\s\S]*$/g, '').replace(/\s+$/g, '');
40
+ });
41
+
42
+ // 無効(空白)行の削除
43
+ lines = lines.filter( line => {
44
+ return line != '';
45
+ });
46
+
47
+ // 時間内かどうかのチェック
48
+ const IsOperationTime = CheckInTime(lines);
49
+
50
+ return { IsOperationTime: IsOperationTime };
51
+ }
52
+
53
+ function CheckInTime(lines) {
54
+ // 現在時間
55
+ const now = new Date();
56
+ const month = now.getMonth() + 1;
57
+ const day = now.getDate();
58
+
59
+ // 祝日指定の抽出
60
+ const holidays = lines.filter(line => {
61
+ return line.split(',')[0].split('/').length == 2;
62
+ });
63
+
64
+ // 祝日チェック
65
+ let flg = true;
66
+ holidays.forEach( line => {
67
+ const tmp = line.split(',');
68
+ const date = tmp[0].split('/');
69
+ if(date.length == 2){
70
+ if(month == date[0] && day == date[1]){
71
+ flg = false;
72
+ }
73
+ }
11
- V3
74
+ })
75
+ return flg;
76
+ }
77
+
78
+ -----------------------------------------------------------------------
79
+ V3のコード
12
80
  // AWSのSDKを使う宣言
13
81
  const {S3} = require('@aws-sdk/client-s3');
14
82
  // S3を使う宣言
83
+ const s3Client = new S3({});
15
- const s3 = new S3({region: 'ap-northeast-1'});
84
+ const bucket = 'abcd-test01-system';
85
+ // フォルダが階層の場合は"/"で連結 例:folder1/folder2/folder3/
86
+ // 最後は"/"で終了する
87
+ const key = 'calendar/';
88
+
89
+ exports.handler = async function(event, context) {
90
+
91
+ console.log(JSON.stringify(event))
92
+
93
+ // オペレーション時間の取得
94
+ // objectNameはAmazon Connectで指定した値を受け取ります。
95
+ // 拡張子も含めて指定 例:operation-time.txt
96
+ // s3にGetObject関数でアクセスするのでこの権限をLambdaに付与する必要がある。
97
+ const data = await s3Client.getObject({Bucket: bucket,Key: key + event.Details.Parameters.objectName}).promise();
98
+ const operationTime = data.Body.toString();
99
+ var lines = operationTime.split('\n');
100
+
101
+ // コメント削除及び、余分な空白削除
102
+ lines = lines.map( line => {
103
+ return line.replace(/#[\s\S]*$/g, '').replace(/\s+$/g, '');
104
+ });
105
+
106
+ // 無効(空白)行の削除
107
+ lines = lines.filter( line => {
108
+ return line != '';
109
+ });
110
+
111
+ // 時間内かどうかのチェック
112
+ const IsOperationTime = CheckInTime(lines);
113
+
114
+ return { IsOperationTime: IsOperationTime };
115
+ }
116
+
117
+ function CheckInTime(lines) {
118
+ // 現在時間
119
+ const now = new Date();
120
+ const month = now.getMonth() + 1;
121
+ const day = now.getDate();
122
+
123
+ // 祝日指定の抽出
124
+ const holidays = lines.filter(line => {
125
+ return line.split(',')[0].split('/').length == 2;
126
+ });
127
+
128
+ // 祝日チェック
129
+ let flg = true;
130
+ holidays.forEach( line => {
131
+ const tmp = line.split(',');
132
+ const date = tmp[0].split('/');
133
+ if(date.length == 2){
134
+ if(month == date[0] && day == date[1]){
135
+ flg = false;
136
+ }
137
+ }
138
+ })
139
+ return flg;
140
+ }

1

文法の修正

2023/08/25 05:54

投稿

akira777
akira777

スコア8

test CHANGED
File without changes
test CHANGED
@@ -7,7 +7,7 @@
7
7
  // S3を使う宣言
8
8
  const s3 = new aws_sdk.S3();
9
9
 
10
- **ボールドテキスト**
10
+
11
11
  V3
12
12
  // AWSのSDKを使う宣言
13
13
  const {S3} = require('@aws-sdk/client-s3');