https://discord.cloudflare.com logo
Join Discord
Powered by
# stream
  • i

    Isaac McFadyen | YYZ01

    08/31/2022, 2:20 AM
    Alongside OPTIONS, the actual POST or GET also needs to return CORS headers.
  • i

    Isaac McFadyen | YYZ01

    08/31/2022, 2:21 AM
    So OPTIONS first which returns header, browser sends real request which also has to return the CORS header otherwise it won't work at all.
  • j

    joshh

    08/31/2022, 2:21 AM
    for the actual post request there's nothing
  • i

    Isaac McFadyen | YYZ01

    08/31/2022, 2:21 AM
    Oh, you're sending the POST to videodelivery
  • i

    Isaac McFadyen | YYZ01

    08/31/2022, 2:21 AM
    Never mind then
  • j

    joshh

    08/31/2022, 2:22 AM
    it's meant to be a direct upload right?
  • j

    joshh

    08/31/2022, 2:22 AM
    that's the URL the CF backend returns
  • i

    Isaac McFadyen | YYZ01

    08/31/2022, 2:24 AM
    Yes, the client should be able to call it
  • i

    Isaac McFadyen | YYZ01

    08/31/2022, 2:24 AM
    You send request to backend -> backend creates upload and returns stuff -> you upload from client
  • j

    joshh

    08/31/2022, 2:25 AM
    I just tried using the Cloudflare upload example on codepen, I used the direct upload thing I got as well, and the error happened there as well.
  • j

    joshh

    08/31/2022, 2:25 AM
    Im just not getting CORS headers
  • j

    joshh

    08/31/2022, 2:25 AM
    oh, wait.
  • j

    joshh

    08/31/2022, 2:25 AM
    it's not finishing because the size isn't accurate
  • j

    joshh

    08/31/2022, 2:26 AM
    so it is going up
  • j

    joshh

    08/31/2022, 2:26 AM
    it's just not finishing because it thinks there's more
  • i

    Isaac McFadyen | YYZ01

    08/31/2022, 2:26 AM
    Oh yeah, I ran into this issue before
  • i

    Isaac McFadyen | YYZ01

    08/31/2022, 2:26 AM
    So it's confusing, slightly
  • i

    Isaac McFadyen | YYZ01

    08/31/2022, 2:26 AM
    When you request you send the size of the video i.e. like the size of the file, but when you upload you send the length. Or something like that, sec let me check docs.
  • i

    Isaac McFadyen | YYZ01

    08/31/2022, 2:28 AM
    So when you do the request to generate the signed URL, the header
    Upload-Length
    should be length in bytes.
  • j

    joshh

    08/31/2022, 2:28 AM
    I'm parsing the ``file.size`` in Javascript, that seems to be the only way to get it I can see
  • j

    joshh

    08/31/2022, 2:28 AM
    Copy code
    async function uploadVideo(e) {
        // Get the selected file from the input element
        var file = e.target.files[0]
        if (!file) {
          return;
        }
    
        const uploadLink = await getUploadLink(file.size);
        if (uploadLink == null) { alert(`[error]: Could not find drop point.`); }
    
        setShowVideoDetailsBox(true);
    
        // Create a new tus upload
        var upload = new tus.Upload(file, {
            endpoint: uploadLink,
            retryDelays: [0, 3000, 5000, 10000, 20000],
            metadata: {
              filename: file.name,
              filetype: file.type
            },
            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)
            }
        })
    
        // Check if there are any previous uploads to continue.
        upload.findPreviousUploads().then(function (previousUploads) {
            // Found previous uploads so we select the first one. 
            if (previousUploads.length) {
                upload.resumeFromPreviousUpload(previousUploads[0])
            }
    
            // Start the upload
            upload.start()
        })
      }
  • i

    Isaac McFadyen | YYZ01

    08/31/2022, 2:31 AM
    What are you sending in the
    Upload-Metadata
    from the backend?
  • i

    Isaac McFadyen | YYZ01

    08/31/2022, 2:31 AM
    file.size
    works for the
    Upload-Length
    for me FWIW
  • j

    joshh

    08/31/2022, 2:31 AM
    nothing
  • j

    joshh

    08/31/2022, 2:32 AM
    Copy code
    const response = await fetch(
      const response = await fetch(
                'https://api.cloudflare.com/client/v4/accounts/165741ddf22256f1c2d442e345765203/stream?direct_user=true',
                {
                    method: 'POST',
                    headers: {
                        'Authorization': `Bearer ${env.StreamReadWriteAPIKey}`,
                        'Tus-Resumable': '1.0.0',
                        'Upload-Length': size,
                        'Upload-Metadata': "",
                    },
                    data: JSON.stringify({ requireSignedURLs: true, allowedOrigins: ["motionfans.com"] })
                }
                )
  • i

    Isaac McFadyen | YYZ01

    08/31/2022, 2:32 AM
    Ah, OK. Body isn't used.
  • i

    Isaac McFadyen | YYZ01

    08/31/2022, 2:32 AM
    You have to put those items in the
    Upload-Metadata
  • i

    Isaac McFadyen | YYZ01

    08/31/2022, 2:32 AM
    (base64 encoded)
  • i

    Isaac McFadyen | YYZ01

    08/31/2022, 2:33 AM
    https://developers.cloudflare.com/stream/uploading-videos/direct-creator-uploads/#upload-metadata-header-syntax
  • i

    Isaac McFadyen | YYZ01

    08/31/2022, 2:34 AM
    Also
    maxDurationSeconds
    is required.
1...353637...105Latest