All Products
Search
Document Center

Object Storage Service:Manage LiveChannels by using OSS SDK for Go

Last Updated:Jan 03, 2024

This topic describes the common operations that you can perform on LiveChannels, such as creating, listing, and deleting LiveChannels.

Create a LiveChannel

Before you can upload audio and video data by using the Real-Time Messaging Protocol (RTMP), you must call the PutLiveChannel operation to create a LiveChannel. The response to the PutLiveChannel request includes the URL that is used to ingest streams to the LiveChannel and the URL that is used to play the ingested streams.

Important

You can use the returned URLs to ingest and play streams. You can also perform operations based on the returned LiveChannel name, such as querying stream ingest status, querying stream ingest records, and disabling stream ingest.

The following code provides an example on how to create 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() {
    /// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
    provider, err := oss.NewEnvironmentVariableCredentialsProvider()
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Create an OSSClient instance. 
    // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
    client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
    if err != nil {
        HandleError(err)
    }

    // Specify the name of the bucket that is used to store the LiveChannel. 
    bucketName := "srcexamplebucket"
    bucket,err := client.Bucket(bucketName)

    if err != nil {
        HandleError(err)
    }

    // Specify the name of the LiveChannel that you want to create. The name cannot contain forward slashes (/). 
    channelName := "mychannel"
    // If you set Type to HLS, you must specify the name of the generated m3u8 file. The name must be 6 to 128 bytes in length and end with .m3u8. 
    playlistName := "playlist.m3u8"
    // Specify the name of the bucket that stores the results of high-frequency snapshot operations. 
    destBucketName := "destexamplebucket"
    // Specify the name of the MNS topic that is used to notify users of the results of high-frequency screenshot operations. 
    notify := "exampletopic"
    target := oss.LiveChannelTarget{
        PlaylistName: playlistName,
        // Specify the format in which the LiveChannel stores the uploaded data. Only the HLS format is supported. 
        Type:         "HLS",
        // If you set Type to HLS, you must specify the number of TS files that are included in the m3u8 file. 
        FragCount:   3,
        // Specify the duration of each TS file. Unit: seconds. 
        FragDuration:   5,
    }
   
    snapshot := oss.LiveChannelSnapshot{
        // Specify the name of the role that is used to perform high-frequency snapshot operations. The role must have the write permissions on DestBucket and the permission to send messages to NotifyTopic. 
        RoleName:    "examplerole",
        // Specify the interval of high-frequency snapshot operations. Unit: seconds. Valid values: 1 to 100. 
        Interval:    10,        
        DestBucket:  destBucketName,
        NotifyTopic: notify,
    }
    config := oss.LiveChannelConfiguration{
        // Specify the description of the LiveChannel. The description can be up to 128 bytes in length. 
        Description : "this is my channel",
        // Specify the status of the LiveChannel. In this example, the Status parameter is set to enabled, which indicates that the LiveChannel is enabled. If you want to disable the LiveChannel, set the Status parameter to disabled. 
        Status:      "enabled",
        Target:      target,
        Snapshot: &snapshot,
    }

    // Create the LiveChannel. 
   data, err := bucket.CreateLiveChannel(channelName, config)
   if err != nil {
       HandleError(err)
   }
   fmt.Println(data)
}

List LiveChannels

The following code provides an example on how to list LiveChannels:

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() {
    /// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
    provider, err := oss.NewEnvironmentVariableCredentialsProvider()
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Create an OSSClient instance. 
    // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
    client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
    if err != nil {
        HandleError(err)
    }

    // Specify the name of the bucket. 
    bucketName := "yourBucketName"
    bucket,err := client.Bucket(bucketName)
    // Specify that LiveChannels whose names contain the test prefix are listed. 
    prefix := "test"

    if err != nil {
        HandleError(err)
    }

    // List the LiveChannels. 
    result, err := bucket.ListLiveChannel(oss.Prefix(prefix))
    if err != nil {
        HandleError(err)
    }
    fmt.Println(result)
}

Configure the status of a LiveChannel

