【文章版权归作者和CNBLOG所有,转载请务必注明原始出处】

猫尾巴的狐狸

导航

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();
        }
    }
}

posted on 2010-05-03 10:33  GinoHuo  阅读(541)  评论(0编辑  收藏  举报