bluegill / katana

🚀 a powerful, open-source screenshot utility for macOS
GNU General Public License v3.0
356 stars 38 forks source link

Is it possible to upload to Amazon S3? #21

Closed igordelorenzi closed 5 years ago

igordelorenzi commented 5 years ago

I'm thinking about build a simple local server to enable image upload integrated with Katana. Anyone have ever tried this?

igordelorenzi commented 5 years ago

I took the initiative to make my own upload server locally. I hope it helps someone.

You only need to install some node packages, run the server and configure a Custom Upload Service at Katana (http://localhost:1337/upload in this case):

npm i --save express multer multer-s3 aws-sdk node server.js

server.js:

const multer   = require('multer')
const multerS3 = require('multer-s3')
const aws      = require('aws-sdk')
const express  = require("express")

aws.config.update({
    secretAccessKey: "YOUR_ACCESS_KEY",
    accessKeyId: "YOUR_ACCESS_KEY_ID",
    region: 'us-east-1'
})

const s3 = new aws.S3()

const upload = multer({
  storage: multerS3({
    s3: s3,
    bucket: 'YOUR_BUCKET_NAME/FOLDER_0/FOLDER_1',
    contentType: multerS3.AUTO_CONTENT_TYPE,
    contentDisposition: 'inline',
    acl: 'public-read',
    metadata: function (req, file, cb) {
      cb(null, { fieldName: file.fieldname })
    },
    key: function (req, file, cb) {
      cb(null, file.originalname)
    }
  })
})

const router = express.Router()

router.post('/upload', upload.array('files[]', 2), function (req, res) {
  const data = { success: true, url: req.files[0].location }
  console.log('upload data', data)
  return res.json(data)
})

const app = express()

const bodyParser = require('body-parser')

app.use(bodyParser.json())
app.use(router)

const PORT = process.env.PORT || 1337

app.listen(PORT , function() {
  console.log(`Server is running at PORT ${PORT}!`)
})
igordelorenzi commented 5 years ago

Update: uploaded the script on a github repo. Enjoy: https://github.com/igordelorenzi/amazon_s3_upload_server