The following code provides an example on how to configure the status 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() {
    /// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
    provider, err := oss.NewEnvironmentVariableCredentialsProvider()
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Create an OSSClient instance. 
    // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
    client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
    if err != nil {
        HandleError(err)
    }

    // Specify the name of the bucket. 
    bucketName := "yourBucketName"
    bucket,err := client.Bucket(bucketName)
    // Specify the name of the LiveChannel. 
    channelName := "mychannel"
   
    if err != nil {
         HandleError(err)
    }

    // A LiveChannel can be in one of the following states: enabled or disabled. 
    // If a LiveChannel is in the disabled state, you cannot ingest streams to the LiveChannel. If you ingest a stream to a LiveChannel that is in the disabled state, your client is disconnected from the LiveChannel after approximately 10 seconds. 
    err = bucket.PutLiveChannelStatus(channelName,"disabled")
    if err != nil {
        HandleError(err)
     }
}

Query the signed URL of a LiveChannel

The following code provides an example on how to query the signed URL of a LiveChannel:

package main

import (
    "fmt"
    "github.com/aliyun/aliyun-oss-go-sdk/oss"
    "os"
)

func main() {

    /// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
    provider, err := oss.NewEnvironmentVariableCredentialsProvider()
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Create an OSSClient instance. 
    // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
    client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Specify the name of the bucket. Example: examplebucket. 
    bucket, err := client.Bucket("examplebucket")
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
    // Specify the name of the LiveChannel. 
    channelName := "test-sign-rtmp-url"
    // Specify the name of the playlist. 
    playlistName := "playlist.m3u8"
    // The expiration parameter indicates the validity period of the signed URL and is a timestamp that follows the UNIX time format. In this example, the validity period is set to 1 hour. 
    expiration := time.Now().Unix() + 3600
    result, err := bucket.SignRtmpURL(channelName,playlistName,expiration)
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
    fmt.Println("Sign Rtmp url:"+result)
}

Query the stream ingest status of a LiveChannel

The following code provides an example on how to query the stream ingest status 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() {
    /// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
    provider, err := oss.NewEnvironmentVariableCredentialsProvider()
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Create an OSSClient instance. 
    // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
    client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
    if err != nil {
        HandleError(err)
    }

    // Specify the name of the bucket. 
    bucketName := "yourBucketName"
    bucket,err := client.Bucket(bucketName)
    // Specify the name of the LiveChannel. 
    channelName := "mychannel"

    if err != nil {
        HandleError(err)
    }

    // Query the stream ingest status of the LiveChannel. 
    result,err := bucket.GetLiveChannelStat(channelName)
    if err != nil {
        HandleError(err)
    }
    fmt.Println(result)
}

Query the configurations of a LiveChannel

The following code provides an example on how to query the configurations 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() {
    /// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
    provider, err := oss.NewEnvironmentVariableCredentialsProvider()
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Create an OSSClient instance. 
    // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
    client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
    if err != nil {
        HandleError(err)
    }

    // Specify the name of the bucket. 
    bucketName := "yourBucketName"
    bucket,err := client.Bucket(bucketName)
    // Specify the name of the LiveChannel. 
    channelName := "mychannel"

    if err != nil {
        HandleError(err)
    }

    // Query the configurations of the LiveChannel. 
    result,err := bucket.GetLiveChannelInfo(channelName)
    if err != nil {
        HandleError(err)
    }
    fmt.Println(result)
}

Generate a playlist for a LiveChannel

You can call the PostVodPlaylist operation to generate a playlist for a LiveChannel that is used for video on demand (VOD). OSS queries the TS files that are generated by the streams ingested to the LiveChannel within a specific time period and converges the files into an M3U8 playlist.

The following code provides an example on how to generate a playlist 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() {
    /// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
    provider, err := oss.NewEnvironmentVariableCredentialsProvider()
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Create an OSSClient instance. 
    // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
    client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
    if err != nil {
        HandleError(err)
    }

    // Specify the name of the bucket. 
    bucketName := "yourBucketName"
    bucket,err := client.Bucket(bucketName)
    // Specify the name of the LiveChannel. 
    channelName := "mychannel"
    playlistName := "playlist.m3u8"
    // Specify the end time of the time range during which the TS files that you want to query are generated. By default, the end time is the same as the current time. 
    endTime := time.Now().Add(time.Minute)
    // Specify the start time of the time range during which the TS files that you want to query are generated. By default, the start time is 60 minutes earlier than the current time. 
    startTime := endTime.Add(-60 * time.Minute)

    if err != nil {
        HandleError(err)
    }

    // Generate a playlist for the LiveChannel. 
    err = bucket.PostVodPlaylist(channelName,playlistName,startTime,endTime)
    if err != nil {
        HandleError(err)
    }
}

