// </Snippet_DownloadFromDirectory>

        #endregion

        #region Download a binary file from a directory

        // ---------------------------------------------------------
        // Download file from directory (binary)
        //----------------------------------------------------------

        // <Snippet_DownloadBinaryFromDirectory>
        public async Task DownloadFile(DataLakeFileSystemClient fileSystemClient)
        {
            DataLakeDirectoryClient directoryClient =
                fileSystemClient.GetDirectoryClient("my-directory");

            DataLakeFileClient fileClient =
                directoryClient.GetFileClient("my-image.png");

            Response <FileDownloadInfo> downloadResponse = await fileClient.ReadAsync();

            BinaryReader reader = new BinaryReader(downloadResponse.Value.Content);

            FileStream fileStream =
                File.OpenWrite("C:\\Users\\contoso\\my-image-downloaded.png");

            int bufferSize = 4096;

            byte[] buffer = new byte[bufferSize];

            int count;

            while ((count = reader.Read(buffer, 0, buffer.Length)) != 0)
            {
                fileStream.Write(buffer, 0, count);
            }

            await fileStream.FlushAsync();

            fileStream.Close();
        }
        public async Task ReadAsync()
        {
            // Create a temporary Lorem Ipsum file on disk that we can upload
            string originalPath = CreateTempFile(SampleFileContent);

            // Get a temporary path on disk where we can download the file
            string downloadPath = CreateTempPath();

            // Make StorageSharedKeyCredential to pass to the serviceClient
            string storageAccountName = StorageAccountName;
            string storageAccountKey  = StorageAccountKey;
            Uri    serviceUri         = StorageAccountBlobUri;
            StorageSharedKeyCredential sharedKeyCredential = new StorageSharedKeyCredential(storageAccountName, storageAccountKey);

            // Create DataLakeServiceClient using StorageSharedKeyCredentials
            DataLakeServiceClient serviceClient = new DataLakeServiceClient(serviceUri, sharedKeyCredential);

            // Get a reference to a filesystem named "sample-filesystem-readasync" and then create it
            DataLakeFileSystemClient filesystem = serviceClient.GetFileSystemClient(Randomize("sample-filesystem-read"));
            await filesystem.CreateAsync();

            try
            {
                // Get a reference to a file named "sample-file" in a filesystem
                DataLakeFileClient file = filesystem.GetFileClient(Randomize("sample-file"));

                // First upload something the DataLake file so we have something to download
                await file.CreateAsync();

                await file.AppendAsync(File.OpenRead(originalPath), 0);

                await file.FlushAsync(SampleFileContent.Length);

                // Download the DataLake file's contents and save it to a file
                Response <FileDownloadInfo> fileContents = await file.ReadAsync();

                using (FileStream stream = File.OpenWrite(downloadPath))
                {
                    fileContents.Value.Content.CopyTo(stream);
                }

                // Verify the contents
                Assert.AreEqual(SampleFileContent, File.ReadAllText(downloadPath));
            }
            finally
            {
                // Clean up after the test when we're finished
                await filesystem.DeleteAsync();
            }
        }
Пример #3
0
        public async Task <byte[]> Read(string path, CancellationToken token)
        {
            path.VerifyNotEmpty(nameof(path));

            DataLakeFileClient          file     = _fileSystem.GetFileClient(path);
            Response <FileDownloadInfo> response = await file.ReadAsync(token);

            _logger.LogTrace($"{nameof(Read)} from {path}");

            using MemoryStream memory = new MemoryStream();
            await response.Value.Content.CopyToAsync(memory);

            return(memory.ToArray());
        }
        public async Task <dynamic> DownloadJsonData(DataLakeFileSystemClient fileSystemClient, string directory, string filePath)
        {
            DataLakeDirectoryClient directoryClient =
                fileSystemClient.GetDirectoryClient(directory);

            DataLakeFileClient fileClient =
                directoryClient.GetFileClient(filePath);

            Response <FileDownloadInfo> downloadResponse = await fileClient.ReadAsync();

            var streamContent = new StreamContent(downloadResponse.Value.Content);
            var stringContent = await streamContent.ReadAsStringAsync();

            return(JsonConvert.DeserializeObject <dynamic>(stringContent));
        }
