A progress bar shows the progress of an upload or download. This topic uses the Bucket.GetObjectToFile method as an example to demonstrate how to print a progress bar when you download a file (object).
Usage notes
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.
In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Configure OSSClient instances.
Example code
The following code shows how to print a progress bar when you download the exampleobject.txt file from the examplebucket bucket.
package main
import (
"fmt"
"os"
"sync/atomic"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
// Define a progress bar listener.
type OssProgressListener struct {
lastProgress int64
}
// Define a handler for progress change events.
func (listener *OssProgressListener) ProgressChanged(event *oss.ProgressEvent) {
switch event.EventType {
case oss.TransferStartedEvent:
fmt.Printf("Transfer Started, ConsumedBytes: %d, TotalBytes %d.\n",
event.ConsumedBytes, event.TotalBytes)
case oss.TransferDataEvent:
if event.TotalBytes != 0 {
progress := int64(event.ConsumedBytes * 100 / event.TotalBytes)
if progress != atomic.LoadInt64(&listener.lastProgress) {
atomic.StoreInt64(&listener.lastProgress, progress)
fmt.Printf("\rTransfer Data, ConsumedBytes: %d, TotalBytes %d, %d%%.\n",
event.ConsumedBytes, event.TotalBytes, progress)
}
}
case oss.TransferCompletedEvent:
fmt.Printf("\nTransfer Completed, ConsumedBytes: %d, TotalBytes %d.\n",
event.ConsumedBytes, event.TotalBytes)
case oss.TransferFailedEvent:
fmt.Printf("\nTransfer Failed, ConsumedBytes: %d, TotalBytes %d.\n",
event.ConsumedBytes, event.TotalBytes)
default:
}
}
func main() {
// Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Replace yourEndpoint with the endpoint of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
// Replace yourRegion with the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Set the signature version.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
client, err := oss.New("yourEndpoint", "", "", clientOptions...)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Specify the bucket name.
bucketName := "examplebucket"
// Specify the full path of the object. The full path cannot include the bucket name.
objectName := "exampleobject.txt"
// Specify the full path of the local file. If the specified local file exists, it is overwritten. If it does not exist, it is created.
// If you do not specify a local path, the downloaded file is saved by default to the local path of the project where the sample program resides.
localFile := "D:\\localpath\\examplefile.txt"
// Obtain the bucket.
bucket, err := client.Bucket(bucketName)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Download the file with a progress bar.
err = bucket.GetObjectToFile(objectName, localFile, oss.Progress(&OssProgressListener{}))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
fmt.Println("Transfer Completed.")
}