2
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

AWS S3 の使い方 (Node.js)

Last updated at Posted at 2020-03-01

AWS S3 を Node.js で使ってみます。

1)バケットの一覧

s3_list.js
#! /usr/bin/node
//
//	s3_list.js
//
//					Mar/02/2020
// ---------------------------------------------------------------
var AWS = require('aws-sdk')

var s3 = new AWS.S3({apiVersion: '2006-03-01'})

s3.listBuckets(function(err, data) {
	if (err) {
		console.log("Error", err)
	} else {
		console.log("Success", data.Buckets)
	}
})

// ---------------------------------------------------------------

2)バケット内のファイル一覧

s3_listobjects.js
#! /usr/bin/node
//
//	s3_listobject.js
//
//					Mar/02/2020
// ---------------------------------------------------------------
var AWS = require('aws-sdk')


var s3 = new AWS.S3({apiVersion: '2006-03-01'})

console.log(process.argv.length)

if (process.argv.length < 3)
	{
	console.error ("*** error ***")
	process.exit(1)
	}

const bucket_name=process.argv[2]

console.log (bucket_name)

var bucketParams = { Bucket : bucket_name, }

s3.listObjects(bucketParams, function(err, data) {
  if (err) {
    console.log("Error", err)
  } else {
    console.log("Success", data)
  }
})

// ---------------------------------------------------------------

3)ファイルのアップロード

s3_upload.js
#! /usr/bin/node
//
//	s3_upload.js
//
//					Mar/02/2020
//
// ---------------------------------------------------------------
var AWS = require('aws-sdk');

var s3 = new AWS.S3({apiVersion: '2006-03-01'})

console.log(process.argv.length)

if (process.argv.length < 4)
	{
	console.error ("*** error ***")
	process.exit(1)
	}

const bucket_name = process.argv[2]
const file_name = process.argv[3]

console.log(bucket_name)
console.log(file_name)

var uploadParams = {Bucket: bucket_name, Key: '', Body: ''}

var fs = require('fs')
var fileStream = fs.createReadStream(file_name)
fileStream.on('error', function(err) {
  console.log('File Error', err)
});

uploadParams.Body = fileStream
var path = require('path')
uploadParams.Key = path.basename(file_name)

s3.upload (uploadParams, function (err, data) {
  if (err) {
    console.log("Error", err)
  } if (data) {
    console.log("Upload Success", data.Location)
  }
})

// ---------------------------------------------------------------

4)ファイルのダウンロード

s3_download.js
#! /usr/bin/node
//
//	s3_download.js
//
//					Mar/02/2020
// ---------------------------------------------------------------
var AWS = require('aws-sdk')
var s3 = new AWS.S3()
var fs = require("fs")

console.error ("*** 開始 ***")

console.log(process.argv.length)

if (process.argv.length < 4)
	{
	console.error ("*** error ***")
	process.exit(1)
	}

const bucket_name = process.argv[2]
const file_name = process.argv[3]
console.log(bucket_name)
console.log(file_name)

const params = {
    Bucket: bucket_name,
    Key: file_name
}

const file_out = file_name

s3.getObject(params, function(err, data) {
    if (err) {
        console.log(err, err.stack)
    } else {
	str_out = data.Body.toString()
	fs.writeFile (file_out,str_out,function (err)
	{
	if (err) {
		console.error ("Error on write: " + err)
		}
	else {
		fs.chmodSync (file_out,0666)
		console.log("File written.")
		console.error ("*** 終了 ***")
		}
	})
    }
})

// ---------------------------------------------------------------

5)ファイルの削除

s3_rm.js
#! /usr/bin/node
//
// ---------------------------------------------------------------
var AWS = require('aws-sdk');

var s3 = new AWS.S3({apiVersion: '2006-03-01'});

console.log(process.argv.length)

if (process.argv.length < 4)
	{
	console.error ("*** error ***")
	process.exit(1)
	}

const bucket_name = process.argv[2]
const file_name = process.argv[3]

console.error ("*** 開始 ***")

const params = {
    Bucket: bucket_name,
    Key: file_name
}


s3.deleteObject(params, function(err, data) {
    if (err) {
        console.log(err, err.stack)
    } else {
	console.log("Success", data)
	}
console.error ("*** 終了 ***")
    })


// ---------------------------------------------------------------

6)ファイルの作成

s3_put.js
#! /usr/bin/node
//
//	s3_put.js
//
//					Mar/02/2020
//
// ---------------------------------------------------------------
var AWS = require('aws-sdk');

var s3 = new AWS.S3({apiVersion: '2006-03-01'})

console.log(process.argv.length)

if (process.argv.length < 4)
	{
	console.error ("*** error ***")
	process.exit(1)
	}

const bucket_name = process.argv[2]
const file_name = process.argv[3]

console.log(bucket_name)
console.log(file_name)

var str_data = 'Good Morning\n'
str_data += 'Good Afternoon\n'
str_data += 'Good Evening\n'
str_data += 'Good Night\n'

var Params = {Bucket: bucket_name,
	 Key: file_name,
	 Body: str_data}


s3.putObject (Params, function (err, data) {
	if (err) {
		console.log("Error", err)
	} if (data) {
		console.log("Put Success", data)
	}
})

// ---------------------------------------------------------------
2
5
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
2
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?