Skip to content

Commit

Permalink
fix: support for uploading folders in this current example
Browse files Browse the repository at this point in the history
  • Loading branch information
dayuy committed Dec 20, 2023
1 parent fc1b832 commit 3638831
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions apiserver/examples/upload-download-file/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ import (
"flag"
"fmt"
"io"
"io/fs"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"sync"

"k8s.io/client-go/util/retry"
Expand Down Expand Up @@ -228,11 +231,11 @@ func genURL(
klog.Errorf("[Error] send genMultipartURL request error %s", err)
return GenChunkURLResult{}, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
klog.Infof("[Error] status code is %s, debug resp information %+v\n", resp.StatusCode, *resp)
return GenChunkURLResult{}, fmt.Errorf("response code is %d", resp.StatusCode)
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
klog.Errorf("[Error] failed to read response body error %s", err)
Expand Down Expand Up @@ -333,6 +336,7 @@ func do(

func uploadFile(filePath, bucket, bucketPath string, tp http.RoundTripper) {
f, err := os.Open(filePath)
defer f.Close()
if err != nil {
panic(err)
}
Expand All @@ -347,11 +351,15 @@ func uploadFile(filePath, bucket, bucketPath string, tp http.RoundTripper) {
klog.Errorf("[Error] can't stat file size...")
return
}
fileName := filePath
if filepath.IsAbs(fileName) {
fileName = strings.TrimPrefix(fileName, "/")
}
step := 1

klog.Infof("[Step %d] check the number of chunks the file has completed.", step)
step++
completedChunks, err := successChunks(md5, bucket, bucketPath, filePath, etag, tp)
completedChunks, err := successChunks(md5, bucket, bucketPath, fileName, etag, tp)
if err != nil {
klog.Errorf("[!!!Error] failed to check completed chunks. error %s", err)
return
Expand All @@ -369,7 +377,7 @@ func uploadFile(filePath, bucket, bucketPath string, tp http.RoundTripper) {
klog.Infof("[Step %d] get new uploadid", step)
step++

uploadID, err := newMultipart(md5, bucket, bucketPath, filePath, bufSize, int(chunkCount), tp)
uploadID, err := newMultipart(md5, bucket, bucketPath, fileName, bufSize, int(chunkCount), tp)
if err != nil {
klog.Errorf("[!!!Error] failed to get new uplaodid. error %s", err)
return
Expand Down Expand Up @@ -397,7 +405,7 @@ func uploadFile(filePath, bucket, bucketPath string, tp http.RoundTripper) {

reader := io.NewSectionReader(f, int64(pn-1)*bufSize, bufSize)
go func(partNumber int, reader io.Reader) {
if err := do(&wg, reader, partNumber, bufSize, md5, completedChunks.UploadID, bucket, bucketPath, filePath, tp); err != nil {
if err := do(&wg, reader, partNumber, bufSize, md5, completedChunks.UploadID, bucket, bucketPath, fileName, tp); err != nil {
klog.Errorf("!!![Error] Uploading the %d(st,ne,rd,th) chunk of the file, an error occurs, but the operation will not affect the other chunks at this time, so only the error will be logged here.", partNumber)
lock <- struct{}{}
doComplete = false
Expand All @@ -414,7 +422,7 @@ func uploadFile(filePath, bucket, bucketPath string, tp http.RoundTripper) {
if err := retry.OnError(retry.DefaultRetry, func(err error) bool {
return true
}, func() error {
if err := complete(md5, bucket, bucketPath, completedChunks.UploadID, filePath, tp); err != nil {
if err := complete(md5, bucket, bucketPath, completedChunks.UploadID, fileName, tp); err != nil {
klog.Errorf("[!!!RetryError] retry %d, error %v", retryTimes, err)
retryTimes++
}
Expand Down Expand Up @@ -537,7 +545,25 @@ func main() {
tp := &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}

if *action == upload {
uploadFile(*fileName, *bucket, *bucketPath, tp)
info, err := os.Stat(*fileName)
if err != nil {
klog.Errorf("[Error] can't stat file %s: %s", *fileName, err)
return
}
if info.IsDir() {
filepath.WalkDir(*fileName, func(path string, d fs.DirEntry, err error) error {
if err != nil {
klog.Errorf("[Error] failed access a path %s: %s", path, err)
return err
}
if !d.IsDir() {
uploadFile(path, *bucket, *bucketPath, tp)
}
return nil
})
} else {
uploadFile(*fileName, *bucket, *bucketPath, tp)
}
} else {
downloadFile(*bucket, *bucketPath, *fileName, tp)
}
Expand Down

0 comments on commit 3638831

Please sign in to comment.