Last active
March 21, 2019 20:15
-
-
Save lakesare/2d97c5aa9bad9e587a22dea5c9b6ed64 to your computer and use it in GitHub Desktop.
Example of Transloadit TUS upload with simple fetch and tus-js-client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import TusJsClient from 'tus-js-client'; | |
const TRANSLOADIT_AUTH_KEY = 'bbbb889ccccc211e984444d105c7e04b7'; | |
const createAssembly = (uniqueAwsS3Path) => { | |
const formData = new FormData(); | |
formData.append('params', JSON.stringify({ | |
auth: { | |
key: TRANSLOADIT_AUTH_KEY | |
}, | |
steps: { | |
// 1. Upload the video | |
':original': { robot: '/upload/handle' }, | |
// 2. Convert the original video to mp4. | |
'myEncodedVideo': { | |
use: ':original', | |
robot: '/video/encode', | |
ffmpeg_stack: 'v3.3.3', | |
preset: 'hls-1080p' | |
}, | |
// 3. Export it to AWS S3 | |
'myExportedVideo': { | |
use: 'myEncodedVideo', | |
robot: '/s3/store', | |
credentials: 'MemcodeS3Credentials', | |
path: uniqueAwsS3Path | |
} | |
} | |
})); | |
formData.append('num_expected_upload_files', 1); | |
return fetch('https://api2.transloadit.com/assemblies', { | |
method: 'POST', | |
body: formData | |
}) | |
.then((response) => response.json()) | |
.then((response) => ({ | |
tusUrl: response.tus_url, | |
assemblyUrl: response.assembly_ssl_url | |
}) | |
}; | |
const startTusUpload = (tusUrl, assemblyUrl, file, callbacks = {}) => { | |
const tusJsClientUpload = new TusJsClient.Upload(file, { | |
endpoint: tusUrl, | |
retryDelays: [0, 1000, 3000, 5000], | |
metadata: { | |
name: file.name, | |
type: file.type, | |
filename: file.name, | |
filetype: file.type, | |
fieldname: 'file', | |
assembly_url: assemblyUrl | |
}, | |
resume: false, | |
chunkSize: 10000000, | |
onError: callbacks.onError, | |
onProgress: callbacks.onProgress, | |
onSuccess: callbacks.onSuccess | |
}); | |
tusJsClientUpload.start(); | |
}; | |
const getAssembly = (assemblyUrl) => | |
fetch(assemblyUrl, { method: 'GET' }) | |
.then((response) => response.json()); | |
export default { createAssembly, startTusUpload, getAssembly }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment