// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");
// Retrieve reference to a blob named "myblob.txt".
CloudBlockBlob blockBlob = container.GetBlockBlobReference("myblob.txt");
// Delete the blob.
blockBlob.Delete();
Example ตัวอย่างการเขียน ASP.Net เพื่อลบไฟล์ที่อยู่บน Blob
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;
using System.IO;
namespace myWebApp
{
public partial class Default : System.Web.UI.Page
{
public class Member
{
public string URL { set; get; }
public string Name { set; get; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BinData();
}
}
protected void BinData()
{
// Create the connectionstring
String StorageConnectionString = "DefaultEndpointsProtocol=https;AccountName=[yourAccount];AccountKey=[yourKey]";
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(StorageConnectionString);
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference("pictures");
// Create List
List<Member> ls = new List<Member>();
// Loop over items within the container and output the length and URI.
foreach (IListBlobItem item in container.ListBlobs(null, false))
{
if (item.GetType() == typeof(CloudBlockBlob))
{
CloudBlockBlob blob = (CloudBlockBlob)item;
ls.Add(new Member { URL = blob.Uri.ToString(), Name = blob.Name });
}
}
// Create data to GridView
this.myGridView.DataSource = ls;
this.myGridView.DataBind();
}
protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//*** Name ***//
Label lblName = (Label)(e.Row.FindControl("lblName"));
if (lblName != null)
{
lblName.Text = DataBinder.Eval(e.Row.DataItem, "NAME").ToString();
}
//*** View ***//
Image imgView = (Image)(e.Row.FindControl("imgView"));
if (imgView != null)
{
imgView.ImageUrl = DataBinder.Eval(e.Row.DataItem, "URL").ToString();
}
//*** Delete ***//
Button btnDelete = (Button)(e.Row.FindControl("btnDelete"));
if (btnDelete != null)
{
btnDelete.Attributes.Add("OnClick", "return confirm('Delete ?');");
}
}
}
protected void myGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
// Create the connectionstring
String StorageConnectionString = "DefaultEndpointsProtocol=https;AccountName=[yourAccount];AccountKey=[yourKey]";
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(StorageConnectionString);
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference("pictures");
//*** Name (Delete by Blob Name) ***//
Label lblName = (Label)myGridView.Rows[e.RowIndex].FindControl("lblName");
// Retrieve reference to a blob.
CloudBlockBlob blockBlob = container.GetBlockBlobReference(lblName.Text);
// Delete the blob.
blockBlob.Delete();
BinData();
}
}
}