Query the playlist of a LiveChannel

The following code provides an example on how to query the playlist that is generated by the streams ingested to a LiveChannel within the specified time range:

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() {
    /// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
    provider, err := oss.NewEnvironmentVariableCredentialsProvider()
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Create an OSSClient instance. 
    // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
    client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
    if err != nil {
        HandleError(err)
    }

    // Specify the name of the bucket. 
    bucketName := "yourBucketName"
    bucket,err := client.Bucket(bucketName)
    // Specify the name of the LiveChannel. 
    channelName := "mychannel"
    
    // Specify the end time of the time range during which the TS files that you want to query are generated. By default, the end time is the same as the current time. 
    endTime := time.Now().Add(time.Minute)
    // Specify the start time of the time range during which the TS files that you want to query are generated. By default, the start time is 60 minutes earlier than the current time. 
    startTime := endTime.Add(-60 * time.Minute)

    if err != nil {
        HandleError(err)
    }

    // Query the playlist of the LiveChannel. 
    result,err := bucket.GetVodPlaylist(channelName,startTime,endTime)
    if err != nil {
        HandleError(err)
    }
    fmt.Println(result)
}

Query the stream ingest records of a LiveChannel

You can call the GetLiveChannelHistory operation to query up to the 10 most recent stream ingest records of a LiveChannel.

The following code provides an example on how to query the stream ingest records 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() {
    /// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
    provider, err := oss.NewEnvironmentVariableCredentialsProvider()
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Create an OSSClient instance. 
    // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
    client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
    if err != nil {
        HandleError(err)
    }

    // Specify the name of the bucket. 
    bucketName := "yourBucketName"
    bucket,err := client.Bucket(bucketName)
    // Specify the name of the LiveChannel. 
    channelName := "mychannel"

    if err != nil {
        HandleError(err)
    }

    // Query the stream ingest records of the LiveChannel. 
    result,err := bucket.GetLiveChannelHistory(channelName)
    if err != nil {
        HandleError(err)
    }
    fmt.Println(result)
}

Delete a LiveChannel

Important
  • If you send a DeleteLiveChannel request to delete a LiveChannel to which a client is ingesting streams, the request fails.

  • The DeleteLiveChannel operation deletes only the LiveChannel and does not delete the files that are generated by the streams ingested to the LiveChannel.

The following code provides an example on how to delete 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() {
    /// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
    provider, err := oss.NewEnvironmentVariableCredentialsProvider()
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Create an OSSClient instance. 
    // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
    client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
    if err != nil {
        HandleError(err)
    }

    // Specify the name of the bucket. 
    bucketName := "yourBucketName"
    bucket,err := client.Bucket(bucketName)
    // Specify the name of the LiveChannel. 
    channelName := "mychannel"

    if err != nil {
        HandleError(err)
    }

    // Delete the LiveChannel. 
    err := bucket.DeleteLiveChannel(channelName)
    if err != nil {
        HandleError(err)
    }
 }

References

  • For more information about the complete sample code of LiveChannels, visit GitHub.

  • For more information about the API operation that you can call to create a LiveChannel, see PutLiveChannel.

  • For more information about the API operation that you can call to list LiveChannels, see ListLiveChannel.

  • For more information about the API operation that you can call to configure the status of a LiveChannel, see PutLiveChannelStatus.

  • For more information about the API operation that you can call to query the stream ingest status of a LiveChannel, see GetLiveChannelStat.

  • For more information about the API operation that you can call to query the configurations of a LiveChannel, see GetLiveChannelInfo.

  • For more information about the API operation that you can call to generate a playlist for a LiveChannel, see PostVodPlaylist.

  • For more information about the API operation that you can call to query the playlist of a LiveChannel, see GetVodPlaylist.

  • For more information about the API operation that you can call to query the stream ingest records of a LiveChannel, see GetLiveChannelHistory.

  • For more information about the API operation that you can call to delete a LiveChannel, see DeleteLiveChannel.