Пример #5
0
        public async Task <(byte[]? Data, ETag?Etag)> ReadWithTag(string path, CancellationToken token = default)
        {
            path = WithBasePath(path);
            _logger.LogTrace($"Reading {path}");

            try
            {
                DataLakeFileClient          file     = _fileSystem.GetFileClient(path);
                Response <FileDownloadInfo> response = await file.ReadAsync(token);

                using MemoryStream memory = new MemoryStream();
                await response.Value.Content.CopyToAsync(memory);

                return(memory.ToArray(), response.Value.Properties.ETag);
            }
            catch (RequestFailedException ex) when(ex.ErrorCode == "BlobNotFound")
            {
                return(null, null);
            }
        }
        // </Snippet_UploadFileBulk>

        #endregion

        #region Download a file from a directory

        // ---------------------------------------------------------
        // Download file from directory
        //----------------------------------------------------------

        // <Snippet_DownloadFromDirectory>
        public async Task DownloadFile2(DataLakeFileSystemClient fileSystemClient)
        {
            DataLakeDirectoryClient directoryClient =
                fileSystemClient.GetDirectoryClient("my-directory");

            DataLakeFileClient fileClient = directoryClient.GetFileClient("my-image.png");

            Response <FileDownloadInfo> downloadResponse = await fileClient.ReadAsync();

            StreamReader reader = new StreamReader(downloadResponse.Value.Content);

            FileStream fileStream = File.OpenWrite("C:\\Users\\contoso\\my-image-downloaded.png");

            string output = await reader.ReadToEndAsync();

            byte[] data = System.Text.Encoding.UTF8.GetBytes(output);

            fileStream.Write(data, 0, data.Length);

            await fileStream.FlushAsync();

            fileStream.Close();
        }
        public async Task DownloadFile(DataLakeFileSystemClient fileSystemClient, string keywordFilename, string outputFilePath)
        {
            DataLakeDirectoryClient     directoryClient  = fileSystemClient.GetDirectoryClient(stAccountDirectory);
            DataLakeFileClient          fileClient       = directoryClient.GetFileClient(keywordFilename);
            Response <FileDownloadInfo> downloadResponse = await fileClient.ReadAsync();

            BinaryReader reader     = new BinaryReader(downloadResponse.Value.Content);
            FileStream   fileStream = File.OpenWrite(outputFilePath);

            int bufferSize = 4096;

            byte[] buffer = new byte[bufferSize];
            int    count;

            while ((count = reader.Read(buffer, 0, buffer.Length)) != 0)
            {
                fileStream.Write(buffer, 0, count);
            }

            await fileStream.FlushAsync();

            fileStream.Close();
        }
Пример #8
0
        private static async Task DownloadAsync(string fileSystem, string directory, string fileName)
        {
            DataLakeFileSystemClient fileSystemClient = _dataLakeClient.GetFileSystemClient(fileSystem);

            DataLakeDirectoryClient directoryClient =
                fileSystemClient.GetDirectoryClient(directory);

            DataLakeFileClient fileClient =
                directoryClient.GetFileClient(fileName);

            Console.WriteLine("\nStarting download...");

            Response <FileDownloadInfo> downloadResponse = await fileClient.ReadAsync();

            var reader = new BinaryReader(downloadResponse.Value.Content);

            FileStream fileStream =
                File.OpenWrite($"{fileName}");

            const int bufferSize = 4096;

            var buffer = new byte[bufferSize];

            int count;

            while ((count = reader.Read(buffer, 0, buffer.Length)) != 0)
            {
                fileStream.Write(buffer, 0, count);
            }

            await fileStream.FlushAsync();

            fileStream.Close();

            Console.WriteLine($"File downloaded to {fileName}\n");
        }