wesleydashner
wesleydashner

Reputation: 45

How to download file from an AWS S3 bucket in Swift with unauthenticated users

I want to download a file that's in my AWS S3 bucket from within my iOS app with Swift. How do I do this?

I've done a lot of research and googling and can only find documentation from AWS about how to do this in Objective-C. I've set up AWS Cognito already and gave unauthenticated users permission to get objects from my bucket. I just need to figure out how to actually download from my bucket now. Can anyone guide me in the right direction here? I would greatly appreciate any help.

Upvotes: 3

Views: 2681

Answers (2)

Lawmicha
Lawmicha

Reputation: 157

You can also use AWS Amplify framework. The Storage related documentation is here: https://aws-amplify.github.io/docs/ios/storage which will guide you through provisioning S3 bucket and installing Amplify using CocoaPods. I'd suggest going over that documentation even if you have your own bucket. If you want to use an existing S3 bucket, it will be clearer how to add it to the amplifyconfiguration.json to configure the Amplify Storage plugin.

The Swift code will then look like this:

  Amplify.Storage.downloadData(key: "myKey") { (event) in
      switch event {
      case .completed(let data):
          print("Completed: \(data)")
      case .failed(let storageError):
          print("Failed: \(storageError.errorDescription). \(storageError.recoverySuggestion)")
      case .inProcess(let progress):
          print("Progress: \(progress)")
      default:
          break
      }
  }

Upvotes: 4

Kathan Tripathi
Kathan Tripathi

Reputation: 207

If you are using AWS resources to retrieve the object to down Then they must be having the role to access data within the S3 bucket

If you are retrieving directly from your devices out side AWS, Then you have to make the S3 bucket public

But I suggest you to use EC2 or Lambda as a proxy And using your iOS app will try to retrieve the data, At that time the EC2 or Lambda will first fetch and share the data with you So, you don't need to make your bucket public

Upvotes: 0

Related Questions