2016-06-23 3 views
3

ich erstelle windows-anwendung mit cis zu azurblob blobs zu verwalten. Ich bin in der lage, die blobs, laden die blobs, nehmen snapshot, liste die blobs etc. Jetzt laden Sie die Snapshots von Blobs herunter (laden Sie die ältere Version der Datei herunter).wie die azurblauen blob snapshots mit cis in windows form applikation herunterladen

Ich bin Download Blob-Datei mit dem folgenden Code

StorageCredentials creds = new StorageCredentials(accountName, accountKey); 
     CloudStorageAccount account = new CloudStorageAccount(creds, useHttps: true); 

     //MessageBox.Show(sender.ToString()); 
     Uri myUri; 
     string uri; 
     var btn = sender as Button; 
     uri = btn.Text; 
     if (btn != null) 
     { 
      // MessageBox.Show(btn.Text); 

      myUri = new Uri(btn.Text); 
      MessageBox.Show(myUri.ToString()); 

     } 
     // Create the blob client. 
     CloudBlobClient blobClient = account.CreateCloudBlobClient(); 
     // Retrieve reference to a previously created container. 
     CloudBlobContainer container = blobClient.GetContainerReference("samples"); 
     //CloudBlobContainer sampleContainer = client.GetContainerReference("samples"); 

     string[] parts = uri.Split('/'); 
     string fileName = ""; 

     if (parts.Length > 0) 
      fileName = parts[parts.Length - 1]; 
     else 
      fileName = uri; 


     CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName); 

     // Save blob contents to a file. 
     try 
     { 


      using (var fileStream =  System.IO.File.OpenWrite(@"C:\Users\dev1\Desktop\rakshi1.jpg")) 
      { 
       blockBlob.DownloadToStream(fileStream); 
      } 
     } 

     catch (Exception ex) 
     { 
      MessageBox.Show(ex.ToString()); 
     } 

Ich bin Auflistung der Schnappschuss von perticular Blob mit dem folgenden Code

 

private void button1_Click(object sender, EventArgs e) 
     { 
      //MessageBox.Show(this.filename); 
      //label1 

      string s1= this.filename; 
      string accountName = "portalvhdsq3jyv0y3gccrn"; 
      string accountKey = "VVPgjNO9V3397kOvoJRRZKtZVZaVNQP2xFPTNoWEp8zPJh4n487HVmwup498T8iufFnDS1Icu0EmUKyHg+DdkA=="; 


      StorageCredentials creds = new StorageCredentials(accountName, accountKey); 
      CloudStorageAccount account = new CloudStorageAccount(creds, useHttps: true); 
      CloudBlobClient client = account.CreateCloudBlobClient(); 
      CloudBlobContainer sampleContainer = client.GetContainerReference("samples"); 
      sampleContainer.CreateIfNotExists(); 
      CloudBlockBlob blob = sampleContainer.GetBlockBlobReference(s1); 

      CloudBlockBlob newBlob; 

      //set the metadata and save it 
      blob.Metadata["OriginalFilename"] = s1; 
      blob.SetMetadata(); 
      //create the snapshot 
      newBlob = blob.CreateSnapshot(); 
      /* 
      label3.Text ="Metadata['OriginalFilename'] = {0}, IsSnapshot = {1}, " + "SnapshotTime = {2}, snapshotURI = {3}"+ " "+newBlob.Metadata["OriginalFilename"]+" "+newBlob.IsSnapshot+" "+newBlob.SnapshotTime+" "+newBlob.SnapshotQualifiedUri; 
      */ 
      // label3.Text = ""; 
      string text1 = ""; 
      //retrieve all of the versions for this blob, then iterate through them 
      IEnumerable listOfBlobs = sampleContainer.ListBlobs(s1, true, BlobListingDetails.Snapshots); 
      foreach (IListBlobItem blobItem in listOfBlobs) 
      { 
       //you must cast this as a CloudBlockBlob 
       // because blobItem does not expose all of the properties 
       CloudBlockBlob theBlob = blobItem as CloudBlockBlob; 

       //Call FetchAttributes so it retrieves the metadata. 
       theBlob.FetchAttributes(); 

       //print the snapshot informatino 
       /* System.Diagnostics.Debug.Print("theBlob IsSnapshot = {0}, SnapshotTime = {1}, snapshotURI = {2}", 
        theBlob.IsSnapshot, theBlob.SnapshotTime, theBlob.SnapshotQualifiedUri);*/ 

       // text1 = text1 + "theBlob IsSnapshot = {0}, SnapshotTime = {1}, snapshotURI = {2}" + " @" + theBlob.IsSnapshot + " " + theBlob.SnapshotTime + " " + theBlob.SnapshotQualifiedUri; 



       //in case there's more than one piece of metadata, 
       // iterate through the metadata and display each key-value pair 
       int index = 0; 
       foreach (KeyValuePair kvPair in theBlob.Metadata) 
       { 
        index++; 
       //  text1 = text1 + "@.MetaData {0} = {1},{2}@" + index + kvPair.Key + kvPair.Value; 
       } 
       createButton(theBlob.SnapshotQualifiedUri); 
      //  text1 = text1 + "@@@"; 
       MessageBox.Show(theBlob.SnapshotQualifiedUri.ToString()); 

      } 
     // text1 = text1.Replace("@", System.Environment.NewLine); 

      // label1.Text = text1; 
      button2.Enabled = true; 
     } 

