Advertisement
Guest User

Untitled

a guest
May 29th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.86 KB | None | 0 0
  1. using Microsoft.WindowsAzure.Storage;
  2. using Microsoft.WindowsAzure.Storage.Blob;
  3. using Microsoft.WindowsAzure.Storage.Table;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using WCFServiceWebRole1;
  10.  
  11. namespace Klient
  12. {
  13.     class Program
  14.     {
  15.         static void Main(string[] args)
  16.         {
  17.             Dictionary<String, UsersEntity> blobs = new Dictionary<String, UsersEntity>();
  18.             var account = CloudStorageAccount.DevelopmentStorageAccount;
  19.             CloudTableClient cl = account.CreateCloudTableClient();
  20.             var table = cl.GetTableReference("userzy");
  21.             table.CreateIfNotExists();
  22.             bool endFlag = false;
  23.             CloudBlobClient client = account.CreateCloudBlobClient();
  24.  
  25.             while (!endFlag)
  26.             {
  27.                 Console.WriteLine();
  28.                 Console.WriteLine("Wybierz opcję:");
  29.                 Console.WriteLine("1. Dodaj usera.");
  30.                 Console.WriteLine("2. Przesyłanie blobów.");
  31.                 Console.WriteLine("3. Odczytanie bloba.");
  32.                 Console.WriteLine("4. Lista wszystkich blobów.");
  33.                 Console.WriteLine("ESC. Wyjście.");
  34.                 Console.WriteLine();
  35.                 ConsoleKey option= Console.ReadKey().Key;
  36.                 Console.Clear();
  37.  
  38.                 switch (option)
  39.                 {
  40.                     case ConsoleKey.D1:
  41.                         Console.WriteLine();
  42.                         Console.WriteLine("Login:");
  43.                         String username = "";
  44.                         while (username == "")
  45.                         {
  46.                             username = Console.ReadLine();
  47.                         }
  48.  
  49.                         Console.WriteLine("Haslo:");
  50.                         String password = "";
  51.                         while (password == "")
  52.                         {
  53.                             password = Console.ReadLine();
  54.                         }
  55.                         var e = new UsersEntity(username, password);
  56.  
  57.                         TableOperation testOperation = TableOperation.Retrieve<UsersEntity>(username, password);
  58.                         var res = table.Execute(testOperation);
  59.                         UsersEntity test = (UsersEntity)res.Result;
  60.                         if (test == null)
  61.                         {
  62.                             TableOperation insertOperation = TableOperation.Insert(e);
  63.                             table.Execute(insertOperation);
  64.                             Console.WriteLine("Dodano usera: " + username);
  65.                         }
  66.                         else
  67.                         {
  68.                             Console.WriteLine("Uzytkownik: " + username + " istnieje juz w bazie");
  69.                         }
  70.                         break;
  71.                     case ConsoleKey.D2:
  72.  
  73.                         Console.WriteLine("Nazwa usera: ");
  74.                         String username2 = Console.ReadLine();
  75.                         Console.WriteLine("Haslo: ");
  76.                         String password2 = Console.ReadLine();
  77.                         Console.WriteLine("Nazwa bloba: ");
  78.                         String blobName = Console.ReadLine();
  79.                         Console.WriteLine("Content: ");
  80.                         String content = Console.ReadLine();
  81.  
  82.                         TableOperation testOperation2 = TableOperation.Retrieve<UsersEntity>(username2, password2);
  83.                         var res2 = table.Execute(testOperation2);
  84.                         UsersEntity test2 = (UsersEntity)res2.Result;
  85.                         if (test2 == null)
  86.                         {
  87.                             Console.WriteLine("Przesylanie nie powiodlo sie, poniewaz podany user " + username2 + " nie istnieje");
  88.                         }
  89.                         else
  90.                         {
  91.                             CloudBlobContainer container = client.GetContainerReference(username2 + password2);
  92.                             container.CreateIfNotExists();
  93.                             var blob = container.GetBlockBlobReference(blobName);
  94.                             var bytes = new System.Text.ASCIIEncoding().GetBytes(content);
  95.                             var s = new System.IO.MemoryStream(bytes);
  96.                             blob.UploadFromStream(s);
  97.                         }
  98.                         break;
  99.                     case ConsoleKey.D3:
  100.                         Console.WriteLine("Nazwa usera: ");
  101.                         String username3 = Console.ReadLine();
  102.                         Console.WriteLine("Haslo: ");
  103.                         String password3 = Console.ReadLine();
  104.                         Console.WriteLine("Nazwa bloba: ");
  105.                         String blobName3 = Console.ReadLine();
  106.  
  107.                         TableOperation testOperation3 = TableOperation.Retrieve<UsersEntity>(username3, password3);
  108.                         var res3 = table.Execute(testOperation3);
  109.                         UsersEntity test3 = (UsersEntity)res3.Result;
  110.                         if (test3 == null)
  111.                         {
  112.                             Console.WriteLine("Przesylanie nie powiodlo sie, poniewaz podany user " + username3 + " nie istnieje");
  113.                         }
  114.                         else
  115.                         {
  116.                             CloudBlobContainer container = client.GetContainerReference(username3 + password3);
  117.  
  118.                             var blob = container.GetBlockBlobReference(blobName3);
  119.                             var s2 = new System.IO.MemoryStream();
  120.                             blob.DownloadToStream(s2);
  121.                             string content3 = System.Text.Encoding.UTF8.GetString(s2.ToArray());
  122.  
  123.                             Console.WriteLine("Zawartosc bloba: " + content3);
  124.                         }
  125.                         break;
  126.                     case ConsoleKey.D4:
  127.                         Console.WriteLine("Lista wszystkich blobow:");
  128.                         Console.WriteLine("Nazwa usera: ");
  129.                         String username4 = Console.ReadLine();
  130.                         Console.WriteLine("Haslo: ");
  131.                         String password4 = Console.ReadLine();
  132.                         CloudBlobContainer container4 = client.GetContainerReference(username4 + password4);
  133.  
  134.                         string lst4 = "";
  135.                         foreach (IListBlobItem item in container4.ListBlobs(null, false))
  136.                         {
  137.                             if (item.GetType() == typeof(CloudBlockBlob))
  138.                             {
  139.                                 CloudBlockBlob blob = (CloudBlockBlob)item;
  140.                                 lst4 += String.Format("Block blob of length {0}: {1}",
  141.                                 blob.Properties.Length, blob.Uri) + "\n";
  142.                             }
  143.                             else if (item.GetType() == typeof(CloudPageBlob))
  144.                             {
  145.                                 CloudPageBlob pageBlob = (CloudPageBlob)item;
  146.                                 lst4 += String.Format("Page blob of length {0}: {1}",
  147.                                 pageBlob.Properties.Length, pageBlob.Uri) + "\n";
  148.                             }
  149.                             else if (item.GetType() == typeof(CloudBlobDirectory))
  150.                             {
  151.                                 CloudBlobDirectory directory = (CloudBlobDirectory)item;
  152.                                 lst4 += String.Format("Directory: {0}", directory.Uri) + "\n";
  153.                             }
  154.                         }
  155.                         Console.Write(lst4);
  156.                         break;
  157.                     case ConsoleKey.Escape:
  158.                         endFlag = true;
  159.                         break;
  160.  
  161.                 }
  162.             }
  163.         }
  164.     }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement