WindowsAzure.StorageClient API

Microsoft.WindowsAzure.StorageClient.dll 封装了对Blob,Table,Queue 的常见操作。这样不用直接用REST去coding了,方便不少。

image

 class Program
    {
        const string config = @"BlobEndpoint=http://
    
    
     
     ***
    
    .blob.core.windows.net/;QueueEndpoint=http://
    
    
     
     ***
    
    ..queue.core.windows.net/;TableEndpoint=http://
    
    
     
     ***
    
    ..table.core.windows.net/;AccountName=
    
    
     
     ***
    
    .;AccountKey=
    
    
     
     ***
    
    .";
        static void Main(string[] args)
        {
            QueueTest();
            Console.ReadKey();
        }

private static void BlobTest() 
        {
            CloudStorageAccount acct = CloudStorageAccount.Parse(config);
            CloudBlobClient blobclient = new CloudBlobClient(new Uri("http://
    
    
     
     ***
    
    ..blob.core.windows.net/")
                , acct.Credentials);
            var container = blobclient.GetContainerReference("testcontainer");
            container.CreateIfNotExist();
            var blob = container.GetBlobReference("TestFile");
            blob.UploadText("Hello World!");
            // Display
            var blobcontent = blob.DownloadText();
            Console.WriteLine(blobcontent);
            var blobs = container.ListBlobs();
            foreach (var item in blobs) 
            {
                Console.WriteLine(item.Uri.ToString());
            }
            // Delete 
            var succeed = blob.DeleteIfExists();
            Console.WriteLine(succeed.ToString());
            Console.ReadKey();
        }

        private static void TableTest() 
        {
            CloudStorageAccount acct = CloudStorageAccount.Parse(config);
            CloudTableClient tableclient = new CloudTableClient(acct.TableEndpoint.ToString()
                , acct.Credentials);
            string tableName = "users";
            tableclient.CreateTableIfNotExist(tableName);
            UserInfo user = new UserInfo { UserID = "10" };
            TableServiceContext context = tableclient.GetDataServiceContext();
            context.AddObject(tableName, user);
            context.SaveChanges();
            //query
            var query = context.CreateQuery<UserInfo>(tableName);
            foreach (var item in query.ToList<UserInfo>()) 
            {
                Console.WriteLine(item.RowKey + item.UserID + item.Timestamp.ToString());
            }
            //var result = query.ToList<UserInfo>();
        }

        private static void QueueTest() 
        {
            CloudStorageAccount acct = CloudStorageAccount.Parse(config);
            CloudQueueClient client = new CloudQueueClient(acct.QueueEndpoint, acct.Credentials);
            CloudQueue cq = client.GetQueueReference("testq");
            cq.CreateIfNotExist();
            
            //Receive
            Thread td = new Thread(new ThreadStart(() =>
            {
                Thread.Sleep(3000);
                CloudQueueMessage msg1 = cq.GetMessage();
                while (msg1 != null)
                {
                    Console.WriteLine(string.Format("{0} Receive:{1}", DateTime.Now, msg1.AsString));
                    cq.DeleteMessage(msg1);
                    msg1 = cq.GetMessage();
                    Thread.Sleep(3000);
                }
                Console.WriteLine("Receive Completed.");
            }));
            td.Start();

            //Send
            for (int i = 0; i < 10; i++)
            {
                CloudQueueMessage msg = new CloudQueueMessage(i.ToString());
                cq.AddMessage(msg);
                Console.WriteLine(string.Format("{0} Clinent add:{1}", DateTime.Now, i));
                Thread.Sleep(2000);
            }
            Console.WriteLine("Add Completed.");
           
        } 
    }

    public class UserInfo : TableServiceEntity 
    {
        public string UserID { get; set; }
        public UserInfo() 
        {
            PartitionKey = "UserInfo";
            RowKey = Guid.NewGuid().ToString();
        }
    }
}

转载于:https://www.cnblogs.com/ginohuo/archive/2010/05/03/1726257.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值