英文:
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 (
"fmt"
"os"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
// Lists all objects in a bucket using pagination
//
// Usage:
// listObjects <bucket>
func main() {
if len(os.Args) < 2 {
fmt.Println("you must specify a bucket")
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("Page,", i)
i++
for _, obj := range p.Contents {
fmt.Println("Object:", *obj.Key)
}
return true
})
if err != nil {
fmt.Println("failed to list objects", err)
return
}
}
https://github.com/aws/aws-sdk-go/blob/main/example/service/s3/listObjects/listObjects.go
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论