Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用进度条时,event.TotalBytes 总是为 0 #351

Open
feiquan666 opened this issue Oct 24, 2022 · 0 comments
Open

使用进度条时,event.TotalBytes 总是为 0 #351

feiquan666 opened this issue Oct 24, 2022 · 0 comments

Comments

@feiquan666
Copy link

使用进度条时,event.TotalBytes 总是为 0
源代码如下

func SaveFiles(folderId uint, files []*multipart.FileHeader) (bool, *global.MyError) {
	key := "***************************"
	secret := "****************************"
	bucketName := "*********************"
	endpoint := "************"
	client, err := oss.New(endpoint, key, secret)
	if err != nil {
		log.Printf("建立连接失败:%v", err)
	}
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Printf("获取 Bucket 失败:%v", err)
	}
	f, err := files[0].Open()
	if err != nil {
		log.Printf("files[0].Open() 失败:%v", err)
	}
	reader := bufio.NewReader(f)
	fmt.Println(reader.Size())
	err = bucket.PutObject("Test/"+files[0].Filename, reader, oss.Progress(&OssProgressListener{}))
	if err != nil {
		log.Printf("PutObject 失败:%v", err)
	}
	return false, nil
}

type OssProgressListener struct {
}

// 定义进度变更事件处理函数。
func (listener *OssProgressListener) ProgressChanged(event *oss.ProgressEvent) {
	totalBytes := event.TotalBytes
	switch event.EventType {
	case oss.TransferStartedEvent:
		fmt.Printf("Transfer Started, ConsumedBytes: %d, TotalBytes %d.\n",
			event.ConsumedBytes, totalBytes)
	case oss.TransferDataEvent:
		rate := 0
		if totalBytes != 0 {
			rate = int(event.ConsumedBytes * 100 / totalBytes)
		}
		fmt.Printf("\rTransfer Data, ConsumedBytes: %d, TotalBytes %d, %d%%.",
			event.ConsumedBytes, totalBytes, rate)
	case oss.TransferCompletedEvent:
		fmt.Printf("\nTransfer Completed, ConsumedBytes: %d, TotalBytes %d.\n",
			event.ConsumedBytes, totalBytes)
	case oss.TransferFailedEvent:
		fmt.Printf("\nTransfer Failed, ConsumedBytes: %d, TotalBytes %d.\n",
			event.ConsumedBytes, totalBytes)
	default:
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant