如果您将视频存储在云存储桶(R2、S3、GCS)中,或由提供下载链接的服务托管,您可以传递其 URL。Stream 将代表您获取文件。
使用视频链接向 Stream API 发送 POST 请求。
curl \
--data '{"url":"https://pub-2da57dbfcf5f4369863991d59747d686.r2.dev/acadia.mp4","meta":{"name":"My First Stream Video"}}' \
--header "Authorization: Bearer <API_TOKEN>" \
https://api.cloudflare.com/client/v4/accounts/{account_id}/stream/copyconst client = new Cloudflare({
apiEmail: process.env['CLOUDFLARE_EMAIL'], // This is the default and can be omitted
apiKey: process.env['CLOUDFLARE_API_KEY'], // This is the default and can be omitted
});
const video = await client.stream.copy.create({
account_id: '<ACCOUNT_ID>',
url: 'https://pub-2da57dbfcf5f4369863991d59747d686.r2.dev/acadia.mp4',
});请参阅完整的 Stream REST API 和 SDK 参考,了解如何从外部应用程序使用 REST API,以及适用于外部 TypeScript、Python 或 Go 应用的预生成 SDK。
export default {
async fetch(request, env, ctx): Promise<Response> {
// upload a video with a link
const videoDetails = await env.STREAM.upload(
"https://pub-2da57dbfcf5f4369863991d59747d686.r2.dev/acadia.mp4",
// (optional) attach metadata
{ meta: { name: "My First Stream Video" } }
);
// return a Workers response
return new Response(
JSON.stringify(videoDetails),
);
},
} satisfies ExportedHandler<{ STREAM: StreamBinding }>;{
"$schema": "node_modules/wrangler/config-schema.json",
"name": "<ENTER_WORKER_NAME>",
"main": "src/index.ts",
"compatibility_date": "2026-04-14",
"observability": {
"enabled": true
},
"stream": {
"binding": "STREAM"
}
}请参阅完整的 Workers Stream 绑定 API 参考。
如果您将视频存储在云存储桶中,您可以传递文件的 HTTP 链接,Stream 将代表您获取文件。
Stream 必须下载并编码视频,根据视频长度可能需要几秒到几分钟。
当 readyToStream 值返回 true 时,您的视频已可供流式传输。
您可以选择使用 webhooks,在视频可供流式传输或发生错误时通知您。
{
"result": {
"uid": "6b9e68b07dfee8cc2d116e4c51d6a957",
"thumbnail": "https://customer-f33zs165nr7gyfy4.cloudflarestream.com/6b9e68b07dfee8cc2d116e4c51d6a957/thumbnails/thumbnail.jpg",
"thumbnailTimestampPct": 0,
"readyToStream": false,
"status": {
"state": "downloading"
},
"meta": {
"downloaded-from": "https://pub-2da57dbfcf5f4369863991d59747d686.r2.dev/acadia.mp4",
"name": "My First Stream Video"
},
"created": "2020-10-16T20:20:17.872170843Z",
"modified": "2020-10-16T20:20:17.872170843Z",
"size": 9032701,
"preview": "https://customer-f33zs165nr7gyfy4.cloudflarestream.com/6b9e68b07dfee8cc2d116e4c51d6a957/watch",
"allowedOrigins": [],
"requireSignedURLs": false,
"uploaded": "2020-10-16T20:20:17.872170843Z",
"uploadExpiry": null,
"maxSizeBytes": 0,
"maxDurationSeconds": 0,
"duration": -1,
"input": {
"width": -1,
"height": -1
},
"playback": {
"hls": "https://customer-f33zs165nr7gyfy4.cloudflarestream.com/6b9e68b07dfee8cc2d116e4c51d6a957/manifest/video.m3u8",
"dash": "https://customer-f33zs165nr7gyfy4.cloudflarestream.com/6b9e68b07dfee8cc2d116e4c51d6a957/manifest/video.mpd"
},
"watermark": null
},
"success": true,
"errors": [],
"messages": []
}视频上传后,您可以使用上述示例响应中显示的 video uid,通过 Stream 视频播放器 播放视频。
如果您使用自己的播放器或在移动应用中渲染视频,请参阅 使用您自己的播放器。