aliyundrive

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 28, 2021 License: MIT Imports: 29 Imported by: 1

README

aliyundrive

FOSSA Status

本项目是基于阿里云盘网页接口封装的 SDK 工具包,可以用于扩展开发其它功能,包含以下特性:

  • 文件下载 URL 获取
  • 文件分片上传
  • 秒传(基于 proof code v1 秒传)
  • 文件移动、重命名、删除等操作
  • 文件批量操作(移动)
  • 文件上传限速

使用

go get github.com/jakeslee/aliyundrive

安装后使用以下方式使用:

package main

import (
	"github.com/jakeslee/aliyundrive"
	"log"
	"os"
)

func main() {
	drive := aliyundrive.NewClient(&aliyundrive.Options{
		AutoRefresh: true,
		UploadRate:  2 * 1024 * 1024, // 限速 2MBps
	})

	cred, err := drive.AddCredential(aliyundrive.NewCredential(&aliyundrive.Credential{
		RefreshToken: "aliyundrive refresh token",
	}))

	file, err := os.OpenFile("/tmp/demo", os.O_RDONLY, 0)
	if err != nil {
		log.Fatal(err)
	}

	fileRapid, rapid, err := drive.UploadFileRapid(cred, &aliyundrive.UploadFileRapidOptions{
		UploadFileOptions: aliyundrive.UploadFileOptions{
			Name:         "name",
			Size:         1000,
			ParentFileId: aliyundrive.DefaultRootFileId,
		},
		File: file,
	})

	log.Printf("file: %v, rapid: %v", fileRapid, rapid)
	// ...
}

感谢

本项目开发过程中大量参考了以下优秀开源项目代码,感谢大佬们的贡献!

License

FOSSA Status

Documentation

Index

Constants

View Source
const (
	DefaultRootFileId = "root"
)
View Source
const (
	// ThunkSizeDefault 默认 10MB 大小
	ThunkSizeDefault = 1024 * 1024 * 10
)

Variables

View Source
var ErrPartialFoundPath = errors.New("partial found: only found partial parent")

Functions

func ChecksumFileSha1

func ChecksumFileSha1(file *os.File) (string, error)

func Min

func Min(lhs, rhs int64) int64

func PrefixSlash

func PrefixSlash(path string) string

func RemovePrefixSlash

func RemovePrefixSlash(path string) string

func ToMD5

func ToMD5(content string) string

func ToSHA1WithReader

func ToSHA1WithReader(content io.Reader) (string, error)

Types

type AliyunDrive

type AliyunDrive struct {
	Credentials map[string]*Credential
	// contains filtered or unexported fields
}

func NewClient

func NewClient(options *Options) *AliyunDrive

func (*AliyunDrive) AddCredential

func (d *AliyunDrive) AddCredential(credential *Credential) (*Credential, error)

AddCredential 增加新的 Credential,同时刷新 RefreshToken

func (*AliyunDrive) CompleteUpload

func (d *AliyunDrive) CompleteUpload(credential *Credential, fileId, uploadId string) (*models.CompleteFileUploadResponse, error)

CompleteUpload 完成分片上传后,通过此接口结束上传(合并分片)

func (*AliyunDrive) ComputePreHash

func (d *AliyunDrive) ComputePreHash(content io.Reader) (string, error)

ComputePreHash 计算文件 PreHash,只计算前 1KB 的 SHA1

func (*AliyunDrive) ComputeProofCodeV1

func (d *AliyunDrive) ComputeProofCodeV1(credential *Credential, file *os.File, size int64) (string, error)

ComputeProofCodeV1 计算上传的 proof_code, version=v1 proof code 是文件内容的部分片段,主要用于实现秒传 算法:使用 AccessToken MD5 值的前 16 位 HEX 值转换为十进制数并对文件大小取模, 结果作为 proof code 的获取起始位置,取文件内容 8 位 byte 并用 Base64 编码

func (*AliyunDrive) CreateDirectory

func (d *AliyunDrive) CreateDirectory(credential *Credential, parentFileId, name string) (*models.File, error)

CreateDirectory 创建目录

func (*AliyunDrive) CreateWithFolders

func (d *AliyunDrive) CreateWithFolders(credential *Credential, options *CreateWithFoldersOptions) (http.Response, error)

CreateWithFolders 在目录下创建文件,如果非秒传,接下来需要分片上传 响应内容中,如果需要上传,则从 PartInfoList 中获取分片上传地址信息

func (*AliyunDrive) Download

func (d *AliyunDrive) Download(credential *Credential, fileId, requestRange string) (*http2.Response, error)

Download 下载文件

func (*AliyunDrive) EvictCacheWithPrefix

func (d *AliyunDrive) EvictCacheWithPrefix(keyPrefix string) int

EvictCacheWithPrefix 失效 Key 前缀为 keyPrefix 的缓存

func (*AliyunDrive) GetByPath

func (d *AliyunDrive) GetByPath(credential *Credential, fullPath string) (*models.FileResponse, error)

GetByPath 通过 Path 取得文件信息,不存在则错误

func (*AliyunDrive) GetCredentialFromUserId

func (d *AliyunDrive) GetCredentialFromUserId(userId string) *Credential

GetCredentialFromUserId 通过 UserId 取 Credential

func (*AliyunDrive) GetDownloadURL

func (d *AliyunDrive) GetDownloadURL(credential *Credential, fileId string) (*models.DownloadURLResponse, error)

GetDownloadURL 获取下载路经 https://www.aliyundrive.com 获取的 RefreshToken 得到的 URL 需要带 Referrer 下载 移动端 Web 或手机端获取的 RefreshToken 得到的 URL可以直链下载

func (*AliyunDrive) GetFile

