112 lines
2.8 KiB
Go
112 lines
2.8 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"repository.basistech.ru/de-golang-sdk"
|
|
"repository.basistech.ru/de-golang-sdk/core"
|
|
"repository.basistech.ru/de-golang-sdk/tags/de_istore"
|
|
)
|
|
|
|
func main() {
|
|
if err := run(); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func run() error {
|
|
const imageReference = "0.1"
|
|
|
|
var (
|
|
serverURL = os.Getenv("DESDK_SERVER_URL")
|
|
token = os.Getenv("DESDK_TOKEN")
|
|
|
|
uploadFilePath = os.Getenv("DESDK_UPLOAD_FILE_PATH")
|
|
downloadFilePath = os.Getenv("DESDK_DOWNLOAD_FILE_PATH")
|
|
)
|
|
if uploadFilePath == "" {
|
|
uploadFilePath = "examples/05_file/hello_world.img"
|
|
}
|
|
if downloadFilePath == "" {
|
|
downloadFilePath = "examples/05_file/downloaded_image.img"
|
|
}
|
|
if serverURL == "" || token == "" {
|
|
return fmt.Errorf("DESDK_SERVER_URL and DESDK_TOKEN must be set")
|
|
}
|
|
|
|
client, err := desdk.NewClient(serverURL, false)
|
|
if err != nil {
|
|
return fmt.Errorf("create client: %w", err)
|
|
}
|
|
|
|
authCtx, authCancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer authCancel()
|
|
|
|
if err := client.AuthenticateWithToken(authCtx, token); err != nil {
|
|
return fmt.Errorf("authenticate with token: %w", err)
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
|
|
defer cancel()
|
|
|
|
uploadFile, err := os.Open(uploadFilePath)
|
|
if err != nil {
|
|
return fmt.Errorf("open upload file: %w", err)
|
|
}
|
|
defer uploadFile.Close()
|
|
|
|
imageName := fmt.Sprintf("sdk-file-example-%d", time.Now().UnixNano())
|
|
imageUploadResp, err := client.DeIstore.ImageUpload(ctx, de_istore.ImageUploadRequest{
|
|
Name: imageName,
|
|
Reference: imageReference,
|
|
File: &core.UploadFile{
|
|
Filename: filepath.Base(uploadFilePath),
|
|
Reader: uploadFile,
|
|
},
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("upload image: %w", err)
|
|
}
|
|
fmt.Printf("Imported image %q with ID %q\n", imageName, imageUploadResp.Manifest.ID)
|
|
defer func() {
|
|
cleanupCtx, cleanupCancel := context.WithTimeout(context.Background(), time.Minute)
|
|
defer cleanupCancel()
|
|
|
|
_, cleanupErr := client.DeIstore.ImageDelete(cleanupCtx, de_istore.ImageDeleteRequest{
|
|
ID: imageUploadResp.Manifest.ID,
|
|
})
|
|
if cleanupErr != nil {
|
|
log.Printf("delete image %q: %v", imageName, cleanupErr)
|
|
}
|
|
}()
|
|
|
|
imageReader, err := client.DeIstore.ImageDownload(ctx, de_istore.ImageDownloadRequest{
|
|
Path: imageName,
|
|
Reference: imageReference,
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("download image: %w", err)
|
|
}
|
|
defer imageReader.Close()
|
|
|
|
downloadFile, err := os.Create(downloadFilePath)
|
|
if err != nil {
|
|
return fmt.Errorf("create download file: %w", err)
|
|
}
|
|
defer downloadFile.Close()
|
|
|
|
written, err := io.Copy(downloadFile, imageReader)
|
|
if err != nil {
|
|
return fmt.Errorf("save image: %w", err)
|
|
}
|
|
fmt.Printf("Downloaded %d bytes to %q\n", written, downloadFilePath)
|
|
|
|
return nil
|
|
}
|