A minimalistic node.js package for easily uploading videos to Cloudflare Stream.
Let's start off by installing the package via NPM.
npm i cloudflare-stream
Once you've done that, you're going to want to create an instance of a CloudflareStream with your credentials.
import { CloudflareStream } from 'cloudflare-stream';
const uploader = new CloudflareStream({
email: '[email protected]', // cloudflare email address
key: 'c23864bc3ada9bf9c937810f62bb69a4e90b0' // cloudflare api key
});
Now that you've made an instance, you're going to want to upload your video to your zone. Let's create an upload with your upload options.
const upload = uploader.upload({
zone: 'ca4239cdd6a52cec1e916a6cd0e2f629', // cloudflare zone id
path: './videos/demo-video.mp4' // path to video on filesystem
});
The upload method returns an EventEmitter which has three events (progress, success, error).
Let's create some logs from your upload events.
upload.on('progress', (progress) => {
console.log(`${progress.precentage} of upload completed (${progress.uploaded} bytes / ${progress.total} bytes)`);
});
upload.on('error', (error) => {
console.log('An error has occurred', error);
});
upload.on('success', (response) => {
console.log(response);
});
class CloudflareStream {
constructor(credentials: CloudflareCredentials);
upload(uploadOptions: CloudflareUpload);
}
type CloudflareCredentials {
email,
key,
zone?
}
An email
is always required, it should be the email address which you use to sign in with Cloudflare.
A key
is always required, it should be the API Key which matches the email address which you use to sign in with Cloudflare.
If a zone
is not set in the uploadOptions
of an instance method, then a zone
is required in the credentials
.
A zone
must be a valid Cloudflare DNS Zone which is accessable using the specified email address and API key.
Returns an EventEmitter with three events (progress, success, error).
type CloudflareUpload {
zone?,
path?,
buffer?
}
If a zone
has not been set in the constructor, then a zone
is required in the uploadOptions
.
A zone
must be a valid Cloudflare DNS Zone which is accessable using the specified email address and API key.
If a zone
has already been set in the constructor, this value will overwrite it.
If a buffer
has not been set in the uploadOptions
, then a path
is required.
A path
must be a string pointing to a video file on the local filesystem.
It can be either an absolute path or a path relative to the node.js process (process.cwd()
).
If a path
has not been set in the uploadOptions
, then a buffer
is required.
A buffer
must be a valid node.js Buffer.
Emitted when a chunk has been successfully uploaded.
Example output:
{
uploaded: 23592,
total: 65536,
percentage: 35.99
}
Emitted when the upload has successfully completed.
Example output:
{
"result": {
"uid": "dd5d531a12de0c724bd1275a3b2bc9c6",
"thumbnail": "https://cloudflarestream.com/dd5d531a12de0c724bd1275a3b2bc9c6/thumbnails/thumb.png",
"readyToStream": false,
"status": {
"state": "inprogress",
"step": "encoding",
"pctComplete": "78.18"
},
"meta": {},
"labels": [],
"created": "2018-01-01T01:00:00.474936Z",
"modified": "2018-01-01T01:02:21.076571Z",
"size": 62335189,
"preview": "https://watch.cloudflarestream.com/dd5d531a12de0c724bd1275a3b2bc9c6"
},
"success": true,
"errors": [],
"messages": []
}
Emitted whenever an error has occured.
Example output:
{
message: 'Invalid Cloudflare Credentials'
}