{"id":3791,"date":"2023-01-01T15:15:42","date_gmt":"2023-01-01T06:15:42","guid":{"rendered":"https:\/\/dennie.tokyo\/it\/?p=3791"},"modified":"2023-01-01T15:17:26","modified_gmt":"2023-01-01T06:17:26","slug":"aws-s3-node-js-%e3%82%aa%e3%83%96%e3%82%b8%e3%82%a7%e3%82%af%e3%83%88%e5%8f%96%e5%be%97","status":"publish","type":"post","link":"https:\/\/dennie.tokyo\/it\/?p=3791","title":{"rendered":"AWS S3 node js \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u53d6\u5f97"},"content":{"rendered":"\n<p>AWS S3 \u30d0\u30b1\u30c3\u30c8\u304b\u3089\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u53d6\u5f97\u3057\u307e\u3059\u3002<br>node js \u306b\u3066 AWS SDK\u3092\u5229\u7528\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<!--more-->\n\n\n\n<h1 class=\"wp-block-heading\">\u524d\u63d0<\/h1>\n\n\n\n<p>AWS SDK \u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u3066\u4e0b\u3055\u3044\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>npm install @aws-sdk\/client-s3<\/code><\/pre><\/div>\n\n\n\n<h1 class=\"wp-block-heading\">\u30d5\u30a1\u30a4\u30eb\u69cb\u6210<\/h1>\n\n\n\n<p>index.mjs \u304b\u3089 GetListObject.mjs \u3092\u547c\u3073\u51fa\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">GetListObject.mjs<\/h2>\n\n\n\n<p>GetListObject \u306f S3\u30d0\u30b1\u30c3\u30c8\u304b\u3089\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u53d6\u5f97\u3057\u307e\u3059\u3002<br>node\u3092\u4f7f\u7528\u3059\u308b\u306e\u3067 AWS SDK for javascript \u3092\u4f7f\u7528\u3057\u307e\u3059\u3002<br>ListObjectsV2Command\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-js\" data-file=\"GetListObject.mjs\" data-lang=\"JavaScript\"><code>import { S3Client, ListObjectsV2Command} from &quot;@aws-sdk\/client-s3&quot;;\n\nclass GetListObject {\n\n    #client;\n\n    constructor() {\n\n        \/\/ S3 \u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u3092\u751f\u6210\u3059\u308b\u3002\n        const S3ClientInput = {\n            region: &#39;ap-northeast-1&#39;,\n            credentials: {\n                accessKeyId: &#39;\u30a2\u30af\u30bb\u30b9\u30ad\u30fc&#39;,\n                secretAccessKey:&#39;\u30b7\u30fc\u30af\u30ec\u30c3\u30c8\u30a2\u30af\u30bb\u30b9\u30ad\u30fc&#39;,\n            },\n        }\n        this.#client = new S3Client(S3ClientInput);\n    }\n\n    async exec(prefix = null, continuationToken = null) {\n\n        \/\/ S3\u304b\u3089\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u53d6\u5f97\u3059\u308b\u70ba\u306e\u3001\u6e96\u5099\u3092\u884c\u3046\u3002\n        const input  = {\n            Bucket: &#39;testdennie01&#39;,\n            Prefix: prefix,\n            ContinuationToken: continuationToken,\n        };\n        const command = new ListObjectsV2Command(input);\n\n        \/\/ S3\u304b\u3089\u306e\u53d6\u5f97\u7d50\u679c\u3092\u521d\u671f\u5316\u3059\u308b\u3002\n        let results = {\n            result: true,\n            data: null,\n            continue: false,\n        };\n\n        \/\/ S3\u304b\u3089\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u53d6\u5f97\u3059\u308b\u3002\n        let s3data = {};\n        try {\n            s3data = await this.#client.send(command);\n         } catch (e) {\n            results.result = false;\n            return results;\n         }\n\n         \/\/ \u53d6\u5f97\u7d50\u679c(\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u4e00\u89a7\u60c5\u5831)\u3092\u53d6\u5f97\u3059\u308b\u3002\n         if(&#39;Contents&#39; in s3data) {\n            results.data = s3data.Contents;\n         } else {\n            results.result = false;\n            return results;\n         }\n\n         \/\/ \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c1000\u4ef6\u4ee5\u4e0a\u5b58\u5728\u3059\u308b\u5834\u5408\u3001\u4ee5\u4e0b\u3092\u884c\u3046\u3002\n         if(&#39;IsTruncated&#39; in s3data) {\n            if(s3data.IsTruncated){\n                results.continue = true;\n                results.ContinuationToken = s3data.NextContinuationToken;\n            }\n        }\n        return results;\n    }\n}\n\nexport default new GetListObject();\n<\/code><\/pre><\/div>\n\n\n\n<p>ListObjectsV2Command\u306f1000\u4ef6\u307e\u3067\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u53d6\u5f97\u3057\u307e\u3059\u3002<br>1000\u4ef6\u4ee5\u4e0a\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u5b58\u5728\u3059\u308b\u5834\u5408\u3001ListObjectsV2Command\u306e\u30ec\u30b9\u30dd\u30f3\u30b9\u306e IsTruncated \u304c true \u306b\u306a\u308a\u307e\u3059\u3002<br>\u3053\u306e\u5834\u5408\u3001NextContinuationToken \u306b\u30c8\u30fc\u30af\u30f3\u304c\u8a2d\u5b9a\u3055\u308c\u308b\u306e\u3067\u3001\u3053\u308c\u3092\u4f7f\u7528\u3057\u3066\u518d\u5ea6\u30d0\u30b1\u30c3\u30c8\u304b\u3089\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u53d6\u5f97\u3057\u306b\u3044\u304f\u3068\u672a\u53d6\u5f97\u306e\u7b87\u6240\u304b\u3089\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u53d6\u5f97\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">index.mjs<\/h2>\n\n\n\n<p>GetListObject\u3092\u547c\u3073\u51fa\u3057\u307e\u3059\u3002\u672a\u53d6\u5f97\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u5b58\u5728\u3059\u308b\u5834\u5408\u3001\u518d\u5ea6GetListObject\u3092\u547c\u3073\u51fa\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-ts\" data-lang=\"TypeScript\"><code>import GetListObject from &#39;.\/GetListObject.mjs&#39;\n\nconst prefix = null;\nlet reTryCnt = 0;\nlet continuationToken = null;\nwhile (reTryCnt &lt;= 2) {\n\n    \/\/ S3\u304b\u3089\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u53d6\u5f97\u3059\u308b\u3002\n    console.log(&quot;S3\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u53d6\u5f97\u958b\u59cb&quot; + reTryCnt + &quot;\u56de\u76ee\u3002&quot;);\n    let results = await GetListObject.exec(prefix, continuationToken);\n    console.log(&quot;S3\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u53d6\u5f97\u7d42\u4e86&quot; + reTryCnt + &quot;\u56de\u76ee\u3002&quot;);\n\n    \/\/ \u5b9f\u884c\u7d50\u679c\u304c\u30a8\u30e9\u30fc\u306e\u5834\u5408\u3001\u3082\u3046\u4e00\u5ea6S3\u304b\u3089\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u53d6\u5f97\u3059\u308b\u3002\n    if(!results.result){\n        console.log(&#39;S3\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u53d6\u5f97\u306b\u5931\u6557\u3057\u307e\u3057\u305f\u3002&#39;);\n        reTryCnt++;\n        continue;\n    }\n\n    \/\/ \u5b9f\u884c\u7d50\u679c\u304c\u53d6\u5f97\u3067\u304d\u305f\u306e\u3067\u3001\u30ea\u30c8\u30e9\u30a4\u30ab\u30a6\u30f3\u30c8\u3092\u30ea\u30bb\u30c3\u30c8\u3059\u308b\u3002\n    console.log(&#39;S3\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u53d6\u5f97\u306b\u6210\u529f\u3057\u307e\u3057\u305f\u3002&#39;);\n    reTryCnt = 0;\n\n    \/\/ \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u5b58\u5728\u3057\u306a\u3044\u5834\u5408\u3001S3\u304b\u3089\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u53d6\u5f97\u3092\u3084\u3081\u308b\u3002\n    if(results.data.length === 0){\n        console.log(&#39;S3\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u5b58\u5728\u3057\u306a\u3044\u306e\u3067\u3001\u51e6\u7406\u3092\u7d42\u4e86\u3057\u307e\u3059\u3002&#39;);\n        break;\n    }\n\n    \/\/ \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u60c5\u5831\u306e\u4e00\u89a7\u3092\u8868\u793a\u3059\u308b\u3002\n    console.log(&quot;\u53d6\u5f97\u3057\u305f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u60c5\u5831\u306e\u4e00\u89a7\u3092\u8868\u793a\u3057\u307e\u3059\u3002&quot;);\n    console.log(JSON.stringify(results.data, null, &#39;\\ \\ &#39;));\n\n    \/\/ 1000\u500b\u307e\u3067\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u53d6\u5f97\u3067\u304d\u308b\u3002\n    \/\/ \u5168\u3066\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u3057\u305f\u5834\u5408\u3001S3\u304b\u3089\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u53d6\u5f97\u3092\u3084\u3081\u308b\u3002\n    if(!results.continue){\n        console.log(&#39;S3\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u5168\u3066\u53d6\u5f97\u3057\u307e\u3057\u305f\u3002&#39;);\n        break;\n    }\n\n    \/\/ \u53d6\u5f97\u3057\u3066\u3044\u306a\u3044\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u3042\u308b\u5834\u5408\u3001\u518d\u5ea6\u3001S3\u304b\u3089\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u53d6\u5f97\u3059\u308b\u3002\n    console.log(&#39;\u53d6\u5f97\u51fa\u6765\u3066\u3044\u306a\u3044S3\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u53d6\u5f97\u3057\u307e\u3059\u3002&#39;);\n    continuationToken = results.NextContinuationToken;\n}<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>AWS S3 \u30d0\u30b1\u30c3\u30c8\u304b\u3089\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u53d6\u5f97\u3057\u307e\u3059\u3002node js \u306b\u3066 AWS SDK\u3092\u5229\u7528\u3057\u307e\u3059\u3002<\/p>\n","protected":false},"author":1,"featured_media":1258,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,31],"tags":[],"class_list":["post-3791","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-aws","category-s3"],"_links":{"self":[{"href":"https:\/\/dennie.tokyo\/it\/index.php?rest_route=\/wp\/v2\/posts\/3791","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dennie.tokyo\/it\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dennie.tokyo\/it\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dennie.tokyo\/it\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dennie.tokyo\/it\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3791"}],"version-history":[{"count":3,"href":"https:\/\/dennie.tokyo\/it\/index.php?rest_route=\/wp\/v2\/posts\/3791\/revisions"}],"predecessor-version":[{"id":3794,"href":"https:\/\/dennie.tokyo\/it\/index.php?rest_route=\/wp\/v2\/posts\/3791\/revisions\/3794"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dennie.tokyo\/it\/index.php?rest_route=\/wp\/v2\/media\/1258"}],"wp:attachment":[{"href":"https:\/\/dennie.tokyo\/it\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3791"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dennie.tokyo\/it\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3791"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dennie.tokyo\/it\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3791"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}