https://github.com/maximbilan/swift-amazon-s3-uploading-tutorial
How to upload a file to Amazon S3 using Swift
https://github.com/maximbilan/swift-amazon-s3-uploading-tutorial
amazon amazon-s3-storage amazon-s3-tutorial ios swift tutorial upload upload-file
Last synced: 6 months ago
JSON representation
How to upload a file to Amazon S3 using Swift
- Host: GitHub
- URL: https://github.com/maximbilan/swift-amazon-s3-uploading-tutorial
- Owner: maximbilan
- License: mit
- Created: 2016-12-18T08:01:54.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2018-09-22T10:22:01.000Z (over 7 years ago)
- Last Synced: 2025-04-14T06:55:14.145Z (10 months ago)
- Topics: amazon, amazon-s3-storage, amazon-s3-tutorial, ios, swift, tutorial, upload, upload-file
- Language: Swift
- Homepage:
- Size: 700 KB
- Stars: 77
- Watchers: 2
- Forks: 13
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# How to upload a file to Amazon S3 using Swift
I would like to share a simple tutorial how to upload a to Amazon S3 in iOS using Swift. Let’s go.

We need to add Amazon S3 framework to your project.
In this example I will do this with helping Cocoapods.
Create a Podfile:
platform :ios, '8.0'
inhibit_all_warnings!
use_frameworks!
target 'AmazonS3Upload' do
pod 'AWSS3'
end
Run the next command from Terminal:
pod install
Open the generated workspace. And after that we can implement uploading of files using frameworks from Pods.
We need to import 2 modules:
import AWSS3
import AWSCore
Set up a AWS configuration using your credentials. For example:
let accessKey = "..."
let secretKey = "..."
let credentialsProvider = AWSStaticCredentialsProvider(accessKey: accessKey, secretKey: secretKey)
let configuration = AWSServiceConfiguration(region: AWSRegionType.usEast1, credentialsProvider: credentialsProvider)
AWSServiceManager.default().defaultServiceConfiguration = configuration
Create an upload request:
let url = ...URL to your file...
let remoteName = "Name of uploaded file"
let S3BucketName = "Name of your bucket on Amazon S3"
let uploadRequest = AWSS3TransferManagerUploadRequest()!
uploadRequest.body = url
uploadRequest.key = remoteName
uploadRequest.bucket = S3BucketName
uploadRequest.contentType = "image/jpeg"
uploadRequest.acl = .publicRead
And upload using AWSS3TransferManager.
let transferManager = AWSS3TransferManager.default()
transferManager?.upload(uploadRequest).continue({ (task: AWSTask) -> Any? in
if let error = task.error {
print("Upload failed with error: (\(error.localizedDescription))")
}
if let exception = task.exception {
print("Upload failed with exception (\(exception))")
}
if task.result != nil {
let url = AWSS3.default().configuration.endpoint.url
let publicURL = url?.appendingPathComponent(uploadRequest.bucket!).appendingPathComponent(uploadRequest.key!)
print("Uploaded to:\(publicURL)")
}
return nil
})