douhuanglou1445 2018-01-31 05:17
浏览 382
已采纳

ListObjects始终返回1000个对象的列表。 我们可以列出按日期排序的对象吗?

I am using aws-doc-sdk-examples/go/example_code/s3 example code to list out objects in my s3 bucket.

Following two folders are created by default in bucket:

bucket-logs/AWSLogs/accountxxx/CloudTrail-Digest/
bucket-logs/AWSLogs/accountxxx/CloudTrail/

Both folders have sub folders like

/us-region-1/[year]/[month]/[day]/

I am using below GoLang code:

package main

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

func main() {
    if len(os.Args) != 2 {
        exitErrorf("Bucket name required
Usage: %s bucket_name",
            os.Args[0])
    }

    bucket := os.Args[1]

    sess, err := session.NewSession(&aws.Config{
        Region: aws.String("us-east-1")},
    )

    svc := s3.New(sess)

    resp, err := svc.ListObjects(&s3.ListObjectsInput{Bucket: aws.String(bucket), })
    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+"
", args...)
    os.Exit(1)
}

The above code always returning 1000 object from bucket-logs/AWSLogs/accountxxx/CloudTrail-Digest/ Whereas I want to list objects from bucket-logs/AWSLogs/accountxxx/CloudTrail/ folder.

Can we do this. Can we filter list object sorted by year/month/date?

Any help ?

EDIT:

I am running the Go programm using the below cli command

./s3_list_objects bucket-logs
  • 写回答

1条回答 默认 最新

  • duanbiao4025 2018-01-31 06:00
    关注

    You will need to add prefix in ListObjectsInput. Check out this https://stackoverflow.com/a/43737253/1225070

    params := &s3.ListObjectsInput { 
        Bucket: aws.String("bucket"), 
        Prefix: aws.String("root/subfolder"), 
    } 
    

    Hope this will help you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?