1
1

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 1 year has passed since last update.

Node.jsでS3に画像をアップロードする。

Posted at

#Node.jsで画像アップロードする。

S3に画像アップロードのサンプル
import S3 from 'aws-sdk/clients/s3'
import fs from 'fs'

const bucket = new S3({
  accessKeyId: process.env.AWS_S3_BUCKET_ID,
  secretAccessKey: process.env.AWS_SECRET_KEY,
})

const fileContent = fs.readFileSync('./images/lisa.png')

const param: S3.Types.PutObjectRequest = {
        Bucket: 'bucketName', // バケット名
        Key: 'lisa.png', // 保存時の画像名
        Body: fileContent,
        ACL: 'public-read', // アクセスコントロール。パブリックアクセスの許可
}

bucket.upload(param, (err: Error, data: S3.ManagedUpload.SendData) => {
  if (err) {
    console.error(err)
  } else {
    console.log('アップロード成功!!!', data)
  }
})

1
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?