Aber ich habe keine Ahnung, wie die einzelnen Schnappschüsse zum Download

Ich aktualisiere diesen Code mit create_button und btn_click Methode

 




    private void createButton(Uri uri1) 
     { 
      MessageBox.Show("hhhhhhhis"); 

      //This block dynamically creates a Button and adds it to the form 
      Button btn = new Button(); 
      btn.Name = "btn1"; 
      btn.Location = new Point(3 + i, 14 + x); 
      btn.BackColor = System.Drawing.Color.White; 
      btn.Text = uri1.ToString(); 
      btn.Width = 370; 
      btn.TextAlign = ContentAlignment.MiddleLeft; 
      //Hook our button up to our generic button handler 
      btn.Click += new EventHandler(btn_Click); 
      this.Controls.Add(btn); 
      // textBox1.Text = textBox1.Text + "hai"; 
      //i += 20; 
      x += 30; 

     } 



void btn_Click(object sender, EventArgs e) 
     { 
      StorageCredentials creds = new StorageCredentials(accountName, accountKey); 
      CloudStorageAccount account = new CloudStorageAccount(creds, useHttps: true); 

      //MessageBox.Show(sender.ToString()); 
      Uri myUri; 
      string uri; 
      var btn = sender as Button; 
      uri = btn.Text; 
      if (btn != null) 
      { 
       // MessageBox.Show(btn.Text); 

       myUri = new Uri(btn.Text); 
       MessageBox.Show(myUri.ToString()); 

      } 
      // Create the blob client. 
      CloudBlobClient blobClient = account.CreateCloudBlobClient(); 
      // Retrieve reference to a previously created container. 
      CloudBlobContainer container = blobClient.GetContainerReference("samples"); 
      //CloudBlobContainer sampleContainer = client.GetContainerReference("samples"); 
      // Retrieve reference to a blob named "photo1.jpg". 
      string[] parts = uri.Split('/'); 
      string fileName = ""; 

      if (parts.Length > 0) 
       fileName = parts[parts.Length - 1]; 
      else 
       fileName = uri; 
      // MessageBox.Show(fileName); 

      CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName); 

      // Save blob contents to a file. 
      try 
      { 
       // using (Stream outputFile = new FileStream("rakshi.jpg", FileMode.Create)) 
       //{ 
       //blockBlob.DownloadToStream(outputFile); 
       //} 


       using (var fileStream = System.IO.File.OpenWrite(@"C:\Users\dev1\Desktop\rakshi1.jpg")) 
       { 
        blockBlob.DownloadToStream(fileStream); 
       } 
      } 

      catch (Exception ex) 
      { 
       // Console.WriteLine(ex); 
       MessageBox.Show(ex.ToString()); 
      } 







     } 



Create_button() Funktion aufruft, für jeden Schnappschuss und eine andere Form mit Liste der Schaltfläche echte Schnappschüsse anzuzeigen, wenn ich auf den Button klicken sollte ich die snapshot.but herunterladen jetzt feuert es einige Ausnahme

+0

Sie können einen Snapshot heraufstufen oder einen Snapshot in einen anderen Blob kopieren und dann lesen. – Aravind

+0

'wenn ich auf den Knopf klicke, sollte ich den Schnappschuss herunterladen. Aber jetzt löst es eine Ausnahme aus -> Können Sie bitte die Ausnahme beschreiben? –

Antwort

2

Es ist eigentlich ziemlich gerade nach vorne! Sobald Sie festgestellt haben, dass die IsSnapshot Eigenschaft des Blobs wahr ist, können Sie einfach die Download-Funktion für diesen Blob aufrufen. Siehe den Beispielcode hier:

static void DownloadBlobSnapshot() 
    { 
     var cred = new StorageCredentials(accountName, accountKey); 
     var account = new CloudStorageAccount(cred, true); 
     var client = account.CreateCloudBlobClient(); 
     var container = client.GetContainerReference("snapshot-download-test"); 
     IEnumerable listOfBlobs = container.ListBlobs(null, true, BlobListingDetails.Snapshots); 
     var downloadPath = @"D:\temp\"; 
     foreach (IListBlobItem blobItem in listOfBlobs) 
     { 
      var theBlob = blobItem as CloudBlockBlob; 
      if (theBlob != null) 
      { 
       if (theBlob.IsSnapshot) 
       { 
        theBlob.DownloadToFile(downloadPath + theBlob.Name.Replace("/", "\\"), FileMode.Create); 
       } 
      } 
     } 
    } 

Sie brauchen nicht Blobs Attribut oder Metadaten zu holen. Offensichtlich ist der obige Code ziemlich einfach. Wenn ein Blob mehrere Snapshots enthält und Sie jeden Snapshot separat speichern möchten, müssen Sie den Snapshot-Datum/die Uhrzeit des Blobs in den Dateinamen einfügen.

+0

Sir danke.Ich möchte alle Snapshots von pertikulären Blob anzeigen, und wenn ich auf pentric Snapshot klicken, sollte es heruntergeladen werden .Kann ich den downloadbaren Link von Snapshot erhalten – rakshi

+2

Für den Snapshot-URI können Sie 'SnapshotQualifiedUri' Eigenschaft des Blobs verwenden. Sie erhalten die URL des Blob-Snapshots. –

+0

aber diese URL ist das gleiche wie Basis-URL. Ich bin nicht in der Lage, den Snapshot mit dieser URL herunterladen. Download-Basis-Blob – rakshi