列出AWS S3存储桶中的所有对象

问题描述:

我试图弄清楚如何从Swift中的AWS S3存储桶中列出所有对象.我似乎无法在Internet上的任何地方找到这些信息,但是也许我看起来不够努力.如果有人能推荐我使用允许我执行此操作的代码,那就太好了.

I am trying to figure out how to list all the objects from an AWS S3 bucket in Swift. I can't seem to find the information anywhere on the internet, but maybe I didn't look hard enough. If anyone could refer me to the code that will allow me to do this that would be great.

不知道您是否仍然需要它,但是在这里您可以:

Don't know if you still need it but here you go:

let credentialsProvider = AWSStaticCredentialsProvider(accessKey: "ACCESS KEY", secretKey: "SECRET KEY")
    let configuration = AWSServiceConfiguration(region: .USWest2, credentialsProvider: credentialsProvider)
    AWSS3.registerS3WithConfiguration(configuration, forKey: "defaultKey")
    let s3 = AWSS3.S3ForKey("defaultKey")

    let listRequest: AWSS3ListObjectsRequest = AWSS3ListObjectsRequest()
    listRequest.bucket = "BUCKET"

    s3.listObjects(listRequest).continueWithBlock { (task) -> AnyObject? in
        print("call returned")
        let listObjectsOutput = task.result;
        for object in (listObjectsOutput?.contents)! {

            print(object.key)
        }

        return nil
    }

(感谢Daniel提醒我不要使用不推荐使用的代码);)

(Thanks to Daniel for reminding me not to use deprecated code) ;)