// Lists all objects in a bucket using pagination
Lists all objects in a bucket using pagination
// Lists all objects in a bucket using pagination
func main() {
	flag.Parse()
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}
	cfg, err := config.LoadDefaultConfig()
	if err != nil {
		log.Fatalf("failed to load SDK configuration, %v", err)
	}
	client := s3.NewFromConfig(cfg)
	params := &s3.ListObjectsV2Input{
		Bucket: &bucketName,
	}
	if len(objectPrefix) != 0 {
		params.Prefix = &objectPrefix
	}
	if len(objectDelimiter) != 0 {
		params.Delimiter = &objectDelimiter
	}
	p := NewS3ListObjectsV2Paginator(client, params, func(o *S3ListObjectsV2PaginatorOptions) {
		if v := int32(maxKeys); v != 0 {
			o.Limit = v
		}
	})
	var i int
	log.Println("Objects:")
	for p.HasMorePages() {
		i++
		page, err := p.NextPage(context.TODO())
		if err != nil {
			log.Fatalf("failed to get page %v, %v", i, err)
		}
		for _, obj := range page.Contents {
			fmt.Println("Object:", *obj.Key)
		}
	}
}
func main ( ) { flag . Parse ( ) if len ( bucketName ) == 0 { flag . PrintDefaults ( ) log . Fatalf ( " " ) } cfg , err := config . LoadDefaultConfig ( ) if err != nil { log . Fatalf ( " " , err ) } client := s3 . NewFromConfig ( cfg ) params := & s3 . ListObjectsV2Input { Bucket : & bucketName , } if len ( objectPrefix ) != 0 { params . Prefix = & objectPrefix } if len ( objectDelimiter ) != 0 { params . Delimiter = & objectDelimiter } p := NewS3ListObjectsV2Paginator ( client , params , func ( o * S3ListObjectsV2PaginatorOptions ) { if v := int32 ( maxKeys ) ; v != 0 { o . Limit = v } } ) var i int log . Println ( " " ) for p . HasMorePages ( ) { i ++ page , err := p . NextPage ( context . TODO ( ) ) if err != nil { log . Fatalf ( " " , i , err ) } for _ , obj := range page . Contents { fmt . Println ( " " , * obj . Key ) } } }
Lists all objects in a bucket using pagination
Lists all objects in a bucket using pagination
// Load the SDK's configuration from environment and shared config, and // create the client with this. // Set the parameters based on the CLI flag inputs. // TODO replace this with the code generate paginator when available // s3.NewListObjectsV2Paginator() // Iterate through the S3 object pages, printing each object returned. // Next Page takes a new context for each page retrieval. This is where // you could add timeouts or deadlines. // Log the objects found