当前位置: 动力学知识库 > 问答 > 编程问答 >

ios - Upload video in SOFTLAYER(IBM Cloud)

问题描述:

I have to upload video in SOFTLAYER(IBM Cloud) server using Objective C. I'm using xcode7.2 with Objective C. I don't have any idea how to upload video in cloud base server using Objective C. When go to SOFTLAYER API link there is no guideline for iOS or how to implement SOFTLYAER using Objective C. My question is how to integrate SOFTLAYER in Xcode and how to implement this.

I'm adding an screen shot of SOFTLAYER guide there is no guide for iOS.

If any one upload video in SOFTLAYER server using Objective C or if You have any idea about this please help.

Thanks in advance.

网友答案:

yes as you wrote all the Softlayer clients are here:

https://github.com/softlayer

However there is not a specific client to manage object storage using objective-c, But you can manage Softlayer's object storage using simple rest requests and be able to upload your video successfully, I am pretty sure that Objective-C provides an API to deal with REST request.

you can see here a guide about manage Object Storage using rest here:

https://sldn.softlayer.com/blog/waelriac/managing-softlayer-object-storage-through-rest-apis

Let me know if you have more questions

Regards

网友答案:

Thanks @Nelson for this link Softlayer Object Storage access through rest api. From this link we can upload video in object storage directly from app. For more details you see this link Access Softlayer using Postman.

I'm uploading video in softlayer object storage using rest api calling. I'm calling rest api using AFNetworking in objective C. Here are the following simple steps:-

My authToken is:- "AUTH_tk9eafc199756fabe8066f"

1) Get "auth token" and "x-storage-url" from softlayer object storage server. You can get this by using "Authentication Endpoint (public and private)" this api. Sending your "username" and "API Key (Password)" in header field.

-(void)callRestAPI{

NSURL *URL = [NSURL URLWithString:@"https://sjc01.objectstorage.softlayer.net/auth/v1.0/"];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.requestSerializer =  [AFHTTPRequestSerializer serializer];
[manager.requestSerializer setValue:@"HJYBOS984353-3:ghym8435" forHTTPHeaderField:@"X-Auth-User"];
[manager.requestSerializer setValue:@"26704dbc93e5c39986da451e539857f6596dc9be18aad7a6b4dc2822f48e66f8" forHTTPHeaderField:@"X-Auth-Key"];
[manager GET:URL.absoluteString parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {

    NSHTTPURLResponse *response = ((NSHTTPURLResponse *)[task response]);
    NSDictionary *headers = [response allHeaderFields];
    authURL = [headers valueForKey:@"X-Storage-Url"];
    authToken = [headers valueForKey:@"X-Auth-Token"];
    NSLog(@"\nHeader Token:: %@ \n Header URL:: %@ \n",authToken,authURL);

    [PMCommonClass userDefaultValueSave:authToken key:SOFTLAYER_AUTH_TOKEN];
    [PMCommonClass userDefaultValueSave:authURL key:SOFTLAYER_AUTH_URL];

    NSDictionary *mainDict = [PMCommonClass jsonDictionaryFromResponse:responseObject];
    NSLog(@"Main Dict:: %@",mainDict);
} failure:^(NSURLSessionTask *operation, NSError *error) {
    NSLog(@"Error:: %@", error.localizedDescription);
}];
}

2) After that from that "auth token" and "x-Storage-url" we can created container in object storage.

-(void)createConatiner{
NSString *authUrl = @"https://sjc01.objectstorage.softlayer.net/v1/AUTH_695d079a-ead3-4045-b303-a1b123acdb3db";
NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"%@/Container6",authUrl]];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.requestSerializer =  [AFHTTPRequestSerializer serializer];
[manager.requestSerializer setValue:authToken forHTTPHeaderField:@"X-Auth-Token"];
[manager PUT:URL.absoluteString parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    NSHTTPURLResponse *response = ((NSHTTPURLResponse *)[task response]);
    NSDictionary *headers = [response allHeaderFields];
    NSLog(@"Header Dict:: %@",headers);
    if ([response statusCode] == 201) {
        //Successfully container created
    }else{
        //Some problem occure
    }

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    NSLog(@"Error:: %@", error.localizedDescription);
}];}

3)At last upload video in container using "Auth token" and "x-Storage-url" with bellow code

-(void)uploadFileInContainer{
NSString *authUrl = @"https://sjc01.objectstorage.softlayer.net/v1/AUTH_695d079a-ead3-4045-b303-a1b123acdb3db";
//Get Video From Document Directory
NSError *error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *videoFilePath = [documentsDirectory stringByAppendingFormat:@"/vid10.mp4"];

//Convert video url to NSData
NSData *videoData = [NSData dataWithContentsOfFile:videoFilePath options:NSDataReadingMappedIfSafe error:&error];
if (videoData != nil ) {

    if ([PMCommonClass isNetworkRechable]) {

        NSString *urlString = [NSString stringWithFormat:@"%@/Container5/Video4.mp4",authUrl];
        NSMutableURLRequest *request  = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"PUT" URLString:urlString parameters:nil error:nil];
        [request setValue:authToken forHTTPHeaderField:@"X-Auth-Token"];
        [request setValue:@"video/mp4" forHTTPHeaderField:@"Content-Type"];
        [request setValue:@"video/mp4" forHTTPHeaderField:@"Accept"];

        NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
        AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:configuration];

        NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithRequest:request fromData:videoData progress:^(NSProgress * _Nonnull uploadProgress) {
            dispatch_async(dispatch_get_main_queue(), ^{
                NSLog(@"UPLOAD Progress==>%f",uploadProgress.fractionCompleted*100);
            });
        } completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
            if (error) {
                NSLog(@"Error: %@", error.localizedDescription);
            } else {
                NSLog(@"Success: %@ %@", response, responseObject);
            }
        }];
        [uploadTask resume];
    }
}}

4) Then download this uploaded object from object storage container using same process.

-(void)getObject{
NSString *authUrl = @"https://sjc01.objectstorage.softlayer.net/v1/AUTH_695d079a-ead3-4045-b303-a1b123acdb3db";
NSString *authToken = @"AUTH_tk9eafc199756fabe8066f";
NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"%@/Container5/Video4.mp4",authURL]];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.requestSerializer =  [AFHTTPRequestSerializer serializer];
[manager.requestSerializer setValue:authToken forHTTPHeaderField:@"X-Auth-Token"];
[manager.requestSerializer setValue:@"video/mp4" forHTTPHeaderField:@"Content-Type"];
[manager.requestSerializer setValue:@"video/mp4" forHTTPHeaderField:@"Accept"];
[manager GET:URL.absoluteString parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {

    NSHTTPURLResponse *response = ((NSHTTPURLResponse *)[task response]);
    NSDictionary *headers = [response allHeaderFields];
    NSLog(@"Header Dict:: %@",headers);

    //NSLog(@"Response OBJ:: %@",responseObject);
    //UIImage *image = [UIImage imageWithData:responseObject];
    //NSLog(@"uiiamge %@",image);


    NSData *videoData = responseObject;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *tempPath = [documentsDirectory stringByAppendingFormat:@"/video1026.mp4"];
    BOOL success = [videoData writeToFile:tempPath atomically:NO];
    if (success) {
        NSLog(@"save Path is:: %@",tempPath);
    }

} failure:^(NSURLSessionTask *operation, NSError *error) {
    NSLog(@"Error:: %@", error.localizedDescription);
}];}

That's the following step for upload and download video from softlayer object storage.

分享给朋友:
您可能感兴趣的文章:
随机阅读: