Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

examples/aws-nodejs: merge multipart and non-multipart examples #4521

Merged
merged 3 commits into from
Jun 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 24 additions & 26 deletions examples/aws-nodejs/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@
</head>
<body>
<h1>AWS upload example</h1>
<h2>AWS S3 (non multipart)</h2>
<div id="aws-non-multipart"></div>
<h2>AWS S3 multipart</h2>
<div id="aws-multipart"></div>
<div id="uppy"></div>
<script type="module">
import { Uppy, Dashboard, AwsS3Multipart, AwsS3 } from "https://releases.transloadit.com/uppy/v3.10.0/uppy.min.mjs"
import { Uppy, Dashboard, AwsS3 } from "https://releases.transloadit.com/uppy/v3.10.0/uppy.min.mjs"
/**
* This generator transforms a deep object into URL-encodable pairs
* to work with `URLSearchParams` on the client and `body-parser` on the server.
Expand Down Expand Up @@ -40,17 +37,28 @@ <h2>AWS S3 multipart</h2>
// You'd also have to add `Content-Type` header with value `application/json`.
}
{
const MiB = 0x10_00_00;

const uppy = new Uppy()
.use(Dashboard, {
inline: true,
target: "#aws-non-multipart",
target: '#uppy',
})
.use(AwsS3, {
// Files that are more than 100MiB should be uploaded in multiple parts.
shouldUseMultipart: (file) => file.size > 100 * MiB,

// ========== Non-Multipart Uploads ==========

/**
* This method tells Uppy how to handle non-multipart uploads.
* If for some reason you want to only support multipart uploads,
* you don't need to implement it.
*/
async getUploadParameters (file) {
// Send a request to our Express.js signing endpoint.
const response = await fetch('/sign-s3', {
method: 'POST',
// Send and receive JSON.
headers: {
accept: 'application/json',
},
Expand All @@ -69,30 +77,20 @@ <h2>AWS S3 multipart</h2>
return {
method: data.method,
url: data.url,
fields: data.fields, // For presigned PUT uploads, this should be left empty.
fields: {}, // For presigned PUT uploads, this should be left empty.
// Provide content type header required by S3
headers: {
'Content-Type': file.type,
},
}
},
});

uppy.on('complete', (result) => {
console.log('Upload complete! We’ve uploaded these files:', result.successful)
})

uppy.on('upload-success', (file, data) => {
console.log('Upload success! We’ve uploaded this file:', file.meta['name'])
})
}
{
const uppy = new Uppy()
.use(Dashboard, {
inline: true,
target: "#aws-multipart",
})
.use(AwsS3Multipart, {

// ========== Multipart Uploads ==========

// The following methods are only useful for multipart uploads:
// If you are not interested in multipart uploads, you don't need to
// implement them (you'd also need to set `shouldUseMultipart: false` though).

async createMultipartUpload(file, signal) {
if (signal?.aborted) {
const err = new DOMException('The operation was aborted', 'AbortError')
Expand Down Expand Up @@ -202,7 +200,7 @@ <h2>AWS S3 multipart</h2>
const data = await response.json()

return data
}
},
})

uppy.on('complete', (result) => {
Expand Down