我是从一本书上看到了Azure Blob,然后在网上浪了一会儿,发现了这篇文章,里面他已经把重点的则重地方讲完了,“飞机票:http://www.cnblogs.com/sparkdev/p/6441421.html#commentform”
但是对于我来说,一开始还是有点困惑的。里面的文章已经讲得很详细了,只是在做这个事情之前,你需要引入一个package(我为了找这个package也是煞费苦心,后来才发现前一章节有讲到。。。)
就是
Install-Package WindowsAzure.Storage
安装完成后大概就是这个样子喽
好像还有些别的东西。。嗯。。。不管了。
在上面那个地址中已经介绍了添加文件,删除文件啊一些基本操作,噢,对,千万别忘记下一个这个软件,
链接: http://pan.baidu.com/s/1c15dhfi 密码: rcar
原地址只有外网才可以访问的到,所以我放到了百度云里面,原作者应该不介意把。。。介意的话告诉我我去立马删除。。。
这个软件很好用的,而且so easy,点那个像插头一样的图标,然后就尽情的下一步吧~
我把原作者写的几个示例集合到了一个方法中(也许这个方法还有很多需要改进的地方。。)
我贴到这里来给大家用,这样我走的坑,大家就尽量别再掉进去了
public class BlobCommon
{
private CloudBlobContainer container; public BlobCommon(string connectionUrl,string containerName)
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionUrl);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
container = blobClient.GetContainerReference(containerName);
container.CreateIfNotExists();
} public BlobCommon()
{
try
{
var connectionUrl = ConfigurationManager.ConnectionStrings["BlobConnectionUrl"].ConnectionString;
var containerName = ConfigurationManager.AppSettings["BlobContainer"].ToString();
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionUrl);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
container = blobClient.GetContainerReference(containerName);
container.CreateIfNotExists();
}
catch (Exception ex)
{
} } public bool SaveImage(string imageName,string file)
{
try
{
CloudBlockBlob blockBlob = container.GetBlockBlobReference(imageName);
using (var fileStream = System.IO.File.OpenRead(file))
{
// 这是一个同步执行的方法
blockBlob.UploadFromStream(fileStream);
}
return true;
}
catch (Exception ex)
{
return false;
}
} public bool SaveImage(string imageName, byte[] imageData)
{
try
{
CloudBlockBlob blockBlob = container.GetBlockBlobReference(imageName);
blockBlob.UploadFromByteArray(imageData,, imageData.Length);
//using (var fileStream = System.IO.File.OpenRead(file))
//{
// // 这是一个同步执行的方法
// blockBlob.UploadFromStream(fileStream);
//}
return true;
}
catch (Exception ex)
{
return false;
}
} public List<SupervisorReportImageModel> GetBlobImageList()
{
try
{
var list = new List<SupervisorReportImageModel>();
foreach (IListBlobItem item in container.ListBlobs(null, false))
{
if (item.GetType() == typeof(CloudBlockBlob))
{
CloudBlockBlob blob = (CloudBlockBlob)item;
// todo something
list.Add(new SupervisorReportImageModel()
{
Name = blob.Name.ToString(),
Url = blob.SnapshotQualifiedStorageUri.SecondaryUri.ToString()
});
}
else if (item.GetType() == typeof(CloudAppendBlob))
{
CloudAppendBlob appendBlob = (CloudAppendBlob)item;
// todo something
list.Add(new SupervisorReportImageModel()
{
Name = appendBlob.Name.ToString(),
Url = appendBlob.SnapshotQualifiedStorageUri.SecondaryUri.ToString()
});
}
else if (item.GetType() == typeof(CloudPageBlob))
{
CloudPageBlob pageBlob = (CloudPageBlob)item;
// todo something
list.Add(new SupervisorReportImageModel()
{
Name = pageBlob.Name.ToString(),
Url = pageBlob.SnapshotQualifiedStorageUri.SecondaryUri.ToString()
});
}
else if (item.GetType() == typeof(CloudBlobDirectory))
{
CloudBlobDirectory directory = (CloudBlobDirectory)item;
// todo something
list.Add(new SupervisorReportImageModel()
{
Name = directory.Uri.ToString(),
Url = directory.StorageUri.SecondaryUri.ToString()
});
}
}
return list;
}
catch (Exception ex)
{
return null;
}
} public bool DownloadImage(string imageName,string saveUrl)
{
try
{
// 创建名称为 mypicture.png 的 Blob 对象的引用。
CloudBlockBlob blockBlob = container.GetBlockBlobReference(imageName);
// 把文件保存到本地。
using (var fileStream = System.IO.File.OpenWrite(saveUrl))
{
blockBlob.DownloadToStream(fileStream);
}
return true;
}
catch (Exception ex)
{
return false;
}
} public bool SetupBlobRole()
{
try
{
BlobContainerPermissions permissions = container.GetPermissions();
// Container 中的所有 Blob 都能被访问
permissions.PublicAccess = BlobContainerPublicAccessType.Container;
container.SetPermissions(permissions);
return true;
}
catch (Exception ex)
{
return false;
}
} public bool DeleteImage(string imageName)
{
try
{
CloudBlockBlob blockBlob = container.GetBlockBlobReference(imageName);
blockBlob.DeleteIfExists();
return true;
}
catch (Exception ex)
{
return false;
}
} public SupervisorReportImageModel GetBlobImageByImageName(string imageName)
{
try
{
List<IListBlobItem> imageData = container.ListBlobs(imageName, false).ToList();
foreach (var item in imageData)
{
if (item.GetType() == typeof(CloudBlockBlob))
{
CloudBlockBlob blob = (CloudBlockBlob)item;
// todo something
return new SupervisorReportImageModel()
{
Name = blob.Name.ToString(),
Url = blob.SnapshotQualifiedStorageUri.SecondaryUri.ToString()
};
}
else if (item.GetType() == typeof(CloudAppendBlob))
{
CloudAppendBlob appendBlob = (CloudAppendBlob)item;
// todo something
return new SupervisorReportImageModel()
{
Name = appendBlob.Name.ToString(),
Url = appendBlob.SnapshotQualifiedStorageUri.SecondaryUri.ToString()
};
}
else if (item.GetType() == typeof(CloudPageBlob))
{
CloudPageBlob pageBlob = (CloudPageBlob)item;
// todo something
return new SupervisorReportImageModel()
{
Name = pageBlob.Name.ToString(),
Url = pageBlob.SnapshotQualifiedStorageUri.SecondaryUri.ToString()
};
}
else if (item.GetType() == typeof(CloudBlobDirectory))
{
CloudBlobDirectory directory = (CloudBlobDirectory)item;
// todo something
return new SupervisorReportImageModel()
{
Name = directory.Uri.ToString(),
Url = directory.StorageUri.SecondaryUri.ToString()
};
}
}
return null;
}
catch (Exception ex)
{
return null;
}
}
}
----------------------------------------------------我是分割线-------------------------------------------------------
写完之后我不由得有一些困惑,我看到其中有一些方法可以创建文件夹啊什么的,可是暂时还不会用,还需要学习一下子。或者有老司机教教我,给我一个使用文档也可以啦~
我现在有一个最大的难题,就是文件权限的问题。在文档中,我看到了可以获取权限的方法,可是,如果我的图片,提供url出来之后,我怎么才能使用这个权限去下载图片呢。。。