通过 LastModified 在 AWS S3 中搜索文件 [英] Search Files in AWS S3 by LastModified

查看:17
本文介绍了通过 LastModified 在 AWS S3 中搜索文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据文件创建时间(或上次修改时间)在 AWS S3 中搜索文件.我知道在 python 中使用 boto3 分页器提供了提供查询字符串的选项,但希望在 go 中实现相同的功能.

I want to search files in AWS S3 based on file creation time (or LastModified) time in go. I am aware of same in python using boto3 paginator which provides the options to provide the query string but want to achieve same in go.

go-lang 中的任何建议或任何示例将不胜感激?

Any suggestion or any sample in go-lang would be appreciated?

我试图列出所有文件的示例代码:

Sample code I am trying to list all files:

for s.NextContinuationToken != ""  { 

        maxFileRead := 15

        bucket := "XXX-XXX-test"

        // To check if previous result  was truncated
        if s.IsTruncated {
            fileList, err = s.session.ListObjectsV2(&s3.ListObjectsV2Input{
                Bucket:            aws.String(bucket),
                MaxKeys:           aws.Int64(maxFileRead),
                ContinuationToken: &s.NextContinuationToken,
            })
        } else {
            fileList, err = s.session.ListObjectsV2(&s3.ListObjectsV2Input{
                Bucket:      aws.String(bucket),
                MaxKeys:     aws.Int64(maxFileRead),
            })
        }

        s.IsTruncated = *fileList.IsTruncated

        if s.IsTruncated {
            s.NextContinuationToken = *fileList.NextContinuationToken
        } else {
            s.NextContinuationToken = ""
        }

        if err != nil {
            if aerr, ok := err.(awserr.Error); ok {
                switch aerr.Code() {
                case s3.ErrCodeNoSuchBucket:
                    fmt.Println(s3.ErrCodeNoSuchBucket, aerr.Error())
                default:
                    fmt.Println(aerr.Error())
                }
            } else {
                // Print the error, cast err to awserr.Error to get the Code and
                // Message from an error.
                fmt.Println(err.Error())
            }
    }
}

现在我想将搜索修改为仅列出特定时间之后创建的文件.

Now I want to modify the search to only list files created after a particular time.

推荐答案

  1. 调用 ListObjectsV2 (https://docs.aws.amazon.com/sdk-for-go/api/service/s3/#S3.ListObjectsV2) 在每个存储桶上.

返回的 Contents 属性是关于每个存储桶对象的元数据列表.

The Contents property returned is a list of metadata about each bucket object.

使用 LastModified 字段.

Use the LastModified field.

这篇关于通过 LastModified 在 AWS S3 中搜索文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