Renée Kooi 7 роки тому
батько
коміт
cf5653f016

+ 3 - 27
examples/digitalocean-spaces/main.js

@@ -11,32 +11,8 @@ uppy.use(Dashboard, {
   inline: true,
   target: 'body'
 })
-uppy.use(AwsS3, {
-  getUploadParameters (file) {
-    // Send a request to our custom signing endpoint.
-    return fetch('/params', {
-      method: 'post',
-      // Send and receive JSON.
-      headers: {
-        accept: 'application/json',
-        'content-type': 'application/json'
-      },
-      body: JSON.stringify({
-        filename: file.name,
-        contentType: file.type
-      })
-    }).then((response) => {
-      // Parse the JSON response.
-      return response.json()
-    }).then((data) => {
-      // Return an object in the correct shape.
-      return {
-        method: data.method,
-        url: data.url,
-        fields: {}
-      }
-    })
-  }
-})
+
+// No client side changes needed!
+uppy.use(AwsS3, { host: '/uppy-server' })
 
 uppy.run()

+ 25 - 29
examples/digitalocean-spaces/server.js

@@ -2,22 +2,21 @@ const fs = require('fs')
 const path = require('path')
 const budo = require('budo')
 const router = require('router')
-const bodyParser = require('body-parser')
-const S3 = require('aws-sdk/clients/s3')
+const uppy = require('uppy-server')
 
 /**
  * Environment variables:
  *
- *   - DO_REGION - Your space region, eg "ams3"
- *   - DO_ACCESS_KEY - Your access key ID
- *   - DO_SECRET_KEY - Your secret access key
- *   - DO_SPACE - Your space's name.
+ *   - UPPYSERVER_AWS_REGION - Your space region, eg "ams3"
+ *   - UPPYSERVER_AWS_KEY - Your access key ID
+ *   - UPPYSERVER_AWS_SECRET - Your secret access key
+ *   - UPPYSERVER_AWS_BUCKET - Your space's name.
  */
 
-if (!process.env.DO_REGION) throw new Error('Missing Space region, please set the DO_REGION environment variable (eg. "DO_REGION=ams3")')
-if (!process.env.DO_ACCESS_KEY) throw new Error('Missing access key, please set the DO_ACCESS_KEY environment variable')
-if (!process.env.DO_SECRET_KEY) throw new Error('Missing secret key, please set the DO_SECRET_KEY environment variable')
-if (!process.env.DO_SPACE) throw new Error('Missing Space name, please set the DO_SPACE environment variable')
+if (!process.env.UPPYSERVER_AWS_REGION) throw new Error('Missing Space region, please set the UPPYSERVER_AWS_REGION environment variable (eg. "UPPYSERVER_AWS_REGION=ams3")')
+if (!process.env.UPPYSERVER_AWS_KEY) throw new Error('Missing access key, please set the UPPYSERVER_AWS_KEY environment variable')
+if (!process.env.UPPYSERVER_AWS_SECRET) throw new Error('Missing secret key, please set the UPPYSERVER_AWS_SECRET environment variable')
+if (!process.env.UPPYSERVER_AWS_BUCKET) throw new Error('Missing Space name, please set the UPPYSERVER_AWS_BUCKET environment variable')
 
 // Prepare the server.
 const PORT = process.env.PORT || 3452
@@ -25,26 +24,23 @@ const PORT = process.env.PORT || 3452
 const app = router()
 
 // Set up the /params endpoint that will create signed URLs for us.
-const s3 = new S3({
-  endpoint: `${process.env.DO_REGION}.digitaloceanspaces.com`,
-  accessKeyId: process.env.DO_ACCESS_KEY,
-  secretAccessKey: process.env.DO_SECRET_KEY
-})
-app.use(bodyParser.json())
-app.post('/params', (req, res, next) => {
-  const { filename, contentType } = req.body
-  s3.getSignedUrl('putObject', {
-    Bucket: process.env.DO_SPACE,
-    Key: filename,
-    ContentType: contentType,
-    Expires: 5 * 60 * 1000 // 5 minutes
-  }, (err, data) => {
-    if (err) return next(err)
+app.use(require('cors')())
+app.use(require('body-parser').json())
+app.use('/uppy-server', uppy.app({
+  providerOptions: {
+    s3: {
+      // This is the crucial part; set an endpoint template for the service you want to use.
+      endpoint: 'https://{region}.digitaloceanspaces.com',
+      getKey: (req, filename) => `uploads/${filename}`,
 
-    res.setHeader('content-type', 'application/json')
-    res.end(JSON.stringify({ method: 'put', url: data }, null, 2))
-  })
-})
+      key: process.env.UPPYSERVER_AWS_KEY,
+      secret: process.env.UPPYSERVER_AWS_SECRET,
+      bucket: process.env.UPPYSERVER_AWS_BUCKET,
+      region: process.env.UPPYSERVER_AWS_REGION
+    }
+  },
+  server: { host: `localhost:${PORT}` }
+}))
 
 // Serve the built CSS file.
 app.get('/uppy.min.css', (req, res) => {