コード例 #1
0
        public async Task <IFile> GetFile(string directory, string fileName)
        {
            // Construct the URI. This will look like this:
            //   https://myaccount.blob.core.windows.net/resource
            String uri = $"https://{storageAccountName}.file.core.windows.net/{rootDirectory}/{directory}/{fileName}";

            // Set this to whatever payload you desire. Ours is null because
            //   we're not passing anything in.
            Byte[] responsePayload = null;

            //Instantiate the request message with a null payload.
            using (var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, uri))
            {
                // Add the request headers for x-ms-date and x-ms-version.
                DateTime now = DateTime.UtcNow;
                SetDefaultHeaders(httpRequestMessage, now);
                // If you need any additional headers, add them here before creating
                //   the authorization header.

                // Add the authorization header.
                httpRequestMessage.Headers.Authorization = AzureStorageAuthenticationHelper.GetAuthorizationHeader(
                    storageAccountName, storageAccountKey, now, httpRequestMessage);

                // Send the request.
                using (HttpResponseMessage httpResponseMessage = await new HttpClient().SendAsync(httpRequestMessage))
                {
                    // If successful (status code = 200),
                    //   parse the XML response for the container names.
                    if (httpResponseMessage.StatusCode == HttpStatusCode.OK)
                    {
                        var fileContent = await httpResponseMessage.Content.ReadAsByteArrayAsync();

                        var file = new File
                        {
                            Directory     = directory,
                            Name          = fileName,
                            ContentLength = fileContent.Length
                        };
                        file.AddDataContent(fileContent);
                        return(file);
                    }
                }
                return(null);
            }
        }