如何列出 AWS S3 存储桶目录中的项目。

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

How to list items in an aws S3 bucket directory

问题

我有一个名为"Project"的Amazon S3存储桶,并且想要找到"Project/screenshots/hotels"路径下文件的数量。当我只指定存储桶名称时,我只能获取整个S3中的文件总数,而当我在存储桶名称中指定完整路径时,出现错误。

以下是用Go SDK获取S3存储桶中文件列表的示例代码:

package main

import (
	"fmt"
	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/s3"
	"os"
)

// 列出指定S3存储桶中的项目
//
// 用法:
//    go run s3_list_objects.go 存储桶名称
func main() {
	bucket := "Project/screenshots/hotels"

	// 初始化一个位于us-west-2区域的会话,SDK将使用该会话从共享凭证文件~/.aws/credentials中加载凭证。
	sess, err := session.NewSession(&aws.Config{
		Region: aws.String("us-east-1")},
	)
	var temp int64 = 1000000000
	var maxKeys *int64
	maxKeys = &temp
	// 创建S3服务客户端
	svc := s3.New(sess)
	// 获取项目列表
	resp, err := svc.ListObjectsV2(&s3.ListObjectsV2Input{Bucket: aws.String(bucket), MaxKeys: maxKeys})
	if err != nil {
		exitErrorf("无法列出存储桶 %q 中的项目,%v", bucket, err)
	}

	for _, item := range resp.Contents {
		fmt.Println("名称:         ", *item.Key)
		fmt.Println("最后修改时间: ", *item.LastModified)
		fmt.Println("大小:         ", *item.Size)
		fmt.Println("存储类别:     ", *item.StorageClass)
		fmt.Println("")
	}
	fmt.Println("在存储桶", bucket, "中找到", len(resp.Contents), "个项目")
	fmt.Println("")
}

func exitErrorf(msg string, args ...interface{}) {
	fmt.Fprintf(os.Stderr, msg+"\n", args...)
	os.Exit(1)
}

你可以使用上述代码来获取指定路径下文件的数量。

英文:

I have an Amazon S3 bucket by name "Project" and want to find count of files in Project/screenshots/hotels. I am only getting total files in the whole s3 using Go SDK while specifying only bucket name and an error when specifying whole path in bucket name.

https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/go/example_code/s3/s3_list_objects.go

package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"os"
)
// Lists the items in the specified S3 Bucket
//
// Usage:
//    go run s3_list_objects.go BUCKET_NAME
func main() {
bucket := "Project/screenshots/hotels"
// Initialize a session in us-west-2 that the SDK will use to load
// credentials from the shared credentials file ~/.aws/credentials.
sess, err := session.NewSession(&aws.Config{
Region: aws.String("us-east-1")},
)
var temp int64 = 1000000000
var maxKeys *int64  
maxKeys = &temp
// Create S3 service client
svc := s3.New(sess)
// Get the list of items
resp, err := svc.ListObjectsV2(&s3.ListObjectsV2Input{Bucket: aws.String(bucket),MaxKeys: maxKeys})
if err != nil {
exitErrorf("Unable to list items in bucket %q, %v", bucket, err)
}
for _, item := range resp.Contents {
fmt.Println("Name:         ", *item.Key)
fmt.Println("Last modified:", *item.LastModified)
fmt.Println("Size:         ", *item.Size)
fmt.Println("Storage class:", *item.StorageClass)
fmt.Println("")
}
fmt.Println("Found", len(resp.Contents), "items in bucket", bucket)
fmt.Println("")
}
func exitErrorf(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, msg+"\n", args...)
os.Exit(1)
}

答案1

得分: 3

剩下的“路径”不是存储桶的一部分。您想将Prefix设置为screenshots/hotels/。请参阅ListObjectV2Input文档

英文:

The rest of the "path" is not part of the bucket. You want to set the Prefix to screenshots/hotels/.
See ListObjectV2Input doc

huangapple
  • 本文由 发表于 2021年12月23日 02:59:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/70453927.html
匿名

发表评论

匿名网友

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

确定