Reputation: 425
Нi, everybody! Faced with the issue when trying to use local Microsoft Azure Storage for uploading files through Data Lake Service Client in c#.
As connection string to emulator used:
"UseDevelopmentStorage=true"
Code used:
DataLakeServiceClient _dataLakeClient = new DataLakeServiceClient(connectionString)
DataLakeFileSystemClient fileSystemClient _dataLakeClient.GetFileSystemClient(containerNameLowercase);
DataLakeDirectoryClient directoryClient = fileSystemClient.GetDirectoryClient(path);
DataLakeFileClient fileClient = await directoryClient.CreateFileAsync(name);
Problem occurs in : await directoryClient.CreateFileAsync(name)
Versions:
If you have any suggestions in how to handle it, feel free in your answers.
Updated: Required header is: x-ms-blob-type
.
So I faced with another problem, how to add that header to DataLakeFileClient header?
Thank you in advance
Upvotes: 1
Views: 2050
Reputation: 1448
You code looks correct, the problem is the missing header file in your request. As mentioned in this document, to upload a file to a directory first we need create a file reference in the target directory by creating an instance of the DataLakeFileClient class. Then upload a file by calling the DataLakeFileClient.AppendAsync method. And lastly we need to make sure to complete the upload by calling the DataLakeFileClient.FlushAsync method. As shown in the below.
public async Task UploadFile(DataLakeFileSystemClient fileSystemClient)
{
DataLakeDirectoryClient directoryClient = fileSystemClient.GetDirectoryClient("my-directory");
DataLakeFileClient fileClient = await directoryClient.CreateFileAsync("uploaded-file.txt");
FileStream fileStream = File.OpenRead("C:\\Users\\contoso\\Temp\\file-to-upload.txt");
long fileSize = fileStream.Length;
await fileClient.AppendAsync(fileStream, offset: 0);
await fileClient.FlushAsync(position: fileSize);
}
To solve this problem first we need to find out which Header is missing in the request as the error code says MissingRequiredHeader.
Try to use fiddler and look at the http Request and Response for more clear view and figure it out which header is missing and try adding the required headers from this page (like x-ms-blob-type for example).
I would also suggest you to specify explicit service endpoints in your connection string instead of using the default endpoints. For more information check this Create a connection string for an explicit storage endpoint section of MS Document.
Upvotes: 1