/// <summary>
        /// Set analytics settings
        /// </summary>
        /// <param name="baseUri"></param>
        /// <param name="credentials"></param>
        /// <param name="settings"></param>
        /// <param name="useSharedKeyLite"></param>
        public static void SetSettings(Uri baseUri, StorageCredentials credentials, AnalyticsSettings settings, Boolean useSharedKeyLite)
        {
            UriBuilder builder = new UriBuilder(baseUri);

            builder.Query = string.Format(
                CultureInfo.InvariantCulture,
                "comp=properties&restype=service&timeout={0}",
                DefaultTimeout.TotalSeconds);

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(builder.Uri);

            request.Headers.Add(VersionHeaderName, Sep2009Version);
            request.Method = "PUT";

            StorageCredentialsAccountAndKey accountAndKey = credentials as StorageCredentialsAccountAndKey;

            using (MemoryStream buffer = new MemoryStream())
            {
                XmlTextWriter writer = new XmlTextWriter(buffer, Encoding.UTF8);
                SettingsSerializerHelper.SerializeAnalyticsSettings(writer, settings);
                writer.Flush();
                buffer.Seek(0, SeekOrigin.Begin);
                request.ContentLength = buffer.Length;

                if (useSharedKeyLite)
                {
                    credentials.SignRequestLite(request);
                }
                else
                {
                    credentials.SignRequest(request);
                }

                using (Stream stream = request.GetRequestStream())
                {
                    stream.Write(buffer.GetBuffer(), 0, (int)buffer.Length);
                }

                try
                {
                    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                    {
                        Console.WriteLine("Response Request Id = {0} Status={1}", response.Headers[RequestIdHeaderName], response.StatusCode);
                        if (HttpStatusCode.Accepted != response.StatusCode)
                        {
                            throw new Exception("Request failed with incorrect response status.");
                        }
                    }
                }
                catch (WebException e)
                {
                    Console.WriteLine(
                        "Response Request Id={0} Status={1}",
                        e.Response != null ? e.Response.Headers[RequestIdHeaderName] : "Response is null",
                        e.Status);
                    throw;
                }
            }
        }
        /// <summary>
        /// Get analytics settings
        /// </summary>
        /// <param name="baseUri"></param>
        /// <param name="credentials"></param>
        /// <param name="useSharedKeyLite"></param>
        /// <returns></returns>
        public static AnalyticsSettings GetSettings(Uri baseUri, StorageCredentials credentials, bool useSharedKeyLite)
        {
            UriBuilder builder = new UriBuilder(baseUri);

            builder.Query = string.Format(
                CultureInfo.InvariantCulture,
                "comp=properties&restype=service&timeout={0}",
                DefaultTimeout.TotalSeconds);

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(builder.Uri);

            request.Headers.Add(VersionHeaderName, Sep2009Version);
            request.Method = "GET";

            StorageCredentialsAccountAndKey accountAndKey = credentials as StorageCredentialsAccountAndKey;

            if (useSharedKeyLite)
            {
                credentials.SignRequestLite(request);
            }
            else
            {
                credentials.SignRequest(request);
            }

            try
            {
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    Console.WriteLine("Response Request Id={0} Status={1}", response.Headers[RequestIdHeaderName], response.StatusCode);

                    if (HttpStatusCode.OK != response.StatusCode)
                    {
                        throw new Exception("expected HttpStatusCode.OK");
                    }

                    using (Stream stream = response.GetResponseStream())
                    {
                        using (StreamReader streamReader = new StreamReader(stream))
                        {
                            string responseString = streamReader.ReadToEnd();
                            Console.WriteLine(responseString);

                            XmlReader reader = XmlReader.Create(new MemoryStream(ASCIIEncoding.UTF8.GetBytes(responseString)));
                            return(SettingsSerializerHelper.DeserializeAnalyticsSettings(reader));
                        }
                    }
                }
            }
            catch (WebException e)
            {
                Console.WriteLine(
                    "Response Request Id={0} Status={1}",
                    e.Response != null ? e.Response.Headers[RequestIdHeaderName] : "Response is null",
                    e.Status);
                throw;
            }
        }
Пример #3
0
        public byte[] DownloadChunk(string blobName, List <ChunkInfo> blobMetadata, int chunkIndex)
        {
            long offset = 0;
            int  size   = 0;

            foreach (ChunkInfo chunk in blobMetadata)
            {
                if (chunk.chunkIndex == chunkIndex)
                {
                    if (chunkCompressionType == CompressionType.None)
                    {
                        size = chunk.rsize;
                    }
                    else
                    {
                        size = chunk.csize;
                    }
                    break;
                }
                else
                {
                    if (chunkCompressionType == CompressionType.None)
                    {
                        offset += chunk.rsize;
                    }
                    else
                    {
                        offset += chunk.csize;
                    }
                }
            }


            byte[] buffer = new byte[size];
            try
            {
                CloudBlockBlob blockBlob      = GetBlockBlobReference(blobName, false);
                HttpWebRequest blobGetRequest = BlobRequest.Get(blockBlob.Uri, 60, null, null);

                // Add header to specify the range
                blobGetRequest.Headers.Add("x-ms-range", string.Format(System.Globalization.CultureInfo.InvariantCulture, "bytes={0}-{1}", offset, offset + size - 1));

                blobGetRequest.Headers.Add("x-ms-range-get-content-md5", "true"); // this tell azure to compute md5 of the byte range and return in the Content-MD5 header
                // Note: it only works for byte ranges that are <= 4MB. So will not work for chunk sizes more than that

                // Sign request.
                StorageCredentials credentials = blockBlob.ServiceClient.Credentials;
                credentials.SignRequest(blobGetRequest);


                // Read chunk.
                while (true)
                {
                    using (HttpWebResponse response = blobGetRequest.GetResponse() as
                                                      HttpWebResponse)
                    {
                        using (Stream stream = response.GetResponseStream())
                        {
                            int offsetInBuffer = 0;
                            int remaining      = size;
                            while (remaining > 0)
                            {
                                int read = stream.Read(buffer, offsetInBuffer, remaining);
                                offsetInBuffer += read;
                                remaining      -= read;
                            }
                        }

                        string contentMD5 = response.GetResponseHeader("Content-MD5");
                        if (!GetMD5FromStream(buffer).Equals(contentMD5, StringComparison.CurrentCultureIgnoreCase))
                        {
                            structuredLog("ER", "Content md5 does not match in download of chunk. retrying");
                        }
                        else
                        {
                            break;
                        }
                    }
                }

                if (logger != null)
                {
                    logger.Log("Start Synchronizer Decrypt Chunk");
                }
                Byte[] ret = Decrypt(buffer);
                if (logger != null)
                {
                    logger.Log("End Synchronizer Decrypt Chunk");
                }
                if (logger != null)
                {
                    logger.Log("Start Synchronizer Decompress Chunk");
                }
                ret = Decompress(ret);
                if (logger != null)
                {
                    logger.Log("End Synchronizer Decompress Chunk");
                }
                return(ret);
            }

            catch (Exception e)
            {
                structuredLog("E", " Exception in DownloadChunk: " + e);
                return(null);
            }
        }