--- title: "Javascript向け AWS SDK" slug: "storage-storage-8-4" tags: ["Object Storage"] updated: 2026-10-20T09:11:39Z published: 2026-10-20T09:11:39Z canonical: "guide.ncloud-docs.com/storage-storage-8-4" --- > ## Documentation Index > Fetch the complete documentation index at: https://guide.ncloud-docs.com/llms.txt > Use this file to discover all available pages before exploring further. # Javascript向け AWS SDK Classic/VPC環境で利用できます。 ## Javascript向け SDK for S3 API AWS S3で提供する Javascript向け SDKを利用して NAVERクラウドプラットフォームの Object Storageを使用するユースケースです。AWS Javascript SDK 2.348.0バージョンを基準に作成されました。 ### SDKインストール ``` npm install --save aws-sdk@2.348.0 ``` - AWS Javascript SDKソース: [https://github.com/aws/aws-sdk-js](https://github.com/aws/aws-sdk-js) - 関連文書: [https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html) ### ユースケース 参考 例で使用する accessKey、secretKeyの値には、登録した API認証キー情報を入力します。 #### バケット作成 ``` const AWS = require('aws-sdk'); const endpoint = new AWS.Endpoint('https://kr.object.ncloudstorage.com'); const region = 'kr-standard'; const access_key = 'ACCESS_KEY'; const secret_key = 'SECRET_KEY'; const S3 = new AWS.S3({ endpoint, region, credentials: { accessKeyId : access_key, secretAccessKey: secret_key } }); const bucket_name = 'sample-bucket'; (async () => { await S3.createBucket({ Bucket: bucket_name, CreateBucketConfiguration: {} }).promise() })(); ``` #### バケットリストの照会 ``` const AWS = require('aws-sdk'); const endpoint = new AWS.Endpoint('https://kr.object.ncloudstorage.com'); const region = 'kr-standard'; const access_key = 'ACCESS_KEY'; const secret_key = 'SECRET_KEY'; const S3 = new AWS.S3({ endpoint: endpoint, region: region, credentials: { accessKeyId : access_key, secretAccessKey: secret_key } }); (async () => { let { Buckets } = await S3.listBuckets().promise(); for(let bucket of Buckets) { console.log(bucket.Name); } })(); ``` #### バケット削除 ``` const AWS = require('aws-sdk'); const endpoint = new AWS.Endpoint('https://kr.object.ncloudstorage.com'); const region = 'kr-standard'; const access_key = 'ACCESS_KEY'; const secret_key = 'SECRET_KEY'; const S3 = new AWS.S3({ endpoint: endpoint, region: region, credentials: { accessKeyId : access_key, secretAccessKey: secret_key } }); const bucket_name = 'sample-bucket'; (async () => { await S3.deleteBucket({ Bucket: bucket_name }).promise(); })(); ``` #### ファイルのアップロード ``` const AWS = require('aws-sdk'); const fs = require('fs'); const endpoint = new AWS.Endpoint('https://kr.object.ncloudstorage.com'); const region = 'kr-standard'; const access_key = 'ACCESS_KEY'; const secret_key = 'SECRET_KEY'; const S3 = new AWS.S3({ endpoint: endpoint, region: region, credentials: { accessKeyId : access_key, secretAccessKey: secret_key } }); const bucket_name = 'sample-bucket'; const local_file_path = '/tmp/test.txt'; (async () => { let object_name = 'sample-folder/'; // create folder await S3.putObject({ Bucket: bucket_name, Key: object_name }).promise(); object_name = 'sample-object'; // upload file await S3.putObject({ Bucket: bucket_name, Key: object_name, ACL: 'public-read', //ACLを削除すると、全体公開されません。 Body: fs.createReadStream(local_file_path) }).promise(); })(); ``` #### ファイルリストの照会 ``` const AWS = require('aws-sdk'); const endpoint = new AWS.Endpoint('https://kr.object.ncloudstorage.com'); const region = 'kr-standard'; const access_key = 'ACCESS_KEY'; const secret_key = 'SECRET_KEY'; const S3 = new AWS.S3({ endpoint: endpoint, region: region, credentials: { accessKeyId : access_key, secretAccessKey: secret_key } }); const bucket_name = 'sample-bucket'; const MAX_KEYS = 300; var params = { Bucket: bucket_name, MaxKeys: MAX_KEYS, FetchOwner: true }; (async () => { // List All Objects console.log("List All In The Bucket"); console.log("=========================="); while (true) { let response = await S3.listObjectsV2(params).promise(); console.log(`IsTruncated = ${response.IsTruncated}`); console.log( `ContinuationToken = ${response.ContinuationToken ? response.ContinuationToken : null}` ); console.log( `NextContinuationToken = ${ response.NextContinuationToken ? response.NextContinuationToken : null }` ); console.log(` Object Lists`); for (let content of response.Contents) { console.log( ` Name = ${content.Key}, Size = ${content.Size}, Owner = ${content.Owner.ID}` ); } if (response.IsTruncated) { params.ContinuationToken = response.NextContinuationToken; } else { break; } } // List Top Level Folder And Files params.Delimiter = "/"; console.log("Top Level Folders And Files In The Bucket"); console.log("=========================="); while (true) { let response = await S3.listObjectsV2(params).promise(); console.log(`IsTruncated = ${response.IsTruncated}`); console.log( `ContinuationToken = ${response.ContinuationToken ? response.ContinuationToken : null}` ); console.log( `NextContinuationToken = ${ response.NextContinuationToken ? response.NextContinuationToken : null }` ); console.log(` Folder Lists`); for (let folder of response.CommonPrefixes) { console.log(` Name = ${folder.Prefix}`); } console.log(` File Lists`); for (let content of response.Contents) { console.log( ` Name = ${content.Key}, Size = ${content.Size}, Owner = ${content.Owner.ID}` ); } if (response.IsTruncated) { params.ContinuationToken = response.NextContinuationToken; } else { break; } } })(); ``` #### ファイルのダウンロード ``` const AWS = require('aws-sdk'); const fs = require('fs'); const endpoint = new AWS.Endpoint('https://kr.object.ncloudstorage.com'); const region = 'kr-standard'; const access_key = 'ACCESS_KEY'; const secret_key = 'SECRET_KEY'; const S3 = new AWS.S3({ endpoint: endpoint, region: region, credentials: { accessKeyId : access_key, secretAccessKey: secret_key } }); const bucket_name = 'sample-bucket'; const object_name = 'sample-object'; const local_file_path = '/tmp/test.txt'; (() => { let outStream = fs.createWriteStream(local_file_path); let inStream = S3.getObject({ Bucket: bucket_name, Key: object_name }).createReadStream(); inStream.pipe(outStream); inStream.on('end', () => { console.log("Download Done"); }); })(); ``` #### ファイル削除 ``` const AWS = require('aws-sdk'); const endpoint = new AWS.Endpoint('https://kr.object.ncloudstorage.com'); const region = 'kr-standard'; const access_key = 'ACCESS_KEY'; const secret_key = 'SECRET_KEY'; const S3 = new AWS.S3({ endpoint: endpoint, region: region, credentials: { accessKeyId : access_key, secretAccessKey: secret_key } }); const bucket_name = 'sample-bucket'; (async () => { // Delete Folder let object_name = 'sample-folder/'; await S3.deleteObject({ Bucket: bucket_name, Key: object_name }).promise(); // Delete File object_name = 'sample-object'; await S3.deleteObject({ Bucket: bucket_name, Key: object_name }).promise(); })(); ``` #### ACL設定 ``` const AWS = require('aws-sdk'); const endpoint = new AWS.Endpoint('https://kr.object.ncloudstorage.com'); const region = 'kr-standard'; const access_key = 'ACCESS_KEY'; const secret_key = 'SECRET_KEY'; const S3 = new AWS.S3({ endpoint: endpoint, region: region, credentials: { accessKeyId : access_key, secretAccessKey: secret_key } }); const bucket_name = 'sample-bucket' const object_name = 'sample-object' const owner_id = 'test-owner-id' const target_id = 'test-user-id' (async () => { await S3.putObjectAcl({ Bucket: bucket_name, Key: object_name, AccessControlPolicy: { 'Grants': [ { 'Grantee': { 'ID': owner_id, 'Type': 'CanonicalUser' }, 'Permission': 'FULL_CONTROL' }, { 'Grantee': { 'ID': target_id, 'Type': 'CanonicalUser' }, 'Permission': 'READ' } ], 'Owner': { 'ID': owner_id } } }).promise(); })(); ``` #### マルチパートのアップロード 注意 マルチパートのアップロードを完了しない場合、残りのファイルがバケットに残り、バケット容量に含まれて課金されます。以下の通り不完全なマルチパートのオブジェクトを削除し、不要に課金されないようにご注意ください。 1. Cloud Advisorから不完全なマルチパートアップロードのオブジェクトについて通知を受信 1. Cloud Advisorに関する詳細は、[Cloud Advisor ご利用ガイド](https://guide.ncloud-docs.com/docs/ja/cloudadvisor-overview)をご確認ください。 2. 不完全なマルチパートのオブジェクトを確認および削除 1. キャンセルされたか、完了していないマルチパートアップロード情報を照会する: [ListMultipartUploads APIガイド](https://api.ncloud-docs.com/docs/ja/storage-objectstorage-listmultipartuploads) 2. マルチパート削除: [AbortMultipartUpload APIガイド](https://api.ncloud-docs.com/docs/ja/storage-objectstorage-abortmultipartupload) ``` const AWS = require('aws-sdk'); const fs = require('fs'); const endpoint = new AWS.Endpoint('https://kr.object.ncloudstorage.com'); const region = 'kr-standard'; const access_key = 'ACCESS_KEY'; const secret_key = 'SECRET_KEY'; const S3 = new AWS.S3({ endpoint: endpoint, region: region, credentials: { accessKeyId : access_key, secretAccessKey: secret_key } }); const bucket_name = 'sample-bucket'; const local_file_name = '/tmp/sample.file'; const object_name = 'sample-large-object'; let options = { partSize: 5 * 1024 * 1024 }; (async () => { await S3.upload({ Bucket: bucket_name, Key: object_name, Body: fs.createReadStream(local_file_name) }, options).promise(); })(); ``` #### Cross-Origin Resource Sharing(CORS)設定 ``` const AWS = require("aws-sdk"); const endpoint = new AWS.Endpoint("https://kr.object.ncloudstorage.com"); const region = "kr-standard"; const access_key = ""; const secret_key = ""; const S3 = new AWS.S3({ endpoint, region, credentials: { accessKeyId: access_key, secretAccessKey: secret_key, }, }); const bucket_name = "sample-bucket"; const params = { Bucket: bucket_name, CORSConfiguration: { CORSRules: [ { AllowedHeaders: ["*"], AllowedMethods: ["GET", "PUT"], AllowedOrigins: ["*"], MaxAgeSeconds: 3000, }, ], }, }; (async () => { // Set CORS await S3.putBucketCors(params).promise(); // Get CORS const response = await S3.getBucketCors({ Bucket: bucket_name }).promise(); console.log(JSON.stringify(response, null, 2)); })(); ```