golang的S3客户端库是否有获取迭代器函数来检索S3存储桶中的所有对象?

huangapple go评论92阅读模式
英文:

Does golang S3 client library has the get Iterator function to retrieve all the objects in S3 bucket

问题

我正在尝试使用Golang列出S3 Bucket中的所有对象,但是我发现Golang的S3 List函数只会返回1000个对象。

所以,在PHP中,我可以使用以下代码:

$objects=S3->getIterator('ListObjects', array('Bucket' => $bucket,'Prefix'=>'test/'));

如果我在$objects中迭代100X个项目,它可以返回接下来的100X个对象。那么在Golang中是否有类似的机制(或迭代器函数),可以迭代下一个1000个对象呢?

谢谢。

英文:

I am trying to use Golang to list all the objects in S3 Bucket, however, I found the Golang

S3 List function would return only 1000 objects.

So, in PHP, I can use

$objects=S3->getIterator('ListObjects', array('Bucket' => $bucket,'Prefix'=>'test/'));

which can return the next 100x objects if I iterate the 100X items in $objects. So is there any

mechanism (or iterator function) in Golang that I can also iterate the next 1000 objects from

the iterator?

Thanks

答案1

得分: 2

编辑:是的,他们有。

var (
sqsRegion string
)

func init() {
if err := godotenv.Load(); err != nil {
log.Fatalf("加载.env文件时出错:%s", err.Error())
}

sqsRegion = os.Getenv("AWS_REGION")

}

func main() {

sess := session.New(&aws.Config{Region: aws.String(sqsRegion)})

svc := s3.New(sess)

resp, err := svc.ListObjects(&s3.ListObjectsInput{
    Bucket: aws.String("yourbucket"),
})

if err != nil {
    log.Fatalln(err.Error())
}

for _, key := range resp.Contents {
    log.Println(*key.Key)
}

}

英文:

Edit: Yes, they do.

var (
	sqsRegion string
)

func init() {
	if err := godotenv.Load(); err != nil {
		log.Fatalf("Error loading .env file: %s", err.Error())
	}

	sqsRegion = os.Getenv("AWS_REGION")
}

func main() {

	sess := session.New(&aws.Config{Region: aws.String(sqsRegion)})

	svc := s3.New(sess)

	resp, err := svc.ListObjects(&s3.ListObjectsInput{
		Bucket: aws.String("yourbucket"),
	})

	if err != nil {
		log.Fatalln(err.Error())
	}

	for _, key := range resp.Contents {
		log.Println(*key.Key)
	}

}

答案2

得分: 0

你可以使用分页来列出所有存储桶的文件。

package main

import (
	"fmt"
	"os"

	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/s3"
)

// 使用分页列出存储桶中的所有对象
//
// 用法:
// listObjects <bucket>
func main() {
	if len(os.Args) < 2 {
		fmt.Println("你必须指定一个存储桶")
		return
	}

	sess := session.Must(session.NewSession())

	svc := s3.New(sess)

	i := 0
	err := svc.ListObjectsPages(&s3.ListObjectsInput{
		Bucket: &os.Args[1],
	}, func(p *s3.ListObjectsOutput, last bool) (shouldContinue bool) {
		fmt.Println("页,", i)
		i++

		for _, obj := range p.Contents {
			fmt.Println("对象:", *obj.Key)
		}
		return true
	})
	if err != nil {
		fmt.Println("列出对象失败", err)
		return
	}
}

https://github.com/aws/aws-sdk-go/blob/main/example/service/s3/listObjects/listObjects.go

英文:

You can use pagination to list all bucket's files.

package main

import (
	&quot;fmt&quot;
	&quot;os&quot;

	&quot;github.com/aws/aws-sdk-go/aws/session&quot;
	&quot;github.com/aws/aws-sdk-go/service/s3&quot;
)

// Lists all objects in a bucket using pagination
//
// Usage:
// listObjects &lt;bucket&gt;
func main() {
	if len(os.Args) &lt; 2 {
		fmt.Println(&quot;you must specify a bucket&quot;)
		return
	}

	sess := session.Must(session.NewSession())

	svc := s3.New(sess)

	i := 0
	err := svc.ListObjectsPages(&amp;s3.ListObjectsInput{
		Bucket: &amp;os.Args[1],
	}, func(p *s3.ListObjectsOutput, last bool) (shouldContinue bool) {
		fmt.Println(&quot;Page,&quot;, i)
		i++

		for _, obj := range p.Contents {
			fmt.Println(&quot;Object:&quot;, *obj.Key)
		}
		return true
	})
	if err != nil {
		fmt.Println(&quot;failed to list objects&quot;, err)
		return
	}
}

https://github.com/aws/aws-sdk-go/blob/main/example/service/s3/listObjects/listObjects.go

huangapple
  • 本文由 发表于 2014年11月12日 23:22:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/26890421.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定