使用Azure Blob存储

时间:2024-04-23 04:12:46

可以通过多种方式来对Azure Blob进行操作。在此我们介绍通过VS的客户端及代码两种方式来操作Blob。

一、通过VS来操作Blob.

1.首先下载publish settings 文件:打开“https://manage.windowsazure.cn/publishsettings/index”,登陆China Azure,下载publish Settings文件到本地。

使用Azure Blob存储

2.      打开Visual Studio, 选择
View -> Server Explorer

使用Azure Blob存储

3.    在左侧面板,右键点击Windows Azure, 选择Manage
Subscriptions;

使用Azure Blob存储

4.       在Manage Microsoft Azure Subscriptions中点击Certificates(0)选项卡。 点击Import,把第一步下载的Publish settings文件导入

使用Azure Blob存储

5.展开Azure->Storage下的存储(我的存储实例为terryteststorage)
       使用Azure Blob存储

6.  点击存储实例下的Blobs即可查看此存储下有哪些Container及每个Container下存储的数据信息。

使用Azure Blob存储

7.   上传文件到指定的Container。先在Blobs上右击,然后选择Create Blob Container,然后弹出创建Container页面。

使用Azure Blob存储

使用Azure Blob存储

8.   输入Container名称”test1”,然后点击OK按钮。

使用Azure Blob存储

9.至此,test1已创建成功,test1中未有任何数据

使用Azure Blob存储

10.    上传文件到test1中。点击页面上方的Upload Blob按钮

使用Azure Blob存储

11.     在弹出的上传控件中选择要上传的文件后,单击OK按钮。

使用Azure Blob存储

12.    至此,选择的文件已上传到test1中。

使用Azure Blob存储

二、通过 Storage API来创建Container、显示Container及上传文件到指定的Container.

1. 创建AzureBlockStorage类型工程, 输入工程名称” DataBlobStorageExample”,点击OK.

使用Azure Blob存储

2.    具体代码实例如下:

static StorageCredentials credentials = new StorageCredentials("terryteststorage", "0HnFgkllVNf7m0dTBM/U7hbC96o/YUVH ==");

        static CloudStorageAccount storageAccount = new CloudStorageAccount(credentials,new Uri("https://terryteststorage.blob.core.chinacloudapi.cn/"),
            new Uri("https://terryteststorage.queue.core.chinacloudapi.cn/"),
            new Uri("https://terryteststorage.table.core.chinacloudapi.cn/"),null);         static void Main(string[] args)
        {
            
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();   
            //create container
            CloudBlobContainer container = blobClient.GetContainerReference("container1");
            container.CreateIfNotExists();             //List containers
            List<CloudBlobContainer> containers ;
            if (ListContainers(out containers))
            {              
                if (containers != null)
                {
                    foreach (CloudBlobContainer container2 in containers)
                    {
                        Console.Write(container2.Name + " ");
                    }
                    Console.WriteLine();
                }
            }         
                        //put file to blob
            PutBlob("container1", "blob1.txt", "This is a text blob!");   
}   public static bool ListContainers(out List<CloudBlobContainer> containerList)
        {
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            containerList = new List<CloudBlobContainer>();
            try
            {
                IEnumerable<CloudBlobContainer> containers = blobClient.ListContainers();
                if (containers != null)
                {
                    containerList.AddRange(containers);
                }
                return true;
            }
            catch (Exception ex)
            {
                               throw;
            }
        }   public static  bool PutBlob(string containerName, string blobName, string content)
        {
            try
            {
                CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
                CloudBlobContainer container = blobClient.GetContainerReference(containerName);
                ICloudBlob blob = container.GetBlockBlobReference(blobName);
                string filePath = @"D:\Terry\blocktest.txt";
                blob.UploadFromFile(filePath, FileMode.OpenOrCreate);
                return true;
            }
            catch (Exception ex)
            {                 Console.WriteLine("error in Putblock");
            }
        }

3. 运行结果

使用Azure Blob存储

使用Azure Blob存储