Thank you so much for your reply, my apologies for not being super clear on what I was doing. I am currently using direct creator uploads.
- I do a POST request to https://api.cloudflare.com/client/v4/accounts/${accId}/stream?direct_user=true
and that creates a pending upload in the dashboard
This is where the confusion happens…
- When you said I am using TUS instead of upload to Stream? what would be the difference?
- with URL that I get from the response from POST request where do I make the PATCH request?
Thanks in advance
projectInput.addEventListener("input", function() {
const fileTypeVideo = this.files[0].type.includes('video')
const token = ""
const uploadLength = 900000000
const authEmail = "email"
const authKey = ""
const accId = ""
if (fileTypeVideo) {
// request for one-time tokenzied URL
async function handleRequest() {
const init = {
method: 'POST',
headers: {
'Authorization': `bearer ${token}`,
'Tus-Resumable': '1.0.0',
'Upload-Length': uploadLength,
'X-Auth-Email': authEmail,
'X-Auth-Key': authKey,
'Content-Type': "application/json"
},
}
const response = await fetch(`https://api.cloudflare.com/client/v4/accounts/${accId}/stream?direct_user=true`, init)
const results = await gatherResponse(response)
return new Response(null, {headers: {'Access-Control-Expose-Headers':'Location','Access-Control-Allow-Headers':'*','Access-Control-Allow-Origin':'*','location': results}})
}
async function gatherResponse(response) {
const { headers } = response
return await headers.get('location')
}
handleRequest()
.then((res) => {
const endPoint = "http://localhost:3000/files/"
console.log(res.headers.get("location"))
const options = {
endpoint: endPoint,
headers: {
'Access-Control-Expose-Headers':'Location',
'Access-Control-Allow-Headers':'*',
'Access-Control-Allow-Origin':'*',
'location': res.headers.get("location")
},
chunkSize: 50 * 1024 * 1024,
resume: true,
onError: function(error) {
console.log("Failed because: " + error)
},
onProgress: function(bytesUploaded, bytesTotal) {
var percentage = (bytesUploaded / bytesTotal * 100).toFixed(2)
console.log(bytesUploaded, bytesTotal, percentage + "%")
},
onSuccess: function() {
console.log("Download %s from %s", upload.file.name, upload.url)
}
}
const upload = new tus.Upload(document.querySelector(".custom-file-upload").files[0], options)
upload.start()
// start uploading here
})
console.log("video")
} else {
console.log("not a video")
}
});