Use the OSS Go SDK to create and manage LiveChannels for RTMP-based live streaming. This page covers all common LiveChannel operations: creating, listing, enabling or disabling, generating signed URLs, querying status and configuration, managing VOD playlists, retrieving stream ingest history, and deleting LiveChannels.
Prerequisites
Before you begin, ensure that you have:
An OSS bucket
The
OSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRETenvironment variables set with valid access credentialsThe OSS Go SDK installed:
github.com/aliyun/aliyun-oss-go-sdk/oss
How it works
All examples on this page share the same setup pattern:
Load credentials from environment variables using
oss.NewEnvironmentVariableCredentialsProvider().Create an
OSSClientinstance with your bucket's endpoint.Get a
Buckethandle, then call the LiveChannel method on it.
Create a LiveChannel
Call PutLiveChannel to create a LiveChannel before ingesting audio or video over Real-Time Messaging Protocol (RTMP). The operation returns an RTMP ingest URL and a corresponding HTTP Live Streaming (HLS) playback URL. Use the channel name for subsequent operations such as querying ingest status, retrieving ingest history, or disabling the channel.
LiveChannel names cannot contain forward slashes (/).
package main
import (
"fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"os"
)
func handleError(err error) {
fmt.Println("Error:", err)
os.Exit(-1)
}
func main() {
// Load credentials from environment variables.
// Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this example.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Replace yourEndpoint with the endpoint for your bucket's region,
// for example, https://oss-cn-hangzhou.aliyuncs.com for China (Hangzhou).
client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
if err != nil {
handleError(err)
}
bucket, err := client.Bucket("srcexamplebucket")
if err != nil {
handleError(err)
}
// Configure the HLS output. PlaylistName must end with .m3u8 and be 6–128 bytes.
// FragCount sets the number of .ts fragments in the .m3u8 file.
// FragDuration sets the duration of each .ts fragment in seconds.
target := oss.LiveChannelTarget{
PlaylistName: "playlist.m3u8",
Type: "HLS", // Only HLS is supported.
FragCount: 3,
FragDuration: 5,
}
// Configure high-frequency snapshots (optional).
// The role must have write permission on DestBucket and permission to publish to NotifyTopic.
// Interval must be between 1 and 100 seconds.
snapshot := oss.LiveChannelSnapshot{
RoleName: "examplerole",
Interval: 10,
DestBucket: "destexamplebucket",
NotifyTopic: "exampletopic",
}
// Description can be up to 128 bytes.
// Set Status to "disabled" to create the channel in a paused state.
config := oss.LiveChannelConfiguration{
Description: "this is my channel",
Status: "enabled",
Target: target,
Snapshot: &snapshot,
}
// Create the LiveChannel.
// data.PublishUrls[0] — RTMP ingest URL, for example:
// rtmp://srcexamplebucket.oss-cn-hangzhou.aliyuncs.com/live/mychannel
// data.PlayUrls[0] — HLS playback URL, for example:
// http://srcexamplebucket.oss-cn-hangzhou.aliyuncs.com/mychannel/playlist.m3u8
data, err := bucket.CreateLiveChannel("mychannel", config)
if err != nil {
handleError(err)
}
fmt.Println(data)
}List LiveChannels
Use a prefix to filter results.
package main
import (
"fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"os"
)
func handleError(err error) {
fmt.Println("Error:", err)
os.Exit(-1)
}
func main() {
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
if err != nil {
handleError(err)
}
bucket, err := client.Bucket("yourBucketName")
if err != nil {
handleError(err)
}
// List all LiveChannels whose names start with "test".
result, err := bucket.ListLiveChannel(oss.Prefix("test"))
if err != nil {
handleError(err)
}
fmt.Println(result)
}Enable or disable a LiveChannel
A LiveChannel can be in one of two states: enabled or disabled.
enabled: The channel accepts stream ingest requests.
disabled: OSS rejects new ingest requests. Any active ingest session is disconnected within approximately 10 seconds.
Disable the LiveChannel before deleting it. Delete requests fail if a client is actively ingesting to the channel.
package main
import (
"fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"os"
)
func handleError(err error) {
fmt.Println("Error:", err)
os.Exit(-1)
}
func main() {
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
if err != nil {
handleError(err)
}
bucket, err := client.Bucket("yourBucketName")
if err != nil {
handleError(err)
}
// Set status to "enabled" to resume stream ingest.
err = bucket.PutLiveChannelStatus("mychannel", "disabled")
if err != nil {
handleError(err)
}
}Get a signed RTMP URL
Generate a time-limited signed RTMP URL for authenticated stream ingest. The expiration time is a UNIX timestamp.
package main
import (
"fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"os"
"time"
)
func main() {
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
bucket, err := client.Bucket("examplebucket")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// expiration is a UNIX timestamp. This example sets it to 1 hour from now.
expiration := time.Now().Unix() + 3600
result, err := bucket.SignRtmpURL("test-sign-rtmp-url", "playlist.m3u8", expiration)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
fmt.Println("Signed RTMP URL:", result)
}Get LiveChannel status
Query the current ingest state of a LiveChannel.
package main
import (
"fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"os"
)
func handleError(err error) {
fmt.Println("Error:", err)
os.Exit(-1)
}
func main() {
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
if err != nil {
handleError(err)
}
bucket, err := client.Bucket("yourBucketName")
if err != nil {
handleError(err)
}
result, err := bucket.GetLiveChannelStat("mychannel")
if err != nil {
handleError(err)
}
fmt.Println(result)
}Get LiveChannel configuration
Retrieve the full configuration of a LiveChannel, including its HLS target settings, snapshot configuration, status, and description.
package main
import (
"fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"os"
)
func handleError(err error) {
fmt.Println("Error:", err)
os.Exit(-1)
}
func main() {
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
if err != nil {
handleError(err)
}
bucket, err := client.Bucket("yourBucketName")
if err != nil {
handleError(err)
}
result, err := bucket.GetLiveChannelInfo("mychannel")
if err != nil {
handleError(err)
}
fmt.Println(result)
}Generate a VOD playlist
PostVodPlaylist collects the .ts fragments generated during stream ingest within a time range and combines them into an .m3u8 playlist file.
package main
import (
"fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"os"
"time"
)
func handleError(err error) {
fmt.Println("Error:", err)
os.Exit(-1)
}
func main() {
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
if err != nil {
handleError(err)
}
bucket, err := client.Bucket("yourBucketName")
if err != nil {
handleError(err)
}
// Query .ts fragments from the last 60 minutes and combine them into a playlist.
endTime := time.Now().Add(time.Minute)
startTime := endTime.Add(-60 * time.Minute)
err = bucket.PostVodPlaylist("mychannel", "playlist.m3u8", startTime, endTime)
if err != nil {
handleError(err)
}
}View a VOD playlist
GetVodPlaylist reads the .m3u8 playlist generated from stream ingest within a time range and returns its content.
package main
import (
"fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"os"
"time"
)
func handleError(err error) {
fmt.Println("Error:", err)
os.Exit(-1)
}
func main() {
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
if err != nil {
handleError(err)
}
bucket, err := client.Bucket("yourBucketName")
if err != nil {
handleError(err)
}
endTime := time.Now().Add(time.Minute)
startTime := endTime.Add(-60 * time.Minute)
result, err := bucket.GetVodPlaylist("mychannel", startTime, endTime)
if err != nil {
handleError(err)
}
fmt.Println(result)
}Get stream ingest history
GetLiveChannelHistory returns the last 10 stream ingest records for a LiveChannel.
package main
import (
"fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"os"
)
func handleError(err error) {
fmt.Println("Error:", err)
os.Exit(-1)
}
func main() {
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
if err != nil {
handleError(err)
}
bucket, err := client.Bucket("yourBucketName")
if err != nil {
handleError(err)
}
result, err := bucket.GetLiveChannelHistory("mychannel")
if err != nil {
handleError(err)
}
fmt.Println(result)
}Delete a LiveChannel
Disable the LiveChannel before deleting it. Delete requests fail if a client is actively ingesting to the channel.
DeleteLiveChannelremoves only the LiveChannel. Files generated from stream ingest are not deleted.
package main
import (
"fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"os"
)
func handleError(err error) {
fmt.Println("Error:", err)
os.Exit(-1)
}
func main() {
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
if err != nil {
handleError(err)
}
bucket, err := client.Bucket("yourBucketName")
if err != nil {
handleError(err)
}
err = bucket.DeleteLiveChannel("mychannel")
if err != nil {
handleError(err)
}
}What's next
For the complete LiveChannel sample code, see the GitHub sample.
For API details, see the following references: