Skip to content

Instantly share code, notes, and snippets.

@hmelenok
Created August 25, 2021 07:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hmelenok/3520159443379ce51bc93f880ac0bcb1 to your computer and use it in GitHub Desktop.
Save hmelenok/3520159443379ce51bc93f880ac0bcb1 to your computer and use it in GitHub Desktop.
Upload Uppy file
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