Skip to content

Commit

Permalink
feat: add drive upload file/media api
Browse files Browse the repository at this point in the history
Change-Id: Ib78028e02d382cc847705c7dd6b8c07043f788d4
  • Loading branch information
mangotree committed Dec 15, 2020
1 parent 86b5162 commit 06c74ce
Show file tree
Hide file tree
Showing 5 changed files with 529 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ $ go get -u github.com/larksuite/oapi-sdk-go
| Image | v4 | [service/image](service/image)|[sample/api/image.go](sample/api/image.go)|
| Contact | v3 | [service/contact](service/contact) | [sample/api/contact.go](sample/api/contact.go) |
| Calendar | v4 | [service/calendar](service/calendar)|[sample/api/calendar.go](sample/api/calendar.go)|

| Drive | v1 | [service/drive](service/drive)|[sample/api/drive.go](sample/api/drive.go)|


- Instructions for use(For`No business API SDK is generated`the processing method)
- For`App Store application`, when acquiring`app_access_token`, you need `app_ticket` to start the event subscription service(`Module event`
Expand Down
3 changes: 2 additions & 1 deletion README.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ $ go get -u github.com/larksuite/oapi-sdk-go
| 图片 | v4 | [service/image](service/image)|[sample/api/image.go](sample/api/image.go)|
| 通讯录 | v3 | [service/contact](service/contact)|[sample/api/contact.go](sample/api/contact.go)|
| 日历 | v4 | [service/calendar](service/calendar)|[sample/api/calendar.go](sample/api/calendar.go)|

| 云空间文件 | v1 | [service/drive](service/drive)|[sample/api/drive.go](sample/api/drive.go)|


- 使用说明(对于`没有生成业务API SDK`的处理方式)
- 对于`应用商店应用`,在获取`app_access_token`时,需要 `app_ticket`,需要启动事件订阅服务(`模块event`
Expand Down
136 changes: 136 additions & 0 deletions sample/api/drive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package main

import (
"context"
"crypto/rand"
"fmt"
"github.com/larksuite/oapi-sdk-go/api/core/request"
"github.com/larksuite/oapi-sdk-go/api/core/response"
"github.com/larksuite/oapi-sdk-go/core"
"github.com/larksuite/oapi-sdk-go/core/test"
"github.com/larksuite/oapi-sdk-go/core/tools"
drivev1 "github.com/larksuite/oapi-sdk-go/service/drive/v1"
"hash/adler32"
"io"
)

var driveService = drivev1.NewService(test.GetInternalConf("online"))

func main() {
testFileUploadAll()
testFileUploadPart()
}
func createRandomFileData(size int64) []byte {
randomData := make([]byte, size)
io.ReadFull(rand.Reader, randomData)
return randomData
}

func testFileUploadAll() {
coreCtx := core.WarpContext(context.Background())
reqCall := driveService.Files.UploadAll(coreCtx, request.SetUserAccessToken("[user_access_token]"))

reqCall.SetParentType("explorer")
reqCall.SetParentNode("[folder_token]")
reqCall.SetFileName(fmt.Sprintf("[file_name]"))
reqCall.SetSize(1024)

fileContent := createRandomFileData(1024)
reqCall.SetChecksum(fmt.Sprintf("%d", adler32.Checksum(fileContent)))
file := request.NewFile()
file.SetContent(fileContent)
reqCall.SetFile(file)

result, err := reqCall.Do()
fmt.Printf("request_id:%s", coreCtx.GetRequestID())
fmt.Printf("http status code:%d", coreCtx.GetHTTPStatusCode())
if err != nil {
e := err.(*response.Error)
fmt.Println(tools.Prettify(e))
return
}
fmt.Printf("reault:%s", tools.Prettify(result))

if len(result.FileToken) == 0 {
fmt.Printf("file token is empty")
return
}
}

func testFileUploadPart() {
coreCtx := core.WarpContext(context.Background())
userAccessTokenOptFn := request.SetUserAccessToken("[user_access_token]")
fileSize := 1024

// upload prepare
uploadPrepareReqCall := driveService.Files.UploadPrepare(coreCtx, &drivev1.UploadInfo{
FileName: fmt.Sprintf("[file_name]"),
ParentType: "explorer",
ParentNode: "[folder_token]",
Size: fileSize,
}, userAccessTokenOptFn)

uploadPrepareResult, err := uploadPrepareReqCall.Do()
fmt.Printf("[upload prepare] request_id:%s", coreCtx.GetRequestID())
fmt.Printf("[upload prepare] http status code:%d", coreCtx.GetHTTPStatusCode())
if err != nil {
e := err.(*response.Error)
fmt.Println(tools.Prettify(e))
return
}

fmt.Printf("[upload prepare] reault:%s", tools.Prettify(uploadPrepareResult))

// upload part
uploadedBlockNum := 0
for i := 0; i < uploadPrepareResult.BlockNum; i++ {
uploadPartReqCall := driveService.Files.UploadPart(coreCtx, userAccessTokenOptFn)
uploadPartReqCall.SetUploadId(uploadPrepareResult.UploadId)
uploadPartReqCall.SetSeq(i)
//uploadPartReqCall.Set
// 最后一块
blockSize := uploadPrepareResult.BlockSize
if i == (uploadPrepareResult.BlockNum - 1) {
blockSize = fileSize - (i * uploadPrepareResult.BlockNum)
}
uploadPartReqCall.SetSize(blockSize)
fileContent := createRandomFileData(int64(blockSize))
file := request.NewFile().SetContent(fileContent)

uploadPartReqCall.SetFile(file)
uploadPartReqCall.SetChecksum(fmt.Sprintf("%d", adler32.Checksum(fileContent)))

result, err := uploadPartReqCall.Do()
fmt.Printf("[upload part[%d]] request_id:%s", i, coreCtx.GetRequestID())
fmt.Printf("[upload part[%d]] http status code:%d", i, coreCtx.GetHTTPStatusCode())
if err != nil {
e := err.(*response.Error)
fmt.Println(tools.Prettify(e))
return
}
uploadedBlockNum++
fmt.Printf("[upload part[%d]] reault:%s", i, tools.Prettify(result))
}

// upload finish
uploadFinishReqCall := driveService.Files.UploadFinish(coreCtx, &drivev1.FileUploadFinishReqBody{
UploadId: uploadPrepareResult.UploadId,
BlockNum: uploadedBlockNum,
}, userAccessTokenOptFn)

uploadFinishResult, err := uploadFinishReqCall.Do()
fmt.Printf("[upload finish] request_id:%s", coreCtx.GetRequestID())
fmt.Printf("[upload finish] http status code:%d", coreCtx.GetHTTPStatusCode())
if err != nil {
e := err.(*response.Error)
fmt.Println(tools.Prettify(e))
return
}

fmt.Printf("[upload finish] reault:%s", tools.Prettify(uploadFinishResult))

if len(uploadFinishResult.FileToken) == 0 {
fmt.Printf("file token is empty")
return
}
}
Loading

0 comments on commit 06c74ce

Please sign in to comment.