Last active
December 22, 2022 05:19
-
-
Save JanPetr/1318eb723c8d1557c412b893641fe428 to your computer and use it in GitHub Desktop.
Upload asset to Pex via TUS client - Golang example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"crypto/sha256" | |
"fmt" | |
"io" | |
"os" | |
"github.com/eventials/go-tus" // install with `$ go get github.com/eventials/go-tus` | |
"github.com/go-resty/resty/v2" // install with `$ go get github.com/go-resty/resty/v2` | |
) | |
func main() { | |
var ( | |
filename = "FIXME" // file to upload | |
apiKey = "FIXME" // use your actual key from page https://drm.pex.com/settings/api-keys | |
chunkSize = 100 * 1024 * 1024 // 100 MB | |
) | |
restClient := resty.New() | |
f, err := os.Open(filename) | |
checkErr(err) | |
defer f.Close() | |
// Get FID | |
buff := make([]byte, 512*1024) | |
_, err = io.ReadFull(f, buff[:]) | |
checkErr(err) | |
h := sha256.New() | |
_, err = io.Copy(h, bytes.NewReader(buff)) | |
checkErr(err) | |
fid := fmt.Sprintf("%x", h.Sum(nil)) | |
// Set asset's metadata, see available fields in docs: https://docs.api.pex.com/#create-an-asset-upload | |
metadata := `{ | |
"title": "example", | |
"production_date":"2014-02-14", | |
"media_type":"video", | |
"mime_type":"video/x-msvideo", | |
"fid":"` + fid + `", | |
"collection_ids":[ | |
123 | |
] | |
}` | |
fmt.Println("Preparing upload ...") | |
result := Response{} | |
req := restClient.R().SetAuthToken(apiKey).SetBody(metadata).SetResult(&result) | |
resp, err := req.Post("https://api.pex.com/v4/cms/assets/upload") | |
checkErr(err) | |
fmt.Println("Upload prepared, response:", resp.String()) | |
// Prepare TUS client | |
cfg := tus.DefaultConfig() | |
cfg.ChunkSize = chunkSize | |
cfg.Header.Add("Authorization", "Bearer "+apiKey) | |
cfg.Header.Add("Content-Length", "0") | |
cfg.Header.Add("Blob-ID", result.BlobID) | |
tusClient, err := tus.NewClient("https://api.pex.com/v4/cms/assets/file-upload/", cfg) | |
checkErr(err) | |
// Upload asset file | |
upl, err := tus.NewUploadFromFile(f) | |
checkErr(err) | |
uploader, err := tusClient.CreateUpload(upl) | |
checkErr(err) | |
progress := make(chan tus.Upload) | |
uploader.NotifyUploadProgress(progress) | |
go func() { | |
for p := range progress { | |
fmt.Println("CHUNK UPLOADED", "Progress (%):", p.Progress()) | |
} | |
}() | |
fmt.Println("File is being uploaded ...") | |
err = uploader.Upload() | |
checkErr(err) | |
// Verify & confirm upload | |
fmt.Println("Upload is done, verifying ...") | |
verifyURL := fmt.Sprintf("https://api.pex.com/v3/cms/assets/%s/verify", result.AssetIDStr) | |
verifyResp, err := restClient.R().SetAuthToken(apiKey).Get(verifyURL) | |
checkErr(err) | |
fmt.Println("Done! Verify response:", verifyResp.String()) | |
} | |
type Response struct { | |
AssetID int64 `json:"asset_id"` | |
AssetIDStr string `json:"asset_id_str"` | |
BlobID string `json:"blob_id"` | |
} | |
func checkErr(err error) { | |
if err != nil { | |
if cErr, ok := err.(tus.ClientError); ok { | |
fmt.Println("ERROR BODY", string(cErr.Body)) | |
} | |
panic(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks a lot!