Skip to content

Instantly share code, notes, and snippets.

@cool-mist
Created April 2, 2024 18:15
Show Gist options
  • Save cool-mist/54e64abe0d625cf86c68aa4bc4c14027 to your computer and use it in GitHub Desktop.
Save cool-mist/54e64abe0d625cf86c68aa4bc4c14027 to your computer and use it in GitHub Desktop.
A repro for how Flush operation fails in ADLS sdk when using non-standard transports
using Azure;
using Azure.Core.Pipeline;
using Azure.Identity;
using Azure.Storage.Files.DataLake;
using Azure.Storage.Files.DataLake.Models;
namespace adlspatch
{
internal class Program
{
private static async Task Main(string[] args)
{
HttpMessageHandler innerHandler = new HttpClientHandler();
if (args.Length > 0)
{
Console.WriteLine("Using WinHttpHandler");
innerHandler = new WinHttpHandler();
}
else
{
Console.WriteLine("Using HttpClientHandler");
}
string accountName = "sndeltest";
DataLakeServiceClient serviceClient = new(
new Uri($"https://{accountName}.dfs.core.windows.net"),
new DefaultAzureCredential(),
new DataLakeClientOptions
{
Transport = new HttpClientTransport(new HttpClient(new LoggingHandler(innerHandler)))
});
DataLakeFileSystemClient fsClient = serviceClient.GetFileSystemClient("test");
_ = await fsClient.CreateIfNotExistsAsync();
DataLakeDirectoryClient directoryClient = fsClient.GetDirectoryClient("test");
_ = await directoryClient.CreateIfNotExistsAsync();
DataLakeFileClient fileClient = directoryClient.GetFileClient("test.txt");
_ = await fileClient.DeleteIfExistsAsync();
_ = await fileClient.CreateAsync();
_ = await fileClient.AppendAsync(new MemoryStream([20, 20, 20]), 0);
_ = await fileClient.FlushAsync(3);
Console.WriteLine("Created file test.txt");
Response<PathProperties> properties = await fileClient.GetPropertiesAsync();
long contentLength = properties.Value.ContentLength;
Console.WriteLine($"File size: {contentLength} bytes");
}
}
public class LoggingHandler(HttpMessageHandler innerHandler) : DelegatingHandler(innerHandler)
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
Console.WriteLine("=========================================");
Console.WriteLine($"Request: {request.Method} {request.RequestUri}");
foreach (KeyValuePair<string, IEnumerable<string>> header in request.Headers)
{
if (header.Key == "Authorization")
{
continue;
}
Console.WriteLine($"{header.Key}: {string.Join(", ", header.Value)}");
}
Console.WriteLine("=========================================");
HttpResponseMessage response = await base.SendAsync(request, cancellationToken);
Console.WriteLine("=========================================");
Console.WriteLine($"Response: {response.StatusCode}");
Console.WriteLine("=========================================");
return response;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment