Lambda(node.js)でlistObjectsV2でファイル一覧を取得する(node.js v18) – AWS SDK for JavaScript v3
listObjectsV2メソッドは、AWS SDK for JavaScript v3ではListObjectsV2Commandになります。
import { S3Client, ListObjectsV2Command } from '@aws-sdk/client-s3' export const handler = async(event) => { const client = new S3Client({ region: 'ap-northeast-1' }) const input = { Bucket: 'バケット名', Prefix: 'tmp/', StartAfter: 'tmp/' } const command = new ListObjectsV2Command(input) const list = await client.send(command) console.log(list.Contents) // 配列 const response = { statusCode: 200, body: JSON.stringify('Hello from Lambda'), }; return response; }
Contentsは以下のような配列になります。
[ { Key: 'tmp/a.json', LastModified: 2023-01-06T05:50:30.000Z, ETag: '"ea703e7aa1efda0064eaa507d9e8ab7e"', ChecksumAlgorithm: undefined, Size: 4, StorageClass: 'STANDARD', Owner: undefined }, { Key: 'tmp/b.json', LastModified: 2023-01-06T05:56:12.000Z, ETag: '"ea703e7aa1efda0064eaa507d9e8ab7e"', ChecksumAlgorithm: undefined, Size: 4, StorageClass: 'STANDARD', Owner: undefined } ]
参考サイト
AWS SDK for JavaScript v3
API Reference
AWS SDK for JavaScript v3
API Reference
KHI入社して退社。今はCONFRAGEで正社員です。関西で140-170/80~120万から受け付けております^^
得意技はJS(ES6),Java,AWSの大体のリソースです
コメントはやさしくお願いいたします^^
座右の銘は、「狭き門より入れ」「願わくは、我に七難八苦を与えたまえ」です^^
コメント