Windows Azure Blob(2) - How to create a blob

 

1. Create a container

 

        static void Main(string[] args)
        {

            //Default HTTP REST interface, create a container named "storagesamplecontainer"
            Uri uri = new Uri("http://127.0.0.1:10000/devstoreaccount1/storagesamplecontainer?timeout=30");

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
            request.Timeout = 30000;
            request.ReadWriteTimeout = 30000;
            request.Method = "PUT";
            request.ContentLength = 0;
            request.Headers.Add("x-ms-date", DateTime.UtcNow.ToString("R", CultureInfo.InvariantCulture));
            request.Headers.Add("x-ms-meta-Name", "StorageSample");

           // Default account name and authentication key for local stimulated version

           //of Windows Azure  storage
            string accountName = "devstoreaccount1";
            string base64Key = "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==";
           

            // Add authentication data to HTTP header, you need to add a reference

            //of "StorageClient" project( Windows Azure SDK sample) to use classes

            //"ResourceUriComponents" and "SharedKeyCredentials" 

            byte[] key = Convert.FromBase64String(base64Key);
            ResourceUriComponents uriComponents = new ResourceUriComponents("devstoreaccount1", "storagesamplecontainer", null);

            SharedKeyCredentials credentials = new SharedKeyCredentials(accountName, key);
            credentials.SignRequest(request, uriComponents);
           

            // Send HTTP request and get response

            bool result = false;
            try
            {
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    if (response.StatusCode == HttpStatusCode.Created)
                        result = true;
                    else
                        result = false;
                    response.Close();
                }
            }
            catch (WebException we)
            {
                Console.WriteLine(we.Message);
            }

        }

 

2. Create a blob in "storagesamplecontainer" container

 

        static void Main(string[] args)
        {

            // Upload a file "hello.txt" to blob in "storagesamplecontainer" container

  string value = "helle world";
            Uri uri = new Uri("http://127.0.0.1:10000/devstoreaccount1/storagesamplecontainer/hello.txt?timeout=30");


            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
            request.Timeout = 30000;
            request.ReadWriteTimeout = 30000;
            request.Method = "PUT";
            request.Headers.Add("x-ms-date", DateTime.UtcNow.ToString("R", CultureInfo.InvariantCulture));
            request.Headers.Add("x-ms-meta-Name", "StorageSample");

            request.ContentType = "text/plain; charset=UTF-8";
            request.Headers.Add("x-ms-meta-m1", "v1");
            request.Headers.Add("x-ms-meta-m2", "v2");
            request.ContentLength = value.Length;

            string accountName = "devstoreaccount1";
            string base64Key = "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==";
            byte[] key = Convert.FromBase64String(base64Key);
            ResourceUriComponents uriComponents = new ResourceUriComponents("devstoreaccount1", "storagesamplecontainer", null);

            SharedKeyCredentials credentials = new SharedKeyCredentials(accountName, key);
            credentials.SignRequest(request, uriComponents);

            Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(value), false);

            bool retval = false;

            try
            {
                using (Stream requestStream = request.GetRequestStream())
                {
                    CopyStream(stream, requestStream, value.Length);
                    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                    {
                        if (response.StatusCode == HttpStatusCode.Created)
                        {
                            retval = true;
                        }
                        else
                        {
                            retval = false;
                        }

                        response.Close();
                    }
                    requestStream.Close();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.WriteLine("Create Blob:{0}", retval);
        }

       static void CopyStream(Stream sourceStream, Stream destinationStream, long length)
        {
            const int BufferSize = 0x10000;
            byte[] buffer = new byte[BufferSize];
            int n = 0;
            long amountLeft = length;          

            do
            {
                amountLeft -= n;
                n = sourceStream.Read(buffer, 0, (int)Math.Min(BufferSize, amountLeft));
                if (n > 0)
                {
                    destinationStream.Write(buffer, 0, n);
                }

            } while (n > 0);
        }

 

posted on 2009-01-18 21:08  Yang - Windows Azure  阅读(497)  评论(0编辑  收藏  举报

导航