using System; using System.IO; // Add web references to: // https://services.nirvanix.com/ws/authentication.ashx using NirvanixSampleApp.AuthenticationServices; // https://services.nirvanix.com/ws/imfs.ashx using NirvanixSampleApp.IMFSServices; // https://services.nirvanix.com/ws/image.ashx using NirvanixSampleApp.ImageServices; // https://node1.nirvanix.com/ws/transfer.ashx using NirvanixSampleApp.TransferServices; namespace NirvanixSampleApp { class Program { static void Main(string[] args) { // Every account has an application key, username, and password. Sign for an //application key at https://nmp.nirvanix.com. string applicationKey = "your application key"; string username = "your username"; string password = "your password"; // Login to your account to retrieve your session token. Console.WriteLine("Logging in..."); Authentication auth = new Authentication(); string sessionToken = auth.Login(applicationKey, username, password); Console.WriteLine("Login successful"); // Upload a file to my account UploadFile(sessionToken); // List files in a specific folder ListFiles(sessionToken); // Copy the uploaded file to a new folder CopyFile(sessionToken); // Rename the file I just copied. RenameFile(sessionToken); // Resize the image file that was previously uploaded ResizeImageFile(sessionToken); // Delete folders DeleteFolders(sessionToken); // Logout of your account Console.WriteLine("Logging out..."); auth.Logout(sessionToken); Console.WriteLine("Logout successful"); } private static void UploadFile(string sessionToken) { // I want to upload a file called ‘myimage.jpg’ that resides on my local hard drive // and put this file into my Nirvanix account. string filename = @"C:\temp\myimage.jpg"; // Set the Nirvanix file path that you want to place this file in. // It doesn’t matter if the destination folder "UploadedFiles/Images"already // exists. If it doesn’t it will be created automatically. string nirvanixFilePath = "UploadedFiles/Images/myimage.jpg"; // Get the file size FileInfo fileInfo = new FileInfo(filename); long fileSize = fileInfo.Length; // Read in all of the file data into a byte array byte[] fileData = new byte[fileSize]; using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read)) { fs.Read(fileData, 0, (int)fileSize); } // Get the upload location for this file IMFS imfs = new IMFS(); UploadNode uploadNode = imfs.GetUploadNode(sessionToken, fileSize); // Initialize the connection to the upload location Transfer transfer = new Transfer(); UriBuilder uriBuilder = new UriBuilder(transfer.Url); uriBuilder.Host = uploadNode.IPAddress; transfer.Url = uriBuilder.ToString(); // Upload the file and you’re done! Console.WriteLine("Uploading file..."); transfer.UploadFile(uploadNode.AccessToken, nirvanixFilePath, fileData); Console.WriteLine("Upload complete!"); } private static void ListFiles(string sessionToken) { // Since I just uploaded a file, I want to list the folder to see that the // file is in fact there. string nirvanixFolder = "UploadedFiles/Images"; // Initialize parameters for listing the folder int pageNumber = 1; int pageSize = 10; string sortCode = "Name"; bool sortDescending = false; // List the folder contents IMFS imfs = new IMFS(); FSFolderList folderList = imfs.ListFolder(sessionToken, nirvanixFolder, pageNumber, pageSize, sortCode, sortDescending); // List of the files in this folder. Print out the filename, file size // and the date and time the file was added to the account Console.WriteLine(); foreach (FSFileAttributes fileAttributes in folderList.File) { Console.WriteLine("Filename: " + fileAttributes.Name); Console.WriteLine("File Size: " + fileAttributes.SizeBytes); Console.WriteLine("Created Date: " + fileAttributes.CreatedDate); Console.WriteLine(); } } private static void CopyFile(string sessionToken) { // Initialize parameters for copying the file. It doesn’t matter if // the destination folder exists, it will be created automatically // if it doesn’t. string fileToCopy = "UploadedFiles/Images/myimage.jpg"; string destinationFolder = "CopiedFiles/Images"; // Copy the file IMFS imfs = new IMFS(); string[] filesToCopy = { fileToCopy }; Console.WriteLine("Copying file..."); imfs.CopyFiles(sessionToken, filesToCopy, destinationFolder); Console.WriteLine("Copy file complete"); } private static void RenameFile(string sessionToken) { // Initialize parameters for renaming the file. string fileToRename = "CopiedFiles/Images/myimage.jpg"; string newFilename = "myrenamedimage.jpg"; // Rename the file IMFS imfs = new IMFS(); Console.WriteLine("Renaming file..."); imfs.RenameFile(sessionToken, fileToRename, newFilename); Console.WriteLine("Rename file complete"); } private static void ResizeImageFile(string sessionToken) { // Set the file path that you want to resize string srcFilePath = "UploadedFiles/Images/myimage.jpg"; // Set the file path for the resized image. Since the extension // of the resized image file is .gif, the file will also be automatically // converted to GIF format string destFilePath = "UploadedFiles/Images/MyResizedImage.gif"; // Resize the image to 320 x 240 Image image = new Image(); image.Resize(sessionToken, srcFilePath, destFilePath, 320, 240, null); } private static void DeleteFolders(string sessionToken) { // Initialize parameters for deleting folders. All subfolders // and files in those folders will also be automatically deleted. string[] foldersToDelete = { "UploadedFiles", "CopiedFiles" }; // Delete the folders IMFS imfs = new IMFS(); Console.WriteLine("Deleting folders..."); imfs.DeleteFolders(sessionToken, foldersToDelete); Console.WriteLine("Delete folders complete"); } } }