LoginSignup
1
2

More than 3 years have passed since last update.

AWS SDK for JavaScriptでS3 のプライベートバケットから画像を取得する

Posted at

前提

処理

aws-sdkを読み込む

const AWS = require('aws-sdk')

インスタンス化

const s3 = new AWS.S3({
  accessKeyId: 'xxxxxxxxxxxxxxxxx', //アクセスキー
  secretAccessKey: 'xxxxxxxxxxxxxxxxx', //シークレットアクセスキー
  region: '' //リージョン
})

getObjectのパラメーターを用意

const params = {
  Bucket: "buket", //バケット名
  Key: "test.jpg", //キー
};

getObjectを実行

const s3Promise = s3.getObject(params).promise();

処理が成功したらbase64型に変換する

s3Promise.then(function(data) {
  console.log('data:image/jpg;base64' + data.Body.toString('base64'))
}).catch(function(err) {
  console.log(err);
});

全体

const AWS = require('aws-sdk')

const s3 = new AWS.S3({
  accessKeyId: '', //アクセスキー
  secretAccessKey: '', //シークレットアクセスキー
  region: '' //リージョン
})

const params = {
  Bucket: "", //バケット名
  Key: "", //キー
};

const s3Promise = s3.getObject(params).promise();
s3Promise.then(function(data) {
  console.log('data:image/jpg;base64' + data.Body.toString('base64'))
}).catch(function(err) {
  console.log(err);
});
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2