In this recipe we will learn how to use AWSS3
for iOS with Minio server. AWSS3
is the official AWS S3 SDK for the swift/objective-c programming language.
Install Minio Server from here.
To get latest AWSS3
SDK v2.5.5 working with minio/minio:edge, you have to modify file AWSSignature.m
from AWSS3
SDK, remove line [urlRequest setValue:@"Chunked" forHTTPHeaderField:@"Transfer-Encoding"];
, keep track on aws-sdk-ios #638
Setup AWSS3
for iOS from the official AWS iOS SDK docs here
we only need 'AWSS3'
Please replace accessKey
, secretKey
, and url
, change the region base on what you need, service must set to .S3
(AWSS3
will auto remove port number if you type xxxx:9000
in url
, currently it only support full url without port, so please make sure you have a domain map to port 9000, you may need refer to this Setup Nginx proxy with Minio Server)
let accessKey = "XXXXXXX"
let secretKey = "XXXXXXX"
let credentialsProvider = AWSStaticCredentialsProvider(accessKey: accessKey, secretKey: secretKey)
let configuration = AWSServiceConfiguration(region: .USEast1, endpoint: AWSEndpoint(region: .USEast1, service: .S3, url: URL(string:"XXXXXX")),credentialsProvider: credentialsProvider)
AWSServiceManager.default().defaultServiceConfiguration = configuration
let S3BucketName = "images"
let remoteName = "prefix_test.jpg"
let fileURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(remoteName)
let image = UIImage(named: "test")
let data = UIImageJPEGRepresentation(image!, 0.9)
do {
try data?.write(to: fileURL)
}
catch {}
let uploadRequest = AWSS3TransferManagerUploadRequest()!
uploadRequest.body = fileURL
uploadRequest.key = remoteName
uploadRequest.bucket = S3BucketName
uploadRequest.contentType = "image/jpeg"
uploadRequest.acl = .publicRead
let transferManager = AWSS3TransferManager.default()
transferManager.upload(uploadRequest).continueWith { (task: AWSTask<AnyObject>) -> Any? in
...
}
for example if you running that eample project
- Run the xcode project on your phone or simulator
- Click upload button on screen
- Check on
Minio Browser
, inside images bucket, there should be an image there called prefix_test.jpg, means you susccessful upload the image