Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
8
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

S3の画像ファイルをExpressで表示する

S3の画像ファイルを表示する以外何もしない単純なwebアプリ。

nodeパッケージ

$ npm install express@4.9.8
$ npm install jade
$ npm install aws-sdk

aws-sdk用設定

$HOME/.aws/credentials
[default]
aws_access_key_id = XXXX
aws_secret_access_key = XXXX

テンプレート

views/index.jade
doctype html
html
  body
    img(src='./fig.png')

アプリケーション

index.js
var express = require('express');

var app = express();

app.set('view engine', 'jade');
app.set('views', __dirname + '/views');

var aws = require('aws-sdk');
var region = 'ap-northeast-1';

var s3 = new aws.S3({region:region});

app.get('/', function (req, res) {
  res.render('index');
});

app.get('/fig.png', function (req, res) {
  bucket= 'BUCKET';
  key = 'fig.png';
  s3.getObject({Bucket:bucket, Key:key},
          function(err,data) {
              if (err) {
                  // some error handling
              }
              else {
                  res.send(data.Body);
              }
          }
  );

});

var PORT = 8080;
app.listen(PORT);
$ node index.js

ポートは任意。$IP_ADDRESS:8080で画像が表示される。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
8
Help us understand the problem. What are the problem?