- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 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());
}
我使用以下代码列出特定 Blob 的快照
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; }
但我不知道如何下载每个快照
我正在使用 create_button 和 btn_click 方法更新此代码
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() 函数调用每个快照并显示另一个表单,其中包含与快照对应的按钮列表,当我单击按钮时,我应该下载快照。但现在它引发了一些异常
最佳答案
实际上非常简单!一旦确定 blob 的 IsSnapshot
属性为 true,您就可以简单地调用该 blob 上的下载函数。请参阅此处的示例代码:
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);
}
}
}
}
您不需要获取 blob 的属性或元数据。显然上面的代码非常简单。如果一个 Blob 有多个快照,并且您希望单独保存每个快照,则需要在文件名中插入 Blob 的快照日期/时间。
关于c# - 如何在 Windows 窗体应用程序中使用 c Sharp 下载 azure blob 快照,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37987760/
对于一个科学实验,我写了一个turtle.py ,它会打开一个 800x480 的窗口并绘制一个缓慢增长的黑点。 turtle.py以 C:\Users\kaza>python C:\Users\ka
我开发了一个 swing 应用程序,但每次运行应用程序时都会打开一个新窗口。我希望如果一个窗口已经打开,则其他窗口不允许打开。 最佳答案 Here是一个 Java 单一应用实例的例子: A singl
有没有办法检测主进程中 Electron 的结构? process.platform 似乎也在 x64 机器上返回 win32,我没有在文档中找到任何获取架构的选项。 最佳答案 你试过 process
public short[] HanningWindow(short[] signal_in ,int pos ,int size) { for (int i= pos; i < pos+si
我有一个具有这些属性的 Electron 窗口: mainWindow = new BrowserWindow({ width: 800, height: 600, title: "Aqu
我有一个 Ubuntu 工作站,我正在尝试引导一个 Windows 节点。 Windows 节点在端口 2222 上打开了 ssh。我一直在关注 http://docs.opscode.com/plu
我是一名优秀的程序员,十分优秀!