C# 如何在windows窗体应用程序中使用c sharp下载azure blob快照

C# 如何在windows窗体应用程序中使用c sharp下载azure blob快照,c#,windows,azure,C#,Windows,Azure,我正在使用c sharp创建windows应用程序来管理azure Blob。我可以上载Blob、下载Blob、拍摄快照、列出Blob等。现在,我的任务是下载Blob的快照(下载文件的旧版本) 我使用以下代码下载blob文件 StorageCredentials creds = new StorageCredentials(accountName, accountKey); CloudStorageAccount account = new CloudStorageAccount

我正在使用c sharp创建windows应用程序来管理azure Blob。我可以上载Blob、下载Blob、拍摄快照、列出Blob等。现在,我的任务是下载Blob的快照(下载文件的旧版本)

我使用以下代码下载blob文件

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());
        }
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; } 我使用以下代码列出了perticularblob的快照

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());
        }
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; } 私有无效按钮1\u单击(对象发送者,事件参数e) { //MessageBox.Show(this.filename); //标签1 字符串s1=this.filename; 字符串accountName=“portalvhdsq3jyv0y3gccrn”; 字符串accountKey=“VVPgjNO9V3397kOvoJRRZKtZVZaVNQP2xFPTNoWEp8zPJh4n487HVmwup498T8iufFnDS1Icu0EmUKyHg+DdkA=”; StorageCredentials creds=新的StorageCredentials(accountName,accountKey); CloudStorageAccount=新的CloudStorageAccount(creds,useHttps:true); CloudBlobClient=account.CreateCloudBlobClient(); CloudBlobContainer sampleContainer=client.GetContainerReference(“示例”); sampleContainer.CreateIfNotExists(); CloudBlockBlob blob=sampleContainer.GetBlockBlobReference(s1); CloudBlockBlob-newBlob; //设置元数据并保存它 blob.Metadata[“OriginalFilename”]=s1; SetMetadata(); //创建快照 newBlob=blob.CreateSnapshot(); /* label3.Text=“Metadata['OriginalFilename']={0},IsSnapshot={1},”+“SnapshotTime={2},snapshotURI={3}”+“”+newBlob.Metadata['OriginalFilename]+“”+newBlob.IsSnapshot+++newBlob.SnapshotTime+++newBlob.SnapshotQualifiedUri; */ //标签3.Text=“”; 字符串text1=“”; //检索此blob的所有版本,然后遍历它们 IEnumerable listOfBlobs=sampleContainer.ListBlobs(s1,true,BlobListingDetails.Snapshots); foreach(ListBlobBlob中的IListBlobItem blobItem) { //您必须将其转换为CloudBlockBlob //因为blobItem不会公开所有属性 CloudBlockBlob theBlob=blobItem作为CloudBlockBlob; //调用FetchAttributes,以便检索元数据。 theBlob.FetchAttributes(); //打印快照信息o /*System.Diagnostics.Debug.Print(“theBlob IsSnapshot={0},SnapshotTime={1},snapshotURI={2}”, 该blob.IsSnapshot,该blob.snapshot时间,该blob.snapshot在i)期间合格*/ //text1=text1+“theBlob IsSnapshot={0},SnapshotTime={1},snapshotURI={2}”+“@”+theBlob.IsSnapshot+”“+theBlob.SnapshotTime+”“+theBlob.SnapshotTime+”“+theBlob.snapshot-qualifieduri; //如果有多个元数据, //遍历元数据并显示每个键值对 int指数=0; foreach(blob.Metadata中的KeyValuePair kvPair) { 索引++; //text1=text1+“@.元数据{0}={1},{2}@”+索引+kvPair.Key+kvPair.Value; } createButton(Blob.Snapshot Qualifieduri); //text1=text1+“@@@@”; Show(blob.snapshot限定duri.ToString()); } //text1=text1.Replace(“@”,System.Environment.NewLine); //label1.Text=text1; 按钮2.Enabled=true; } 但我不知道如何下载每个快照

我正在用create_按钮和btn_单击方法更新代码

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()); } } 私有void createButton(Uri uri1) { MessageBox.Show(“hhhhh是”); //此块动态创建一个按钮并将其添加到表单中 按钮btn=新按钮(); btn.Name=“btn1”; btn.位置=新点(3+i,14+x); btn.BackColor=System.Drawing.Color.White; btn.Text=uri1.ToString(); btn.宽度=370; btn.TextAlign=ContentAlignment.middleft; //将我们的按钮连接到我们的通用按钮处理程序 btn.Click+=新建事件处理程序(btn\u Click); this.Controls.Add(btn); //textBox1.Text=textBox1.Text+hai; //i+=20; x+=30; } 无效btn_单击(对象发送者,事件参数e) { StorageCredentials creds=新的StorageCredentials(accountName,accountKey); CloudStorageAccount=新的CloudStorageAccount(creds,useHttps:true); //Show(sender.ToString()); 乌里myUri; 字符串uri; var btn=发送方作为按钮; uri=btn.Text; 如果(btn!=null) { //MessageBox.Show(btn.Text); myUri=新Uri(btn.Text); Show(myUri.ToString()); } //创建blob客户端。 CloudBlobClient blobClient=account.CreateCloudBlobClient(); //检索对以前创建的容器的引用。 CloudBlobContainer容器=blobClient.GetContainerReference(“示例”); //CloudBlobContainer sampleContainer=client.GetContainerReference(“示例”); //检索对名为“photo1.jpg”的blob的引用。 string[]parts=uri.Split('/'); 字符串fileName=“”; 如果(零件长度>0) 文件名=零件[parts.Length-1]; 其他的 fileName=uri; //Show(文件名); CloudBlockBlob blockBlob=container.getBlockBlockBlobReference(文件名); //将blob内容保存到文件中。 尝试 { //使用(流输出)