About File And Folder Operations

About File and Folder Operations

Contents

File Download

When you make an API call to download a file, the file is downloaded to a temporary directory. Once the API which downloaded the file is released, the file is deleted. To keep the file, use completion callback to move it to a different location.

This example illustrates a call to downloadFile:progress:withCompletionHandler: API which downloads a file to a temporary directory.

downloadFile

  1. If you have not done that already, add Cloud SDK framework for IOS to an existing or a new IOS application.
  2. Include VerizonCloudAPI.framework header file in you class: #import <VerizonCloudAPI/VerizonCloudAPI.h>
  3. Initalize the PCFilesAPIParams class and provide the required parameter. If the required parameter is not provided, the API call results in NSInvalidArgumentException exception.
  4. Initalize the PCAPISDK object and call the downloadFile:progress:withCompletionHandler: method.
  5. Perform the downloadFile:progress:withCompletionHandler: API call using the code sample in this example.
  6. If the session is no longer valid:
    1. Call the logout API which the removes the access token stored in the keychain.
    2. Call authenticate:fromViewController:withCompletionHandler: API to get a new access token.

Code Sample

@try {
        PCFilesAPIParams *params = [[PCFilesAPIParams alloc] init];
        params.path = @"full_path"; // full path to the file (required).
        
        [[PCAPISDK sharedInstance] downloadFile:params progress:^(long long totalBytesDownloaded, long long totalBytesToBeDownloaded) {
            
            NSLog(@"Total Bytes Downloaded %lld \n total Bytes To Be Downloaded %lld", totalBytesDownloaded, totalBytesToBeDownloaded);
            
        } withCompletionHandler:^(PCAPIQueryResponse *response) {
            switch (response.urlResponse.statusCode) {
                case PCAPIResponseCodeOK: {
                    NSData *fileData    = [NSData dataWithContentsOfURL:response.downloadedURL];
                    UIImage *image = [UIImage imageWithData:fileData];
                }
                break;
                case PCAPIResponseCodeUnauthorized:
                    NSLog(@"Session is no longer valid.");
                break;
                case PCAPIResponseCodeServiceUnavailable:
                    NSLog(@"Server Unavailable");
                break;
                default:
                    NSLog(@"failed. Please try again!");
                break;
            }
        }];
    }
    @catch (NSException * e) {
        NSLog(@"catching %@ reason %@", [e name], [e reason]);
    }

File Upload

Call uploadFile:readFromPath:progress:withCompletionHandler: method to upload files to a folder in a user’s Personal Cloud Storage account.

NOTE: You cannot perform uploadFile:readFromPath:progress:withCompletionHandler: operatioin on a virtual folder.

uploadFile

This example illustrates a call to uploadFile:readFromPath:progress:withCompletionHandler: API. This API call uploads a file to a folder.

  1. If you have not done that already, add Cloud SDK framework for IOS to an existing or a new IOS application.
  2. Include VerizonCloudAPI.framework header file in you class: #import <VerizonCloudAPI/VerizonCloudAPI.h>
  3. Initalize the PCUploadAPIParams class and provide the required parameter. If the required parameter is not provided, the API call results in NSInvalidArgumentException exception.
  4. Initalize the PCAPISDK object and call the uploadFile:readFromPath:progress:withCompletionHandler: method.
  5. Perform the uploadFile:readFromPath:progress:withCompletionHandler: API call using the code sample in this example.
  6. If the session is no longer valid:
    1. Call the logout API which the removes the access token stored in the keychain.
    2. Call authenticate:fromViewController:withCompletionHandler: API to get a new access token.

Code Sample

@try {
        PCUploadAPIParams *params = [[PCUploadAPIParams alloc] init];
        params.path = @"path"; // The path to the folder where file has to be uploaded. (required)
        params.name = @"name"; // Name of the file to be uploaded. (required)
        NSString *filePath = @"filePath"; // local path of the file from where file has to be read.
        
        [[PCAPISDK sharedInstance] uploadFile:params readFromPath:filePath progress:^(PCUploadAPIState uploadState, long long totalBytesUploaded, long long totalBytesToBeUploaded) {
            
            NSLog(@"Total Bytes Uploaded %lld \n total Bytes To Be Uploaded %lld",totalBytesUploaded,totalBytesToBeUploaded);
            
        } withCompletionHandler:^(PCResource *metadata, PCAPIQueryResponse *response) {
            switch (response.urlResponse.statusCode) {
                case PCAPIResponseCodeOK:
                case PCAPIResponseCodeResourceCreated:
                    NSLog(@"File uploaded to a given path");
                break;
                case PCAPIResponseCodeUnauthorized:
                    NSLog(@"Session is no longer valid.");
                break;
                case PCAPIResponseCodeServiceUnavailable:
                    NSLog(@"Server Unavailable");
                break;
                default:
                    NSLog(@"failed. Please try again!");
                break;
            }
        }];
    }
    @catch (NSException * e) {
        NSLog(@"catching %@ reason %@", [e name], [e reason]);
    }

Refresh Local Cache

To refresh the local cache of files, call doFullSyncWithCompletionHandler: method.

FullSync

  1. If you have not done that already, add Cloud SDK framework for IOS to an existing or a new IOS application.
  2. Include VerizonCloudAPI.framework header file in you class: #import <VerizonCloudAPI/VerizonCloudAPI.h>
  3. Initalize the PCAPISDK object and call the doFullSyncWithCompletionHandler: method.
  4. Perform the doFullSyncWithCompletionHandler: API call using the code sample in this example. You can find the result (success or failure) in the completion block.
  5. If the session is no longer valid:
    1. Call the logout API which the removes the access token stored in the keychain.
    2. Call authenticate:fromViewController:withCompletionHandler: API to get a new access token.

This code sample allows you to refresh the local cache of files.

Code Sample

@try {
        [[PCAPISDK sharedInstance] doFullSyncWithCompletionHandler:^(PCAPIQueryResponse *response) {
            switch (response.urlResponse.statusCode) {
                case PCAPIResponseCodePreconditionFailed:
                case PCAPIResponseCodeOK: {
                    NSLog(@"Full sync API successfully refreshed local cache of the files");
                }
                    break;
                case PCAPIResponseCodeUnauthorized:
                    NSLog(@"Session is no longer valid");
                    break;
                case PCAPIResponseCodeServiceUnavailable:
                    NSLog(@"Server Unavailable");
                    break;
                default:
                    NSLog(@"Full sync API failed");
                    break;
            }
        }];
    } 
    @catch (NSException *exception) {
          NSLog(@"Catching %@ reason %@", [exception name], [exception reason]);
    }

Thumbnails

To download thumbnails for any resource in user’s Personal Cloud Storage account, call downloadThumbnail:withCompletionHandler: method.

  1. If you have not done that already, add Cloud SDK framework for IOS to an existing or a new IOS application.
  2. Include VerizonCloudAPI.framework header file in you class: #import <VerizonCloudAPI/VerizonCloudAPI.h>
  3. Initalize the PCThumbnailAPIParams class and provide the required parameter. If the required parameter is not provided, the API call results in NSInvalidArgumentException exception.
  4. Initalize the PCAPISDK object and call the downloadThumbnail:withCompletionHandler: method.
  5. Perform the downloadThumbnail:withCompletionHandler: API call using the code sample in this example.
  6. If the session is no longer valid:
    1. Call the logout API which the removes the access token stored in the keychain.
    2. Call authenticate:fromViewController:withCompletionHandler: API to get a new access token.

Download Medium-Size Thumbnails

This code sample downloads thumbnails with medium size image - 128x128 pixel size.

Code Sample


@try {
        PCThumbnailAPIParams *params = [[PCThumbnailAPIParams alloc] init];
        params.contentToken = @"contentToken"; // contentToken of the file
        params.size = @"m"; // size of the thumbnail image: 'xs' (24x24), 's' (64x64), or 'm' (128x128).
        
        [[PCAPISDK sharedInstance] downloadThumbnail:params withCompletionHandler:^(PCAPIQueryResponse *response) {
            
            switch (response.urlResponse.statusCode) {
                case PCAPIResponseCodeOK: {
                    NSData *fileData = [NSData dataWithContentsOfURL:response.downloadedURL];
                    UIImage *image   = [UIImage imageWithData:fileData];
                    NSLog(@"Image downloaded successfully");
                }
                    break;
                case PCAPIResponseCodeUnauthorized:
                    NSLog(@"Session is no longer valid");
                    break;
                case PCAPIResponseCodeServiceUnavailable:
                    NSLog(@"Server Unavailable");
                    break;
                default: {
                    NSLog(@"Image download failed");
                }
                    break;
            }
        }];
    }
    @catch (NSException *exception) {
        NSLog(@"Catching %@ reason %@", [exception name], [exception reason]);
        
    }

Download Large Thumbnails

This code sample downloads thumbnails with large size image - 320x320 pixel size.

Code Sample

 @try {
        PCThumbnailAPIParams *params = [[PCThumbnailAPIParams alloc] init];
        params.contentToken = @"contentToken"; // contentToken of the file
        
        params.th = 320; // Thumbnail image height(in pixels).
        params.tw = 320; // Thumbnail image width(in pixels).
        
        [[PCAPISDK sharedInstance] downloadThumbnail:params withCompletionHandler:^(PCAPIQueryResponse *response) {
            
            switch (response.urlResponse.statusCode) {
                case PCAPIResponseCodeOK: {
                    NSData *fileData = [NSData dataWithContentsOfURL:response.downloadedURL];
                    UIImage *image   = [UIImage imageWithData:fileData];
                    NSLog(@"Image downloaded successfully");
                }
                    break;
                case PCAPIResponseCodeUnauthorized:
                    NSLog(@"Session is no longer valid");
                    break;
                case PCAPIResponseCodeServiceUnavailable:
                    NSLog(@"Server Unavailable");
                    break;
                default: {
                    NSLog(@"Image download failed");
                }
                    break;
            }
        }];
    }
    @catch (NSException *exception) {
        NSLog(@"Catching %@ reason %@", [exception name], [exception reason]);
    }

Copyright © 2015-2017, Verizon and/or its Licensors. All rights reserved.