storj.io/uplink@v1.13.0/buckets.go (about)

     1  // Copyright (C) 2020 Storj Labs, Inc.
     2  // See LICENSE for copying information.
     3  
     4  package uplink
     5  
     6  import (
     7  	"context"
     8  
     9  	"storj.io/uplink/private/metaclient"
    10  )
    11  
    12  // ListBucketsOptions defines bucket listing options.
    13  type ListBucketsOptions struct {
    14  	// Cursor sets the starting position of the iterator. The first item listed will be the one after the cursor.
    15  	Cursor string
    16  }
    17  
    18  // ListBuckets returns an iterator over the buckets.
    19  func (project *Project) ListBuckets(ctx context.Context, options *ListBucketsOptions) *BucketIterator {
    20  	defer mon.Task()(&ctx)(nil)
    21  
    22  	if options == nil {
    23  		options = &ListBucketsOptions{}
    24  	}
    25  
    26  	buckets := BucketIterator{
    27  		iterator: metaclient.IterateBuckets(ctx, metaclient.IterateBucketsOptions{
    28  			Cursor: options.Cursor,
    29  			DialClientFunc: func() (*metaclient.Client, error) {
    30  				return project.dialMetainfoClient(ctx)
    31  			},
    32  		}),
    33  	}
    34  
    35  	return &buckets
    36  }
    37  
    38  // BucketIterator is an iterator over a collection of buckets.
    39  type BucketIterator struct {
    40  	iterator *metaclient.BucketIterator
    41  }
    42  
    43  // Next prepares next Bucket for reading.
    44  // It returns false if the end of the iteration is reached and there are no more buckets, or if there is an error.
    45  func (buckets *BucketIterator) Next() bool {
    46  	return buckets.iterator.Next()
    47  }
    48  
    49  // Err returns error, if one happened during iteration.
    50  func (buckets *BucketIterator) Err() error {
    51  	return convertKnownErrors(buckets.iterator.Err(), "", "")
    52  }
    53  
    54  // Item returns the current bucket in the iterator.
    55  func (buckets *BucketIterator) Item() *Bucket {
    56  	item := buckets.iterator.Item()
    57  	if item == nil {
    58  		return nil
    59  	}
    60  	return &Bucket{
    61  		Name:    item.Name,
    62  		Created: item.Created,
    63  	}
    64  }