Skip to content

Latest commit

 

History

History
66 lines (44 loc) · 2.75 KB

aws-sdk-for-iOS-with-minio.md

File metadata and controls

66 lines (44 loc) · 2.75 KB

如何使用AWS SDK for iOS(swift-3)操作Minio Server Slack

本文我们将学习如何使用AWSS3 for iOS来操作Minio Server。AWSS3 for iOS是swift/objective-c语言版本的官方AWS SDK。

1. 前提条件

这里下载并安装Minio Server。

如果想要最新版的AWSS3 SDK v2.5.5能正常操作minio/minio:edge,你需要修改AWSS3 SDK中的AWSSignature.m文件,删除这一行,[urlRequest setValue:@"Chunked" forHTTPHeaderField:@"Transfer-Encoding"];,请关注aws-sdk-ios #638

2. 安装

AWS IOS SDK官方文档中下载将安装AWSS3 for iOS。

我们只需要'AWSS3'。

3. 示例

修改accessKey, secretKeyurl,根据你的需求修改region, service必须设置成.S3

(如果你在url中键入xxxx:9000AWSS3会自动删除端口号,目前它只支持没有端口的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
  ...
}

完整示例程序

4. 运行程序

例如,如果您运行该示例项目

  1. 在手机或模拟器上运行xcode项目
  2. 点击屏幕上的上传按钮

screenshot

  1. 通过Minio Browser访问, 在images这个存储桶里,应该有一个叫prefix_test.jpg的文件,这就代表你成功了。