Skip to content

Commit

Permalink
@uppy/s3-multipart: take advantage of backendEndpoint option
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed Jan 5, 2023
1 parent 90ec8d4 commit 686f2d0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
25 changes: 17 additions & 8 deletions examples/aws-nodejs/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ <h2>AWS S3 multipart</h2>
<script type="module">
import { Uppy, Dashboard, AwsS3Multipart, AwsS3 } from "https://releases.transloadit.com/uppy/v3.3.1/uppy.min.mjs"
{
const allowedMetaFields = ['name']
const uppy = new Uppy()
.use(Dashboard, {
inline: true,
target: '#aws-non-multipart',
})
.use(AwsS3, {
allowedMetaFields,
async getUploadParameters (file) {
// Send a request to our Express.js signing endpoint.
const response = await fetch('/sign-s3', {
Expand Down Expand Up @@ -62,26 +64,33 @@ <h2>AWS S3 multipart</h2>
})
}
{
const allowedMetaFields = ['name']
const uppy = new Uppy()
.use(Dashboard, {
inline: true,
target: '#aws-multipart',
})
.use(AwsS3Multipart, {
allowedMetaFields,

// If your server configuration follows the same paths as Companion,
// you can specify only those two options:
backendURL: window.origin,
backendEndpoint: '/s3/multipart',

// Otherwise, you can provide a custom implementation for each step:
async createMultipartUpload(file, signal) {
if (signal?.aborted) {
const err = new DOMException('The operation was aborted', 'AbortError')
Object.defineProperty(err, 'cause', { __proto__: null, configurable: true, writable: true, value: signal.reason })
throw err
}

const metadata = {}

Object.keys(file.meta || {}).forEach(key => {
if (file.meta[key] != null) {
metadata[key] = file.meta[key].toString()
}
})
const metadata = file.meta ? Object.fromEntries(
allowedMetaFields
.filter(key => file.meta[key] != null)
.map(key => [key, String(file.meta[key])]),
) : {}

const response = await fetch('/s3/multipart', {
method: 'POST',
Expand Down Expand Up @@ -155,7 +164,7 @@ <h2>AWS S3 multipart</h2>
return data
},

async completeMultipartUpload (file, { key, uploadId, parts }, signal) {
async _completeMultipartUpload (file, { key, uploadId, parts }, signal) {
if (signal?.aborted) {
const err = new DOMException('The operation was aborted', 'AbortError')
Object.defineProperty(err, 'cause', { __proto__: null, configurable: true, writable: true, value: signal.reason })
Expand Down
20 changes: 11 additions & 9 deletions packages/@uppy/aws-s3-multipart/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ export default class AwsS3Multipart extends BasePlugin {
completeMultipartUpload: this.completeMultipartUpload.bind(this),
signPart: this.signPart.bind(this),
uploadPartBytes: AwsS3Multipart.uploadPartBytes,
companionHeaders: {},
backendHeaders: {},
backendEndpoint: '/s3/multipart',
}

this.opts = { ...defaultOptions, ...opts }
Expand Down Expand Up @@ -306,8 +307,9 @@ export default class AwsS3Multipart extends BasePlugin {

// TODO: make this a private method in the next major
assertHost (method) {
if (!this.opts.companionUrl) {
throw new Error(`Expected a \`companionUrl\` option containing a Companion address, or if you are not using Companion, a custom \`${method}\` implementation.`)
const backendURL = this.opts.backendURL ?? this.opts.companionUrl
if (!backendURL) {
throw new Error(`Please provide a \`backendURL\` option, or a custom \`${method}\` implementation.`)
}
}

Expand All @@ -318,10 +320,10 @@ export default class AwsS3Multipart extends BasePlugin {
const metadata = file.meta ? Object.fromEntries(
(this.opts.allowedMetaFields ?? Object.keys(file.meta))
.filter(key => file.meta[key] != null)
.map(key => [`metadata[${key}]`, String(file.meta[key])]),
.map(key => [key, String(file.meta[key])]),
) : {}

return this.#client.post('s3/multipart', {
return this.#client.post(this.opts.backendEndpoint, {
filename: file.name,
type: file.type,
metadata,
Expand All @@ -333,7 +335,7 @@ export default class AwsS3Multipart extends BasePlugin {
throwIfAborted(signal)

const filename = encodeURIComponent(key)
return this.#client.get(`s3/multipart/${uploadId}?key=${filename}`, { signal })
return this.#client.get(`${this.opts.backendEndpoint}/${uploadId}?key=${filename}`, { signal })
.then(assertServerError)
}

Expand All @@ -343,7 +345,7 @@ export default class AwsS3Multipart extends BasePlugin {

const filename = encodeURIComponent(key)
const uploadIdEnc = encodeURIComponent(uploadId)
return this.#client.post(`s3/multipart/${uploadIdEnc}/complete?key=${filename}`, { parts }, { signal })
return this.#client.post(`${this.opts.backendEndpoint}/${uploadIdEnc}/complete?key=${filename}`, { parts }, { signal })
.then(assertServerError)
}

Expand All @@ -356,7 +358,7 @@ export default class AwsS3Multipart extends BasePlugin {
}

const filename = encodeURIComponent(key)
return this.#client.get(`s3/multipart/${uploadId}/${partNumber}?key=${filename}`, { signal })
return this.#client.get(`${this.opts.backendEndpoint}/${uploadId}/${partNumber}?key=${filename}`, { signal })
.then(assertServerError)
}

Expand All @@ -365,7 +367,7 @@ export default class AwsS3Multipart extends BasePlugin {

const filename = encodeURIComponent(key)
const uploadIdEnc = encodeURIComponent(uploadId)
return this.#client.delete(`s3/multipart/${uploadIdEnc}?key=${filename}`, undefined, { signal })
return this.#client.delete(`${this.opts.backendEndpoint}/${uploadIdEnc}?key=${filename}`, undefined, { signal })
.then(assertServerError)
}

Expand Down

0 comments on commit 686f2d0

Please sign in to comment.