Skip to content
lfhyPublic

About

百度网盘开放平台 Go SDK 客户端封装 类S3风格 | baidu pan go client like s3

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Latest commit

 

History

37 Commits

Folders and files

Repository files navigation

xpan - 百度网盘 Go SDK

License: MIT

百度网盘开放平台 Go SDK,提供对百度网盘开放 API 的封装,方便开发者快速集成百度网盘功能。 本项目基于百度网盘开放 API 接入,不依赖百度网盘 Web 端接口。

功能特性

  • 用户认证(OAuth2)
  • 获取用户信息和网盘配额
  • 文件管理(上传、下载、移动、复制、删除、重命名等)
  • 文件列表查询
  • 支持大文件分片上传

安装

go get github.com/lfhy/xpan

快速开始

初始化客户端

import "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 文件。

About

百度网盘开放平台 Go SDK 客户端封装 类S3风格 | baidu pan go client like s3

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages