Service agnostic cloud stage access with TMS Cloud Pack f.net Introduction Cloud stage quickly became an accepted technology f various scenarios. Whether it is f backup purposes, sharing files with colleagues a mechanism to ensure critical data is available anytime and anywhere, cloud stage offers an ideal solution. The maj players these days in cloud stage offerings are Dropbox, Google Drive, Microsoft OneDrive and Box.NET. Each of these services also offers their own API to access cloud stage files upload files on the cloud stage belonging to an user account. With TMS Cloud Pack f.net, you can now write an application that has built-in access to a cloud stage that is agnostic of the cloud stage service used. With a single interface, the application gains full access to the cloud stage of choice. In addition to these web cloud stage services, also WebDav server access has been covered by this same common interface. What s me, this agnostic approach is valid f the different platfms.net suppts: WinFms, WPF, Windows Ste apps, ASP.NET apps The ICloudStage interface In TMS Cloud Pack f.net, the stage access is unified with the ICloudStage interface. Our different components f Dropbox, Google Drive, Microsoft OneDrive and Box.NET all offer the ICloudStage interface. private ICloudStage Stage get; set; Internally, in TMS Cloud Pack f.net, the architecture is: ICloudStage interface definition:
public interface ICloudStage : ICloudStageBase CloudStageItemBase Copy(CloudStageItemBase itemsource, string target); CloudStageItemBase Move(CloudStageItemBase itemsource, string target); CloudStageItemBase Download(CloudStageItemBase item); CloudStageItemBase Upload(CloudStageItemBase item); CloudStageItemBase Edit(CloudStageItemBase item); CloudStageItemBase CreateDir(CloudStageItemBase item); CloudStageItemBase RemoveDir(CloudStageItemBase item); CloudStageItemBase DeleteFile(CloudStageItemBase item); ICloudStageFolderCollection Folders get; WebDav,CalDav,CardDav,Box,Dropbox,OneDrive,GoogleDrive are components that currently implement this interface. Creating your first cloud stage agnostic application In this article, we ll provide step by step guidelines to create your first WinFms application that will be able to seamlessly integrate with your cloud stage services of choice. Of course, you need to start by registering your application with each of these services to obtain a unique application key and secret and set a proper callback URL. The TMS Cloud Pack f.net developers guide provides instructions where you can register your application f each of these services. When you have your application key and secret, you can start by dropping the cloud stage service components of your choice to the fm:
In this sample we will use Google Drive, Box.NET, DropBox and OneDrive. Also drop an OAuthWinfmPanel component on the fm that will be used f the authentication/authization step. Each cloud stage component has a property Authenticat and under this Authenticat property you can find the OAuthPanel reference. Set f each cloud stage component Authenticat.OAuthPanel to the instance of the OAuthWinfmPanel: CloudStage.Authenticat.OAuthPanel = OAuthWinfmsPanel1; Next configure the component with the application key and secret. You can do this programmatically via properties CloudStage.Authenticat.OAuthApplication.Key and CloudStage.Authenticat.OAuthApplication.Secret.
You can also create a XML file that contains these settings OAuth.CFG and use LoadSettings() to have the component retrieve the settings. In the next step, wire-up the AfterOpen event f each of the cloud stage components. This event is triggered when the component could obtain a successful connection to the cloud stage service. In this event, the application could fill f example a treeview with the structure of the files as retrieved from the cloud stage: private void stage_afteropen(object sender, ref bool handled) opened = true; ToggleControls(); WinFmsBase.FillTreeView(treeView1, Stage.Folders); FillTreeView is a method available in the WinFmsBase namespace that is just a utility method to allow you to easily populate a treeview component. (Note that equivalent methods are available to fill an ASP.NET treeview f example) The Node.Tag will be set to an ICloudStageItem object, you can use that to access the selected item like this: ICloudStageItem di = (CloudStageItemBase)treeView1.SelectedNode.Tag; and perfm further actions on it. Once this is setup, the application is ready to use the cloud stage service of choice and then wk with this stage service solely through the unified ICloudStage & ICloudStageItem interfaces.
Connecting A global private interface Stage is used that simply points to the cloud stage component that we want to use: private ICloudStage Stage get; set; and that interface could be initialized from a select of the cloud service: switch (CloudStageIndex) case 0: Stage = dropbox1; break; case 1: Stage = onedrive1; break; case 2: Stage = googledrive1; break; case 3: Stage = box1; break; and with this interface, the connection to the cloud stage service can be started with Stage.Open(); This will bring up the authentication / authization fm and when successfully completed, it will trigger the AfterOpen event of the Stage. Retrieving folder infmation ICloudStage.Folders points to the root of the cloud stage. F files and folders under root, Items exist: You can access files and folders within this root folder with: ICloudStageItem ICloudStage.Folders[0].Items[x] F example, to loop through all files and folders within a folder, you can use: feach (ICloudStageItem item in Stage.Folders[0].Items) // do an action on the item
If the item is a folder, it is indicated by item.itemtype and files under this folder can in turn be retrieved with: feach (ICloudStageItem item in Stage.Folders[0].Items) if (item.itemtype == ItemType.Folder) feach (ICloudStageItem subitem in item.items) // get items in subfolder here Note that this folder structure has a lazy load design, so this means that only when you are trying to access it, the component will populate it from the server, significantly benefitting the perfmance when you wk with a cloud stage account that has a large amount of files. Upload/Download You can download files easily when you have the ICloudStageItem interface. Note that when you would have used the FillTreeview method, the Node.Tag of a node in this treeview contains such an ICloudStageItem interface. To retrieve this interface f the selected treeview node: ICloudStageItem Item = (CloudStageItemBase)treeView1.SelectedNode.Tag; Once this ICloudStageItem is retrieved, downloading the file that is represented by this interface can be done with: item.savetopath(mypath); item.download(myfilename); item.download(astream); If your selected ICloudStageItem interface is a Folder, then you can upload a file to this folder with following commands:
var item = FolderItem.Items.InsertFile(MyLocalFilePath); var item = FolderItem.Items.InsertFile(AStream); Note that InsertFile() does not start the upload yet. To allow f uploading several files at once, you can first add multiple files with InsertFile and then start the actual multi-file upload with: FolderItem.Post() To replace an existing file, you would: item.edit(stream); item.edit(myfilename); and finalize the update with: FolderItem.Post() Creating folders To create a folder in the root under a specific folder, you can use: var item = CurrentItem.Items.CreateDir(MyFolderName); Followed by item.post(); CurrentItem.Items.Post() Note that equivalent to file uploading, you can create multiple folders and the actual folder creation on the stage service is only executed when you call Post().
Delete a File Folder You can delete multiple files with following code: followed by f (var i = 0 ; i < CurrentItem.Items.Count; i++) CurrentItem.Items.Delete(I); CurrentItem.Items.Post(); CurrentItem.Items[index].Delete(); CurrentItem.Items[index].Post; Share a File Folder You can create a share link f a file on the cloud stage simply by calling the following command GetShare() and you can send this link to other people to download the file: CurrentItem.GetShare().Link Conclusion TMS Cloud Pack f.net completely eliminates the need to write service specific code f integrating cloud stage services in your.net applications. With one set of components, you can add access to the wld s most popular cloud stage services in a simple and consistent way. At this moment, we offer access to 4 popular cloud stage services and also WebDav servers and our team is already busy developing me component wrappers f other cloud stage services. Me infmation, trial version, demos and documentation about TMS Cloud Pack f.net can be found at: http://www.tmssoftware.com/site/tmscloudnet.asp