ling-baseling-base

第三方服务

OCR / 内容审核 / 对象存储 / 消息队列

在线 Playground

在浏览器中直接体验本页相关 API,无需本地安装 Go 环境。

第三方服务

对接云厂商 OCR、内容审核、对象存储和消息队列。核心包只定义接口,各厂商在独立子 module 中实现,避免拉入不需要的 SDK。

模块导航

能力对照

模块核心接口后端数量典型场景
storesStore(Read/Write/Delete/Exists/PublicURL)9用户上传、静态资源、预签名直传
ocrProvider.Recognize6发票/证件识别、图片转文字
censorTextCensor / ImageCensor / VideoCensor3 云UGC 发帖前审核、直播回放审核
mqBroker / Producer / Consumer5异步任务、事件驱动、解耦服务

按需安装

每个云厂商是独立 Go module,只拉你用的 SDK:

# 对象存储 — 阿里云 OSS
go get github.com/LingByte/ling-base/stores/oss

# OCR — 腾讯云
go get github.com/LingByte/ling-base/providers/ocr/qcloud

# 内容审核 — 七牛
go get github.com/LingByte/ling-base/providers/censor/qiniu

# 消息队列 — RabbitMQ
go get github.com/LingByte/ling-base/common/mq/rabbitmq

对象存储快速示例

import (
    "bytes"
    "github.com/LingByte/ling-base/stores"
    "github.com/LingByte/ling-base/stores/oss"
)

store, _ := oss.New(oss.Config{
    Endpoint:        "oss-cn-hangzhou.aliyuncs.com",
    AccessKeyID:     "...",
    AccessKeySecret: "...",
    Bucket:          "my-bucket",
})

key := "uploads/2026/photo.jpg"
_ = store.Write(key, bytes.NewReader(imageBytes))

rc, size, _ := store.Read(key)
defer rc.Close()

url, _ := stores.SignedURL(store, key, time.Hour) // 私有桶临时链接

OCR 快速示例

import (
    "github.com/LingByte/ling-base/providers/ocr"
    "github.com/LingByte/ling-base/providers/ocr/aliyun"
)

p := aliyun.NewFromEnv()
ocr.RegisterProvider("aliyun", p)
_ = ocr.SetProviderByDriver("aliyun")

text, err := ocr.GetProvider().Recognize(ctx, imageBytes, &ocr.Options{Language: "zh"})

内容审核快速示例

import qiniu "github.com/LingByte/ling-base/providers/censor/qiniu"

c, _ := qiniu.NewTextCensor(qiniu.Config{AccessKey: "...", SecretKey: "..."})
result, _ := c.CensorText(ctx, userComment)
if result.Suggestion == "block" {
    return ErrBlocked
}

消息队列快速示例

import (
    "github.com/LingByte/ling-base/common/mq"
    "github.com/LingByte/ling-base/common/mq/factory"
    "github.com/LingByte/ling-base/common/mq/rabbitmq"
)

broker, _ := factory.NewBroker(mq.BrokerConfig{
    Type: mq.BrokerRabbitMQ,
    RabbitMQConfig: &rabbitmq.Config{URL: "amqp://guest:guest@localhost:5672/"},
})
_ = broker.Connect()
defer broker.Close()

producer, _ := broker.Producer("events", mq.PublishOptions{})
_ = producer.Publish(ctx, &mq.Message{Body: []byte(`{"event":"order.created"}`)})

设计原则

原则说明
接口统一业务依赖 stores.Storeocr.Providercensor.TextCensormq.Broker
厂商隔离每个云厂商独立 module,未使用的 SDK 不进入依赖树
可替换换云通常只需换 import 与构造参数
零核心依赖storesocrcensor 核心包无云 SDK

与 common / infrastructure 的关系

On this page