github.com/aavshr/aws-sdk-go@v1.41.3/example/service/mediastoredata/streamingNonSeekableReader/main.go (about)

     1  //go:build example
     2  // +build example
     3  
     4  package main
     5  
     6  import (
     7  	"fmt"
     8  	"io"
     9  	"log"
    10  	"math/rand"
    11  	"os"
    12  	"time"
    13  
    14  	"github.com/aavshr/aws-sdk-go/aws"
    15  	"github.com/aavshr/aws-sdk-go/aws/session"
    16  	"github.com/aavshr/aws-sdk-go/service/mediastore"
    17  	"github.com/aavshr/aws-sdk-go/service/mediastoredata"
    18  )
    19  
    20  func main() {
    21  	containerName := os.Args[1]
    22  	objectPath := os.Args[2]
    23  
    24  	// Create the SDK's session, and a AWS Elemental MediaStore Data client.
    25  	sess := session.Must(session.NewSession())
    26  	dataSvc, err := getMediaStoreDataClient(containerName, sess)
    27  	if err != nil {
    28  		log.Fatalf("failed to create client, %v", err)
    29  	}
    30  
    31  	// Create a random reader to simulate a unseekable reader, wrap the reader
    32  	// in an io.LimitReader to prevent uploading forever.
    33  	randReader := rand.New(rand.NewSource(0))
    34  	reader := io.LimitReader(randReader, 1024*1024 /* 1MB */)
    35  
    36  	// Wrap the unseekable reader with the SDK's RandSeekCloser. This type will
    37  	// allow the SDK's to use the nonseekable reader.
    38  	body := aws.ReadSeekCloser(reader)
    39  
    40  	// make the PutObject API call with the nonseekable reader, causing the SDK
    41  	// to send the request body payload as chunked transfer encoding.
    42  	_, err = dataSvc.PutObject(&mediastoredata.PutObjectInput{
    43  		Path: &objectPath,
    44  		Body: body,
    45  	})
    46  	if err != nil {
    47  		log.Fatalf("failed to upload object, %v", err)
    48  	}
    49  
    50  	fmt.Println("object uploaded")
    51  }
    52  
    53  // getMediaStoreDataClient uses the AWS Elemental MediaStore API to get the
    54  // endpoint for a container. If the container endpoint can be retrieved a AWS
    55  // Elemental MediaStore Data client will be created and returned. Otherwise
    56  // error is returned.
    57  func getMediaStoreDataClient(containerName string, sess *session.Session) (*mediastoredata.MediaStoreData, error) {
    58  	endpoint, err := containerEndpoint(containerName, sess)
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  
    63  	dataSvc := mediastoredata.New(sess, &aws.Config{
    64  		Endpoint: endpoint,
    65  	})
    66  
    67  	return dataSvc, nil
    68  }
    69  
    70  // ContainerEndpoint will attempt to get the endpoint for a container,
    71  // returning error if the container doesn't exist, or is not active within a
    72  // timeout.
    73  func containerEndpoint(name string, sess *session.Session) (*string, error) {
    74  	for i := 0; i < 3; i++ {
    75  		ctrlSvc := mediastore.New(sess)
    76  		descResp, err := ctrlSvc.DescribeContainer(&mediastore.DescribeContainerInput{
    77  			ContainerName: &name,
    78  		})
    79  		if err != nil {
    80  			return nil, err
    81  		}
    82  
    83  		if status := aws.StringValue(descResp.Container.Status); status != "ACTIVE" {
    84  			log.Println("waiting for container to be active, ", status)
    85  			time.Sleep(10 * time.Second)
    86  			continue
    87  		}
    88  
    89  		return descResp.Container.Endpoint, nil
    90  	}
    91  
    92  	return nil, fmt.Errorf("container is not active")
    93  }