uploader.js 1.95 KB
Newer Older
mntmn's avatar
mntmn committed
1
2
3
4
5
6
7
8
'use strict';

var AWS = require('aws-sdk');
AWS.config.region = 'eu-central-1';

var fs = require('fs');
var config = require('config');

Martin Guether's avatar
Martin Guether committed
9
10
11
var cdn = config.get("storage_cdn");
var storage_endpoint = config.get("storage_endpoint");

12
13
const bucketName = "sdeck-fresh-development";
const ep = new AWS.Endpoint(storage_endpoint);
Martin Guether's avatar
Martin Guether committed
14
15
16
17
18
19
20
21
22

AWS.config.update(new AWS.Config({
  accessKeyId: process.env.MINIO_ACCESS_KEY, 
  secretAccessKey: process.env.MINIO_SECRET_KEY, 
  region: 'us-east-1',
  s3ForcePathStyle: true,
  signatureVersion: 'v4'
}));

23
24
25
26
const s3 = new AWS.S3({
  endpoint: ep
});

mntmn's avatar
mntmn committed
27
28
module.exports = {
  removeFile: (path, callback) => {
Martin Guether's avatar
Martin Guether committed
29
30
31
    // const s3 = new AWS.S3({
    //   region: 'eu-central-1'
    // });
mntmn's avatar
mntmn committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
    const bucket = config.get("storage_bucket");
    s3.deleteObject({
      Bucket: bucket, Key: path
    }, (err, res) => {
      if (err){
        console.error(err);
        callback(err);
      }else {
        callback(null, res);
      }
    });
  },
  uploadFile: function(fileName, mime, localFilePath, callback) {
    if (typeof(localFilePath)!="string") {
      callback({error:"missing path"}, null);
      return;
    }
Martin Guether's avatar
Martin Guether committed
49
    console.log("[storage] uploading", localFilePath, " to ", fileName);
mntmn's avatar
mntmn committed
50
51
52
53
54
55
56
57
58
59
60

    const bucket = config.get("storage_bucket");
    const fileStream = fs.createReadStream(localFilePath);
    fileStream.on('error', function (err) {
      if (err) {
        console.error(err);
        callback(err);
      }
    });
    fileStream.on('open', function () {
      // FIXME
Martin Guether's avatar
Martin Guether committed
61
62
63
      // var s3 = new AWS.S3({
      //   region: 'eu-central-1'
      // });
mntmn's avatar
mntmn committed
64
65
66
67
68
69
70
71
72
73
74

      s3.putObject({
        Bucket: bucket,
        Key: fileName,
        ContentType: mime,
        Body: fileStream
      }, function (err) {
        if (err){
          console.error(err);
          callback(err);
        }else {
Martin Guether's avatar
Martin Guether committed
75
          const url = cdn + "/" + fileName;
mntmn's avatar
mntmn committed
76
77
78
79
80
81
82
          console.log("[s3]" + localFilePath + " to " + url);
          callback(null, url);
        }
      });
    });
  }
};