github.com/qiniu/go-sdk/v7@v7.27.0/examples/rs_list_bucket_context/main.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  	"strings"
     8  
     9  	"github.com/qiniu/go-sdk/v7/auth"
    10  	"github.com/qiniu/go-sdk/v7/storage"
    11  )
    12  
    13  var (
    14  	accessKey = os.Getenv("QINIU_ACCESS_KEY")
    15  	secretKey = os.Getenv("QINIU_SECRET_KEY")
    16  	bucket    = os.Getenv("QINIU_TEST_BUCKET")
    17  )
    18  
    19  func main() {
    20  	mac := auth.New(accessKey, secretKey)
    21  
    22  	cfg := storage.Config{
    23  		// 是否使用https域名进行资源管理
    24  		UseHTTPS: false,
    25  	}
    26  	// 指定空间所在的区域,如果不指定将自动探测
    27  	// 如果没有特殊需求,默认不需要指定
    28  	// cfg.Zone=&storage.ZoneHuabei
    29  	bucketManager := storage.NewBucketManager(mac, &cfg)
    30  
    31  	// 列举所有文件
    32  	prefix, delimiter, marker := "", "", ""
    33  
    34  	ctx, cancelFunc := context.WithCancel(context.Background())
    35  	entries, err := bucketManager.ListBucketContext(ctx, bucket, prefix, delimiter, marker)
    36  	if err != nil {
    37  		fmt.Fprintf(os.Stderr, "ListBucket: %v\n", err)
    38  		os.Exit(1)
    39  	}
    40  
    41  	ind := 0
    42  	for listItem := range entries {
    43  		if ind > 3 {
    44  			fmt.Println("calling cancelFunc()")
    45  			cancelFunc()
    46  		}
    47  		fmt.Println(listItem.Marker)
    48  		fmt.Println(listItem.Item)
    49  		fmt.Println(listItem.Dir)
    50  		fmt.Println(strings.Repeat("-", 30))
    51  		ind++
    52  	}
    53  }