帮助与文档

我们为你提供丰富、详尽的使用指南以及产品文档

Swift SDK

QingStor Swift SDK 已在 GitHub 开源,下文为简要使用文档。更多详细信息请参见 https://github.com/yunify/qingstor-sdk-swift

安装

安装 CocoaPod:

> gem install cocoapods

编辑依赖描述文件:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!

target '' do
  pod 'QingStorSDK'
end

安装依赖:

> pod install

快速开始

使用 SDK 之前请先在 青云控制台 申请 access key 。

初始化服务

发起请求前首先建立需要初始化服务:

Registry.register(accessKeyID: "ACCESS_KEY_ID", secretAccessKey: "SECRET_ACCESS_KEY")
let qsService = QingStor()

上面代码初始化了一个 QingStor Service

获取账户下的 Bucket 列表

qsService.listBuckets(input: ListBucketsInput()) { response, error in
    // Print HTTP status code.
    print("\(response?.statusCode)")

    // Print the count of buckets.
    print("\(response?.output.count)")

    // Print the first bucket name.
    print("\(response?.output.buckets?[0].name)")
}

创建 Bucket

初始化并创建 Bucket, 需要指定 Bucket 名称和所在 Zone:

let bucket = qsService.bucket(bucketName: "test-bucket")
bucket.put { response, error in
    if let response = response {
        print("StatusCode: \(response.statusCode)")
        if response.output.errMessage == nil {
            print("Bucket \"test-bucket\" created")
        }
    }
}

获取 Bucket 中存储的 Object 列表

let input = ListBucketsInput()
qsService.listBuckets(input: input) { response, error in
    if let response = response {
        print("StatusCode: \(response.statusCode)")
        if response.output.errMessage == nil {
            print("Bucket count: \(response.output.count)")
            print("First bucket name: \(response.output.buckets?[0].name)")
        }
    }
}

创建一个 Object

let path = Bundle.main.path(forResource: "image", ofType: "jpeg")!
let objectFileURL = URL(fileURLWithPath: path)
let input = PutObjectInput(
    contentLength: objectFileURL.contentLength,
    contentType: objectFileURL.mimeType,
    bodyInputStream: InputStream(url: objectFileURL)
)
bucket.putObject(objectKey:"image.jpeg", input: input) { response, error in
    if let response = response {
        print("StatusCode: \(response.statusCode)")
    }
}

删除一个 Object

bucket.deleteObject(objectKey:"image.jpeg") { response, error in
    if let response = response {
        print("StatusCode: \(response.statusCode)")
    }
}