本文我们将学习如何使用AWSS3
for iOS来操作Minio Server。AWSS3
for iOS是swift/objective-c语言版本的官方AWS SDK。
从这里下载并安装Minio Server。
如果想要最新版的AWSS3
SDK v2.5.5能正常操作minio/minio:edge,你需要修改AWSS3
SDK中的AWSSignature.m
文件,删除这一行,[urlRequest setValue:@"Chunked" forHTTPHeaderField:@"Transfer-Encoding"];
,请关注aws-sdk-ios #638。
从AWS IOS SDK官方文档中下载将安装AWSS3
for iOS。
我们只需要'AWSS3'。
修改accessKey
, secretKey
和 url
,根据你的需求修改region, service必须设置成.S3
。
(如果你在url
中键入xxxx:9000
,AWSS3
会自动删除端口号,目前它只支持没有端口的URL,所以请确保你有一个到9000的域映射,你可能需要参考如何给Minio Server设置Nginx代理)
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
...
}
例如,如果您运行该示例项目
- 在手机或模拟器上运行xcode项目
- 点击屏幕上的上传按钮
- 通过
Minio Browser
访问, 在images这个存储桶里,应该有一个叫prefix_test.jpg的文件,这就代表你成功了。