アップロード
一括アップロードを行う例を以下に挙げます。
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]; }];
ここでは以下の処理を実施しています。
- (必要に応じて)KiiObject にキーと値のペアをセット。ファイル名、ファイルサイズ、Object Body の有無などを登録しておくことも可能。
save(_:)
メソッドを実行して、オブジェクトを保存。- アップロード対象ファイル(myImage.jpg)のリファレンスを作成。
uploadBody(with:andContentType:andCompletion:_:andProgress:)
メソッドを実行して、ファイルのアップロードを開始。
uploadBody(with:andContentType:andCompletion:_:andProgress:)
メソッドを使用する場合は、事前に save(_:)
メソッドによって Kii Cloud 上に KiiObject を作成しておく必要があります。
Content-Type は "type/subtype" の形式で指定します。Kii Cloud に送信された Content-Type は、ダウンロード時や、公開された Object Body をブラウザで参照するときに使用されます。