目录

Overview

English | 中文README

HiAgent-SDK is the SDK of the HiAgent product from Volcano Engine. Developers can use this SDK to quickly develop functions and improve development efficiency. HiAgent-SDK provides a complete AI native application development suite, including a rich set of development components and application example code.

Architecture

img.png

Quick Start

package main

import (
    "context"
    "crypto/sha256"
    "encoding/hex"
    "fmt"
    "io"
    "log"
    "os"
    "strings"

    "github.com/google/uuid"
    "github.com/volcengine/hiagent-go-sdk/service/up"
)

func calculateSHA256(filePath string) (string, error) {
    file, err := os.Open(filePath)
    if err != nil {
        return "", err
    }
    defer file.Close()

    hash := sha256.New()
    if _, err := io.Copy(hash, file); err != nil {
        return "", err
    }

    return hex.EncodeToString(hash.Sum(nil)), nil
}

func main() {
    ctx := context.Background()

    // Get credentials from environment
    ak := os.Getenv("VOLC_ACCESSKEY")
    sk := os.Getenv("VOLC_SECRETKEY")
    uploadEndpoint := os.Getenv("HIAGENT_UP_UPLOAD_ENDPOINT")

    // Create upload client
    client := up.New(uploadEndpoint, ak, sk)

    // 1. Open file to upload
    testFile, err := os.Open("example.txt")
    if err != nil {
        log.Fatalf("Failed to open file: %v", err)
    }
    defer testFile.Close()

    // Calculate file hash
    fileHash, err := calculateSHA256("example.txt")
    if err != nil {
        log.Fatalf("Failed to calculate SHA256: %v", err)
    }

    // 2. Upload file
    uploadReq := up.UploadRawRequest{
        ID:          strings.ReplaceAll(uuid.New().String(), "-", ""),
        ContentType: "text/plain",
        Expire:      "15h",
        Sha256:      fileHash,
    }

    uploadResp, err := client.UploadRaw(ctx, uploadReq, testFile)
    if err != nil {
        log.Fatalf("Upload failed: %v", err)
    }
    fmt.Printf("Upload successful. Path: %s, Size: %d\n", uploadResp.Path, uploadResp.Size)

    // 3. Get download key
    downloadKeyResp, err := client.DownloadKey(ctx, uploadResp.Path)
    if err != nil {
        log.Fatalf("Get download key failed: %v", err)
    }
    fmt.Printf("Download key obtained: %s\n", downloadKeyResp.Key)

    // 4. Download file
    downloadBody, err := client.Download(ctx, uploadResp.Path, downloadKeyResp.Key)
    if err != nil {
        log.Fatalf("Download failed: %v", err)
    }

    // Save downloaded file
    saveFile, err := os.Create("downloaded.txt")
    if err != nil {
        log.Fatalf("Failed to create save file: %v", err)
    }
    defer saveFile.Close()

    _, err = io.Copy(saveFile, downloadBody)
    if err != nil {
        log.Fatalf("Failed to save downloaded file: %v", err)
    }
    fmt.Println("File downloaded successfully to downloaded.txt")

    // 5. Delete file
    deleteReq := up.DeleteRequest{
        ID:     uploadReq.ID,
        Sha256: fileHash,
    }

    err = client.Delete(ctx, deleteReq)
    if err != nil {
        log.Fatalf("Delete failed: %v", err)
    }
    fmt.Println("File deleted successfully")
}

Security

If you discover a potential security issue in this project, or think you may have discovered a security issue, we ask that you notify Bytedance Security via our security center or vulnerability reporting email.

Please do not create a public GitHub issue.

License

This project is licensed under the Apache-2.0 License.

关于
65.0 KB
邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

版权所有:中国计算机学会技术支持:开源发展技术委员会
京ICP备13000930号-9 京公网安备 11010802032778号