Skip to content

Commit 327487a

Browse files
authored
tus support for direct creator uploads (#483)
* tus support for direct creator uploads * revised tus section based on kyle's feedback based on feedback from #483 * updated to use bearer token * Update direct-creator-uploads.md * tus capitaliation
1 parent 4c86a7f commit 327487a

File tree

1 file changed

+60
-1
lines changed

1 file changed

+60
-1
lines changed

products/stream/src/content/uploading-videos/direct-creator-uploads.md

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ Additionally, you can control security features through these fields:
5151

5252
</Definitions>
5353

54-
### Example request
54+
## Using Basic Uploads
55+
56+
If the uploads from your creators are under 200MB, you can use basic uploads. For videos over 200 MB, use TUS uploads (described later in this article.)
57+
58+
The first step is to request a token by calling the `direct_upload` end point from your server:
5559

5660
```bash
5761
curl -X POST \
@@ -172,6 +176,61 @@ size, the user will receive a `4xx` response.
172176

173177
</Example>
174178

179+
## Using tus (recommended for videos over 200MB)
180+
181+
tus is a protocol that supports resumable uploads and works best for larger files.
182+
183+
Typically, tus uploads require the authentication information to be sent with every request. This is not ideal for direct creators uploads because it exposes your API key (or token) to the end user.
184+
185+
To get around this, you can request a one-time tokenized URL by making a POST request to the `/stream?direct_user=true` end point:
186+
187+
188+
```
189+
curl -H "Authorization: bearer $TOKEN" -X POST -H 'Tus-Resumable: 1.0.0' -H 'Upload-Length: $VIDEO_LENGTH' 'https://api.cloudflare.com/client/v4/accounts/$ACCOUNT/stream?direct_user=true'
190+
```
191+
192+
The response will contain a `Location` header which provides the one-time URL the client can use to upload the video using tus.
193+
194+
Here is a demo Cloudflare Worker script which returns the one-time upload URL:
195+
196+
```
197+
addEventListener('fetch', event => {
198+
event.respondWith(handleRequest(event.request))
199+
})
200+
201+
/**
202+
* Respond to the request
203+
* @param {Request} request
204+
*/
205+
async function handleRequest(request) {
206+
const init = {
207+
method: 'POST',
208+
headers: {
209+
'Authorization': 'bearer $TOKEN',
210+
'Tus-Resumable': '1.0.0',
211+
'Upload-Length': request.headers.get('Upload-Length'),
212+
'Upload-Metadata': request.headers.get('Upload-Metadata')
213+
},
214+
}
215+
const response = await fetch("https://api.cloudflare.com/client/v4/accounts/$ACCOUNT/stream?direct_user=true", init)
216+
const results = await gatherResponse(response)
217+
return new Response(null, {headers: {'Access-Control-Expose-Headers':'Location','Access-Control-Allow-Headers':'*','Access-Control-Allow-Origin':'*','location':results}})
218+
219+
}
220+
221+
async function gatherResponse(response) {
222+
const { headers } = response
223+
return await headers.get('location')
224+
225+
}
226+
```
227+
228+
You can apply the same constraints as Direct Creator Upload via basic upload: expiry and maxDurationSeconds. To do so, you must pass the expiry and maxDurationSeconds as part of the `Upload-Metadata` request header as part of the first request (made by the Worker in the example above.) The `Upload-Metadata` values are ignored from subsequent requests that do the actual file upload.
229+
230+
Once you have the tokenized URL, you can pass it to the tus client to begin the upload. For details on using a tus client, refer to the [Resumable uploads with tus ](https://developers.cloudflare.com/stream/uploading-videos/upload-video-file#resumable-uploads-with-tus-for-large-files) article.
231+
232+
If you simply want to do a test upload with a tokenized url, visit the [tus codepen demo](https://codepen.io/cfzf/pen/wvGMRXe) and paste the returned url in the "Upload endpoint" field.
233+
175234
## Tracking user upload progress
176235

177236
After the creation of a unique one-time upload URL, you may wish to retain the

0 commit comments

Comments
 (0)