func (d *AliyunDrive) GetFile(credential *Credential, fileId string) (*models.FileResponse, error)

GetFile 获取文件信息

func (*AliyunDrive) GetFolderFiles

func (d *AliyunDrive) GetFolderFiles(credential *Credential, options *FolderFilesOptions) (*models.FolderFilesResponse, error)

GetFolderFiles 获取指定目录下的文件列表

func (*AliyunDrive) GetUserInfo

func (d *AliyunDrive) GetUserInfo(credential *Credential) (*models.UserInfo, error)

GetUserInfo get user information

func (*AliyunDrive) GetVideoPreviewPlayInfo

func (d *AliyunDrive) GetVideoPreviewPlayInfo(credential *Credential, fileId string) (*models.VideoPreviewPlayInfoResponse, error)

GetVideoPreviewPlayInfo 获取视频播放信息

func (*AliyunDrive) GetVideoPreviewUrl

func (d *AliyunDrive) GetVideoPreviewUrl(credential *Credential, fileId string) (*models.VideoPreviewUrlResponse, error)

GetVideoPreviewUrl 获取视频预览 URL

func (*AliyunDrive) MoveFile

func (d *AliyunDrive) MoveFile(credential *Credential, fileId, toParentFileId string) (*http.BaseResponse, error)

MoveFile 移动单个文件

func (*AliyunDrive) PartUpload

func (d *AliyunDrive) PartUpload(credential *Credential, uploadUrl string, reader io.Reader, callback ProgressCallback) error

PartUpload 分片数据上传 因服务端使用流式计算 SHA1 值,单个文件的分片需要串行上传,不支持多个分片平行上传

func (*AliyunDrive) RefreshAllToken added in v1.0.1

func (d *AliyunDrive) RefreshAllToken()

func (*AliyunDrive) RefreshToken

func (d *AliyunDrive) RefreshToken(credential *Credential) (*models.RefreshTokenResponse, error)

RefreshToken 刷新 RefreshToken,更新 AccessToken 和 Credential 里的相关信息

func (*AliyunDrive) RemoveFile

func (d *AliyunDrive) RemoveFile(credential *Credential, fileId string) (*http.BaseResponse, error)

RemoveFile 删除文件

func (*AliyunDrive) RenameFile

func (d *AliyunDrive) RenameFile(credential *Credential, fileId, name string) (*models.RenameFileResponse, error)

RenameFile 重命名文件

func (*AliyunDrive) ResolvePathToFileId

func (d *AliyunDrive) ResolvePathToFileId(credential *Credential, fullpath string) (string, string, error)

ResolvePathToFileId 通过路径查找 fileId 当查询出现错误时,返回 "","", err 当查找到路径前一部分时,返回 fileId, prefix, ErrPartialFoundPath 当全部找到时,返回 fileId, fullpath, nil

func (*AliyunDrive) Search

func (d *AliyunDrive) Search(credential *Credential, keyword, marker string) (*models.SearchResponse, error)

Search 查找文件

func (*AliyunDrive) SearchNameInFolder

func (d *AliyunDrive) SearchNameInFolder(credential *Credential, name, parentFileId string) (*models.SearchResponse, error)

SearchNameInFolder 在目录下查找文件名

func (*AliyunDrive) UploadFile

func (d *AliyunDrive) UploadFile(credential *Credential, options *UploadFileOptions) (*models.File, error)

UploadFile 同步上传文件(非秒传)

func (*AliyunDrive) UploadFileRapid

func (d *AliyunDrive) UploadFileRapid(credential *Credential, options *UploadFileRapidOptions) (file *models.File, rapid bool, err error)

UploadFileRapid 上传文件(秒传) 当文件较大(如1GB以上)时,计算整个文件的 sha1 将花费较大的资源。先执行预秒传匹配到可能的数据才执行秒传。

type CreateWithFoldersOptions

type CreateWithFoldersOptions struct {
	Name          string
	ParentFileId  string // 父路径
	Size          int64
	CheckNameMode models.CheckNameMode
	PreHash       string
	ContentHash   string
	ProofCode     string
}

type Credential

type Credential struct {
	UserId         string
	Name           string
	AccessToken    string
	RefreshToken   string
	RootFolder     string
	DefaultDriveId string
	// contains filtered or unexported fields
}

func NewCredential

func NewCredential(credential *Credential) *Credential

func (*Credential) RegisterChangeEvent

func (c *Credential) RegisterChangeEvent(fn func(credential *Credential)) *Credential

type FolderFilesOptions

type FolderFilesOptions struct {
	FolderFileId   string
	OrderBy        string
	OrderDirection string
	Marker         string
}

type Options

type Options struct {
	AutoRefresh     bool
	UploadRate      int
	RefreshDuration string // 刷新周期,默认 @every 1h30m,支持 cron
	Credential      []*Credential
}

type ProgressCallback

type ProgressCallback func(readCount int64) bool

type ProgressInfo

type ProgressInfo struct {
	FileId       string
	UploadId     string
	PartInfoList []*models.PartInfo
}

type RateLimiterReader added in v1.0.1

type RateLimiterReader struct {
	io.Reader
	// contains filtered or unexported fields
}

func (*RateLimiterReader) Read added in v1.0.1

func (p *RateLimiterReader) Read(buf []byte) (n int, err error)

type UploadFileOptions

type UploadFileOptions struct {
	Name             string
	Size             int64
	ParentFileId     string
	ProgressStart    func(info *ProgressInfo)
	ProgressCallback ProgressCallback
	ProgressDone     func(info *ProgressInfo)
	Reader           io.Reader
}

type UploadFileRapidOptions

type UploadFileRapidOptions struct {
	UploadFileOptions

	File        *os.File
	ContentHash string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL