This project has been deprecated and is no longer actively maintained. Proceed at your own risk!
Bluemix server SDK for Object Storage in Swift
import PackageDescription
let package = Package(
dependencies: [
.Package(url: "https://github.com/ibm-bluemix-mobile-services/bluemix-objectstorage-serversdk-swift.git", majorVersion: 0, minor: 7)
]
)
- 0.7.x releases were tested on OSX and Linux with Swift 3.0.2
Import the BluemixObjectStorage framework
import BluemixObjectStorage
Use ObjectStorage
instance to connect to IBM Object Storage service and manage containers.
let objstorage = ObjectStorage(projectId:"your-project-id")
objstorage.connect( userId: "your-service-userId",
password: "your-service-password",
region: ObjectStorage.REGION_DALLAS) { (error) in
if let error = error {
print("connect error :: \(error)")
} else {
print("connect success")
}
}
objstorage.createContainer(name: "container-name") { (error, container) in
if let error = error {
print("createContainer error :: \(error)")
} else {
print("createContainer success :: \(container?.name)")
}
}
objstorage.retrieveContainer(name: "container-name") { (error, container) in
if let error = error {
print("retrieveContainer error :: \(error)")
} else {
print("retrieveContainer success :: \(container?.name)")
}
}
objstorage.retrieveContainersList { (error, containers) in
if let error = error {
print("retrieveContainersList error :: \(error)")
} else {
print("retrieveContainersList success :: \(containers?.description)")
}
}
objstorage.deleteContainer(name: "container-name") { (error) in
if let error = error {
print("deleteContainer error :: \(error)")
} else {
print("deleteContainer success")
}
}
let metadata:Dictionary<String, String> = ["X-Account-Meta-SomeName":"SomeValue"]
objstorage.updateMetadata(metadata: metadata) { (error) in
if let error = error {
print("updateMetadata error :: \(error)")
} else {
print("updateMetadata success")
}
}
objstorage.retrieveMetadata { (error, metadata) in
if let error = error {
print("retrieveMetadata error :: \(error)")
} else {
print("retrieveMetadata success :: \(metadata)")
}
}
Use ObjectStorageContainer
instance to manage objects inside of particular container
#if os(Linux)
let data = "testdata".dataUsingEncoding(NSUTF8StringEncoding)!
#else
let data = "testdata".data(using: NSUTF8StringEncoding)!
#endif
let data = str.dataUsingEncoding(NSUTF8StringEncoding)
container.storeObject(name: "object-name", data: data) { (error, object) in
if let error = error {
print("storeObject error :: \(error)")
} else {
print("storeObject success :: \(object?.name)")
}
}
container.retrieveObject(name: "object-name") { (error, object) in
if let error = error {
print("retrieveObject error :: \(error)")
} else {
print("retrieveObject success :: \(object?.name)")
}
}
container.retrieveObjectsList { (error, objects) in
if let error = error {
print("retrieveObjectsList error :: \(error)")
} else {
print("retrieveObjectsList success :: \(objects?.description)")
}
}
container.deleteObject(name: "object-name") { (error) in
if let error = error {
print("deleteObject error :: \(error)")
} else {
print("deleteObject success")
}
}
container.delete { (error) in
if let error = error {
print("deleteContainer error :: \(error)")
} else {
print("deleteContainer success")
}
}
let metadata:Dictionary<String, String> = ["X-Container-Meta-SomeName":"SomeValue"]
container.updateMetadata(metadata: metadata) { (error) in
if let error = error {
print("updateMetadata error :: \(error)")
} else {
print("updateMetadata success")
}
}
container.retrieveMetadata { (error, metadata) in
if let error = error {
print("retrieveMetadata error :: \(error)")
} else {
print("retrieveMetadata success :: \(metadata)")
}
}
Use ObjectStorageObject
instance to load object content on demand
object.load(shouldCache: false) { (error, data) in
if let error = error {
print("load error :: \(error)")
} else {
print("load success :: \(data)")
}
}
object.delete { (error) in
if let error = error {
print("deleteObject error :: \(error)")
} else {
print("deleteObject success")
}
}
let metadata:Dictionary<String, String> = ["X-Object-Meta-SomeName":"SomeValue"]
object.updateMetadata(metadata: metadata) { (error) in
if let error = error {
print("updateMetadata error :: \(error)")
} else {
print("updateMetadata success")
}
}
object.retrieveMetadata { (error, metadata) in
if let error = error {
print("retrieveMetadata error :: \(error)")
} else {
print("retrieveMetadata success :: \(metadata)")
}
}
The ObjectStorageError
is an enum with possible failure reasons
enum ObjectStorageError: ErrorType {
case ConnectionFailure
case NotFound
case Unauthorized
case ServerError
case InvalidUri
case FailedToRetrieveAuthToken
case NotConnected
}
Copyright 2016 IBM Corp.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.