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)
}
CodePudding user response:
The rest of the "path" is not part of the bucket. You want to set the Prefix
to screenshots/hotels/
.
See ListObjectV2Input doc