百度网盘开放平台 Go SDK,提供对百度网盘开放 API 的封装,方便开发者快速集成百度网盘功能。 本项目基于百度网盘开放 API 接入,不依赖百度网盘 Web 端接口。
- 用户认证(OAuth2)
- 获取用户信息和网盘配额
- 文件管理(上传、下载、移动、复制、删除、重命名等)
- 文件列表查询
- 支持大文件分片上传
go get github.com/lfhy/xpanimport "github.com/lfhy/xpan/client"
// 创建客户端实例
c := client.New()
// 设置认证信息
c.SetAuth(&auth.AuthEnv{
ClientID: "your-client-id",
ClientSecret: "your-client-secret",
AccessToken: "your-access-token",
RefreshToken: "your-refresh-token",
})import "github.com/lfhy/xpan/user"
userInfo, err := user.GetUserInfo(&user.UserInfoReq{})
if err != nil {
log.Fatal(err)
}
fmt.Printf("用户名: %s\n", userInfo.NetDiskName)import "github.com/lfhy/xpan/file"
// 创建目录
err := file.Mkdir(&file.MkdirReq{
Path: "/my-folder",
})
// 列出文件
files, err := file.List(&file.ListReq{
Path: "/",
})推荐直接使用客户端封装,它会先调用 LocateUpload 获取正确的上传域名:
package main
import (
"log"
"os"
"github.com/lfhy/xpan/auth"
"github.com/lfhy/xpan/client"
)
func main() {
c := client.New(&auth.AuthEnv{
ClientId: "your-client-id",
ClientSecret: "your-client-secret",
AccessToken: "your-access-token",
RefreshToken: "your-refresh-token",
})
f, err := os.Open("./example.txt")
if err != nil {
log.Fatal(err)
}
defer f.Close()
res, err := c.PutObject("/apps/your-app/example.txt", f)
if err != nil {
log.Fatal(err)
}
log.Printf("upload ok: path=%s size=%d fsid=%d", res.Path, res.Size, res.FsId)
}如果你要直接调用底层 file.Upload,可以把 uploadHost 留空,让 SDK 自动调用 LocateUpload 获取 host;如果传了 uploadHost,则应使用 LocateUpload 返回的 host:
package main
import (
"log"
"os"
"github.com/lfhy/xpan/file"
)
func main() {
remotePath := "/apps/your-app/example.txt"
f, err := os.Open("./example.txt")
if err != nil {
log.Fatal(err)
}
defer f.Close()
uploadRes, err := file.Upload(&file.UploadReq{
Path: remotePath,
File: f,
}, "")
if err != nil {
log.Fatal(err)
}
log.Printf("upload ok: path=%s size=%d fsid=%d", uploadRes.Path, uploadRes.Size, uploadRes.FsId)
}本项目采用 MIT 许可证,详情请见 LICENSE 文件。