List Objects Stored In AWS S3 Bucket

This Swift code example will demonstrate how to list objects stored in AWS S3 bucket.

The code example below will cover:

  • Configure AWSCognitoCredentialsProvider,
  • Set up AWSS3ListObjectsRequest
  • Perform listObjects() to list objects stored in AWS S3 bucket

For you to be able to use Amazon AWS S3 Service you will need to set up Amazon SDK for iOS and once it is done import needed libraries into your View Controller. The code below shows how to import required libraries only. Watch this video tutorial on how to set up AWS SDK for iOS.

Import Amazon AWS iOS SDK Required Libraries Into Your View Controller

import AWSCore
import AWSCognito
import AWSS3

List Objects From Amazon S3 Bucket. Code Example in Swift.

// Configure AWS Cognito Credentials
        let myIdentityPoolId = "us-east-1:cf7dabf8-1078-4192-b1d7-132fe51dbd02"
        
        let credentialsProvider:AWSCognitoCredentialsProvider = AWSCognitoCredentialsProvider(regionType:AWSRegionType.USEast1, identityPoolId: myIdentityPoolId)
        
        let configuration = AWSServiceConfiguration(region:AWSRegionType.USEast1, credentialsProvider:credentialsProvider)
        
        AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration
       
 
        AWSS3.registerS3WithConfiguration(configuration, forKey: "defaultKey")
        let s3 = AWSS3.S3ForKey("defaultKey")
        
        let listRequest: AWSS3ListObjectsRequest = AWSS3ListObjectsRequest()
        listRequest.bucket = "learn-swift"
        
        s3.listObjects(listRequest).continueWithBlock { (task) -> AnyObject? in
 
            for object in (task.result?.contents)! {
                
                print("Object key = \(object.key!)")
            }
            
            return nil
        }
 

Learn iOS Development with these Video Courses


How to Make a Freaking iPhone App - iOS 10 and Swift 3
icon icon


iOS 10 & Swift 3: From Beginner to Paid Professional
icon


The Complete iOS 10 Developer Course - Build 21 Apps
icon


Swift 3 - Learn to Code with Apple's New Language
icon icon


Learn How to Build Mobile Apps for iOS with Swift, PHP and MySQL
icon icon

Follow me on one of your favourite social networks to learn about new video tutorials and code examples:

Twitter: @SwiftVideoBlog
Google Plus: https://plus.google.com/+SergeyKargopolov/posts
Facebook: Swift Developer Blog on Facebook