The following sample code shows how to upload an object body with the transfer-at-once method.
Swift:
// Create a KiiObject in a user-scope bucket.
let bucket = KiiUser . current () !. bucket ( withName : "MyBucket" )
let object = bucket . createObject ()
// Set key-value pairs.
object . setObject ( "MyImage" , forKey : "title" )
object . setObject ( NSNumber ( value : 783204 as Int ), forKey : "fileSize" )
do {
// Save the KiiObject.
try object . saveSynchronous ()
} catch let error as NSError {
// Handle the error.
return
}
// Specify a file to upload.
let targetDirectory : NSString = ( NSHomeDirectory () as NSString ) . appendingPathComponent ( "Documents" ) as NSString
let sourceFilePath = targetDirectory . appendingPathComponent ( "myImage.jpg" )
let sourceFileURL = URL ( fileURLWithPath : sourceFilePath )
// Start uploading.
do {
try object . uploadBodySynchronous ( with : sourceFileURL , andContentType : "image/jpeg" )
} catch let error as NSError {
// Handle the error.
return
}
// Create a KiiObject in a user-scope bucket.
let bucket = KiiUser . current () !. bucket ( withName : "MyBucket" )
let object = bucket . createObject ()
// Set key-value pairs.
object . setObject ( "MyImage" , forKey : "title" )
object . setObject ( NSNumber ( value : 783204 as Int ), forKey : "fileSize" )
// Save the KiiObject.
object . save { ( object : KiiObject ?, error : Error ?) -> Void in
if error != nil {
// Handle the error.
return
}
// Specify a file to upload.
let targetDirectory : NSString = ( NSHomeDirectory () as NSString ) . appendingPathComponent ( "Documents" ) as NSString
let sourceFilePath = targetDirectory . appendingPathComponent ( "myImage.jpg" )
let path = URL ( fileURLWithPath : sourceFilePath )
// Create a progress block.
let progress : KiiObjectBodyProgressBlock = { ( transObj : KiiObject , completedSizeInBytes : Int , totalSizeInBytes : Int , error : Error ?) in
print ( "Progress : \( Float ( completedSizeInBytes / totalSizeInBytes ) ) " )
}
// Start uploading.
object !. uploadBody ( with : path , andContentType : "image/jpeg" , andCompletion : { ( retObject : KiiObject ?, error : Error ?) -> Void in
if error != nil {
// Handle the error.
return
}
}, andProgress : progress )
}
Objective-C:
// Create a KiiObject in a user-scope bucket.
KiiBucket * bucket = [[ KiiUser currentUser ] bucketWithName : @"MyBucket" ];
KiiObject * object = [ bucket createObject ];
// Set key-value pairs.
[ object setObject : @"MyImage"
forKey : @"title" ];
[ object setObject :[ NSNumber numberWithInt : 783204 ]
forKey : @"fileSize" ];
// Save the KiiObject.
NSError * error = nil ;
[ object saveSynchronous : & error ];
if ( error != nil ) {
// Handle the error.
return ;
}
// Specify a file to upload.
NSString * targetDirectory = [ NSHomeDirectory () stringByAppendingPathComponent : @"Documents" ];
NSString * sourceFilePath = [ targetDirectory stringByAppendingPathComponent : @"myImage.jpg" ];
NSURL * path = [ NSURL fileURLWithPath : sourceFilePath ];
// Start uploading.
[ object uploadBodySynchronousWithURL : path
andContentType : @"image/jpeg"
andError :& error ];
if ( error != nil ) {
// Handle the error.
return ;
}
// Create a KiiObject in a user-scope bucket.
KiiBucket * bucket = [[ KiiUser currentUser ] bucketWithName : @"MyBucket" ];
KiiObject * object = [ bucket createObject ];
// Set key-value pairs.
[ object setObject : @"MyImage"
forKey : @"title" ];
[ object setObject :[ NSNumber numberWithInt : 783204 ]
forKey : @"fileSize" ];
// Save the KiiObject.
[ object saveWithBlock : ^ ( KiiObject * object , NSError * error ) {
if ( error != nil ) {
// Handle the error.
return ;
}
// Specify a file to upload.
NSString * targetDirectory = [ NSHomeDirectory () stringByAppendingPathComponent : @"Documents" ];
NSString * sourceFilePath = [ targetDirectory stringByAppendingPathComponent : @"myImage.jpg" ];
NSURL * path = [ NSURL fileURLWithPath : sourceFilePath ];
// Create a progress block.
KiiObjectBodyProgressBlock progress = ^ ( KiiObject * transObj , NSUInteger completedSizeInBytes , NSUInteger totalSizeInBytes , NSError * retError ) {
NSLog ( @"Progress : %f" , ( float ) completedSizeInBytes / totalSizeInBytes );
};
// Start uploading.
[ object uploadBodyWithURL : path
andContentType : @"image/jpeg"
andCompletion: ^ ( KiiObject * obj , NSError * error ) {
if ( error != nil ) {
// Handle the error.
return ;
}
} andProgress : progress ];
}];
Here is what is happening in the sample code:
Set key-value pairs (e.g., file name, file size, and the availability of an object body) in a KiiObject as needed.
Call the save(_:)
method to create a new KiiObject.
Create a reference to the target file (myImage.jpg).
Start uploading by calling the uploadBody(with:andContentType:andCompletion:_:andProgress:)
method.
You need to create a KiiObject with the save(_:)
method prior to uploading its object body with the uploadBody(with:andContentType:andCompletion:)
method.
Set the content type in a form of "type/subtype". The content type sent to Kii Cloud will be used when the object body is downloaded and when the object body is published and viewed on the browser.