Created
August 25, 2021 07:39
-
-
Save hmelenok/3520159443379ce51bc93f880ac0bcb1 to your computer and use it in GitHub Desktop.
Upload Uppy file
This file contains hidden or 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
const taskId = ''; //TODO: get task if from prev mutaition | |
const raw = JSON.stringify({ | |
filename: 'Screenshot 2021-08-20 at 12.06.13.png', | |
type: 'image/png', | |
}); | |
const {key: uploadKey, uploadId} = await fetch('https://api.gsstaging.net/cpw/uppy/s3/multipart', { | |
method: 'POST', | |
headers: { | |
'x-cpw-task-id': taskId, | |
authorization: '', //TODO | |
accept: 'application/json', | |
'Content-Type': 'application/json', | |
}, | |
body: raw, | |
}) | |
.then(response => response.json()) | |
.catch(error => console.log('error', error)); | |
const {url} = await fetch( | |
`https://api.gsstaging.net/cpw/uppy/s3/multipart/${uploadId}/1?key=${uploadKey}`, | |
{ | |
method: 'GET', | |
headers: { | |
'x-cpw-task-id': taskId, | |
authorization: '', //TODO | |
accept: 'application/json', | |
'Content-Type': 'application/json', | |
}, | |
} | |
) | |
.then(response => response.text()) | |
.catch(error => console.log('error', error)); | |
const file = '<file contents here>'; //TODO: fs.readFileSync() | |
const requestOptions = { | |
method: 'PUT', | |
headers: { | |
Accept: '*/*', | |
}, | |
body: file, | |
}; | |
//Final call to upload file | |
await fetch(url, requestOptions).catch(error => console.log('error', error)); | |
//Add GraphQL to notify about upload use uploadKey, uploadId - uploadKey.split('/').reverse()[0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment