Todos los productos
Search
Centro de documentos

Object Storage Service:Gestión de LiveChannels (Go SDK V1)

Última actualización: Sep 03, 2026

Este tema describe las operaciones comunes en LiveChannels, como crear, listar y eliminar LiveChannels.

Crear un LiveChannel

Antes de cargar datos de audio y video mediante el protocolo RTMP (Real-Time Messaging Protocol), debe llamar a la operación PutLiveChannel para crear un LiveChannel. Esta operación devuelve una URL de ingesta RTMP y la URL de reproducción correspondiente.

Importante

Puede utilizar las URL devueltas para la ingesta y reproducción de transmisiones. También puede usar el nombre del LiveChannel para realizar operaciones relacionadas, como consultar el estado de la ingesta, consultar los registros de ingesta y deshabilitar la ingesta de transmisiones.

A continuación se muestra cómo crear un 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 this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
    provider, err := oss.NewEnvironmentVariableCredentialsProvider()
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Create an OSSClient instance.
    // Set the Endpoint to the one that corresponds to your bucket. For example, if your bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the Endpoint as needed.
    client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
    if err != nil {
        HandleError(err)
    }

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

    if err != nil {
        HandleError(err)
    }

    // Specify the LiveChannel name. The name cannot contain forward slashes (/).
    channelName := "mychannel"
    // If Type is set to HLS, specify the name of the generated .m3u8 file. The name must end with .m3u8 and be 6 to 128 bytes in length.
    playlistName := "playlist.m3u8"
    // Specify the name of the bucket that stores high-frequency snapshots.
    destBucketName := "destexamplebucket"
    // Specify the topic name of the lightweight MSMQ. This topic is used to notify users of the results of high-frequency snapshot operations.
    notify := "exampletopic"
    target := oss.LiveChannelTarget{
        PlaylistName: playlistName,
        // Specify the dump type. Only HLS is supported.
        Type:         "HLS",
        // If Type is set to HLS, specify the number of .ts files in the .m3u8 file.
        FragCount:   3,
        // Specify the duration of each .ts file in seconds.
        FragDuration:   5,
    }
   
    snapshot := oss.LiveChannelSnapshot{
        // The name of the role used for high-frequency snapshot operations. The role must have write permissions on DestBucket and permissions to send messages to NotifyTopic.
        RoleName:    "examplerole",
        // The interval for high-frequency snapshots in seconds. The value must be between 1 and 100.
        Interval:    10,        
        DestBucket:  destBucketName,
        NotifyTopic: notify,
    }
    config := oss.LiveChannelConfiguration{
        // Specify the description of the LiveChannel. The maximum length is 128 bytes.
        Description : "this is my channel",
        // Specify the status of the LiveChannel. In this example, the status is set to enabled. To disable the LiveChannel, set this 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)
}

Listar LiveChannels

A continuación se muestra cómo listar LiveChannels específicos.

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 this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
    provider, err := oss.NewEnvironmentVariableCredentialsProvider()
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Create an OSSClient instance.
    // Set the Endpoint to the one that corresponds to your bucket. For example, if your bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the Endpoint as needed.
    client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
    if err != nil {
        HandleError(err)
    }

    // Specify the bucket name.
    bucketName := "yourBucketName"
    bucket,err := client.Bucket(bucketName)
    // List all LiveChannels that have the "test" prefix.
    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)
}

Establecer el estado de un LiveChannel

A continuación se muestra cómo establecer el estado de un 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 this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
    provider, err := oss.NewEnvironmentVariableCredentialsProvider()
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Create an OSSClient instance.
    // Set the Endpoint to the one that corresponds to your bucket. For example, if your bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the Endpoint as needed.
    client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
    if err != nil {
        HandleError(err)
    }

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

    // A LiveChannel can be enabled or disabled.
    // If a LiveChannel is disabled, OSS denies stream ingest requests to this LiveChannel. If a client is ingesting a stream to this LiveChannel, the client is disconnected. This may take about 10 seconds.
    err = bucket.PutLiveChannelStatus(channelName,"disabled")
    if err != nil {
        HandleError(err)
     }
}

Obtener una URL firmada para un LiveChannel

A continuación se muestra cómo obtener una URL firmada para un 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 this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
    provider, err := oss.NewEnvironmentVariableCredentialsProvider()
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Create an OSSClient instance.
    // Set the Endpoint to the one that corresponds to your bucket. For example, if your bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the Endpoint as needed.
    client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Specify the bucket name, for example, examplebucket.
    bucket, err := client.Bucket("examplebucket")
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
    // Specify the LiveChannel name.
    channelName := "test-sign-rtmp-url"
    // Specify the playlist name.
    playlistName := "playlist.m3u8"
    // The expiration time in a UNIX timestamp. This example sets the expiration time to one hour from now.
    expiration := time.Now().Unix() + 3600
    result, err := bucket.SignRtmpURL(channelName,playlistName,expiration)
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
    fmt.Println("Signed RTMP URL:" + result)
}

Obtener la información de estado de un LiveChannel

A continuación se muestra cómo recuperar el estado de ingesta de un LiveChannel específico.

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 this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
    provider, err := oss.NewEnvironmentVariableCredentialsProvider()
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Create an OSSClient instance.
    // Set the Endpoint to the one that corresponds to your bucket. For example, if your bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the Endpoint as needed.
    client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
    if err != nil {
        HandleError(err)
    }

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

    if err != nil {
        HandleError(err)
    }

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

Obtener la configuración de un LiveChannel

A continuación se muestra cómo recuperar la configuración de un LiveChannel específico.

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 this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
    provider, err := oss.NewEnvironmentVariableCredentialsProvider()
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Create an OSSClient instance.
    // Set the Endpoint to the one that corresponds to your bucket. For example, if your bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the Endpoint as needed.
    client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
    if err != nil {
        HandleError(err)
    }

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

    if err != nil {
        HandleError(err)
    }

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

Generar una lista de reproducción para un LiveChannel

La operación PostVodPlaylist genera una lista de reproducción de video bajo demanda (VOD) para un LiveChannel específico. OSS busca los archivos .ts generados por la ingesta de transmisiones en el LiveChannel dentro de un intervalo de tiempo determinado y los combina en una lista de reproducción .m3u8.

A continuación se muestra cómo generar una lista de reproducción para un 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 this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
    provider, err := oss.NewEnvironmentVariableCredentialsProvider()
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Create an OSSClient instance.
    // Set the Endpoint to the one that corresponds to your bucket. For example, if your bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the Endpoint as needed.
    client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
    if err != nil {
        HandleError(err)
    }

    // Specify the bucket name.
    bucketName := "yourBucketName"
    bucket,err := client.Bucket(bucketName)
    // Specify the LiveChannel name.
    channelName := "mychannel"
    playlistName := "playlist.m3u8"
    // Specify the end time to query for .ts files. The default value is the current time.
    endTime := time.Now().Add(time.Minute)
    // Specify the start time to query for .ts files. The default value is 60 minutes before the current time.
    startTime := endTime.Add(-60 * time.Minute)

    if err != nil {
        HandleError(err)
    }

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

Consultar la lista de reproducción de un LiveChannel

A continuación se muestra cómo consultar la lista de reproducción generada por la ingesta de transmisiones para un LiveChannel específico durante un periodo de tiempo determinado.

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 this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
    provider, err := oss.NewEnvironmentVariableCredentialsProvider()
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Create an OSSClient instance.
    // Set the Endpoint to the one that corresponds to your bucket. For example, if your bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the Endpoint as needed.
    client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
    if err != nil {
        HandleError(err)
    }

    // Specify the bucket name.
    bucketName := "yourBucketName"
    bucket,err := client.Bucket(bucketName)
    // Specify the LiveChannel name.
    channelName := "mychannel"
    
    // Specify the end time to query for .ts files. The default value is the current time.
    endTime := time.Now().Add(time.Minute)
    // Specify the start time to query for .ts files. The default value is 60 minutes before the current time.
    startTime := endTime.Add(-60 * time.Minute)

    if err != nil {
        HandleError(err)
    }

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

Obtener los registros de ingesta de un LiveChannel

La operación GetLiveChannelHistory devuelve los últimos 10 registros de ingesta de un LiveChannel específico.

A continuación se muestra cómo recuperar los registros de ingesta de un 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 this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
    provider, err := oss.NewEnvironmentVariableCredentialsProvider()
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Create an OSSClient instance.
    // Set the Endpoint to the one that corresponds to your bucket. For example, if your bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the Endpoint as needed.
    client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
    if err != nil {
        HandleError(err)
    }

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

    if err != nil {
        HandleError(err)
    }

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

Eliminar un LiveChannel

Importante
  • Si un cliente está realizando una ingesta de transmisión hacia el LiveChannel, la solicitud de eliminación fallará.

  • La operación DeleteLiveChannel solo elimina el LiveChannel. No elimina los archivos generados por la ingesta de transmisiones.

A continuación se muestra cómo eliminar un LiveChannel específico.

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 this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
    provider, err := oss.NewEnvironmentVariableCredentialsProvider()
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Create an OSSClient instance.
    // Set the Endpoint to the one that corresponds to your bucket. For example, if your bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the Endpoint as needed.
    client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
    if err != nil {
        HandleError(err)
    }

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

    if err != nil {
        HandleError(err)
    }

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

Referencias

  • Para ver el código de ejemplo completo de LiveChannel, consulte el ejemplo en GitHub.

  • Para obtener más información sobre la operación de API PutLiveChannel, consulte PutLiveChannel.

  • Para obtener más información sobre la operación de API ListLiveChannel, consulte ListLiveChannel.

  • Para obtener más información sobre la operación de API PutLiveChannelStatus, consulte PutLiveChannelStatus.

  • Para obtener más información sobre la operación de API GetLiveChannelStat, consulte GetLiveChannelStat.

  • Para obtener más información sobre la operación de API GetLiveChannelInfo, consulte GetLiveChannelInfo.

  • Para obtener más información sobre la operación de API PostVodPlaylist, consulte PostVodPlaylist.

  • Para obtener más información sobre la operación de API GetVodPlaylist, consulte GetVodPlaylist.

  • Para obtener más información sobre la operación de API GetLiveChannelHistory, consulte GetLiveChannelHistory.

  • Para obtener más información sobre la operación de API DeleteLiveChannel, consulte DeleteLiveChannel.