Since Microsoft.Azure.Storage.File is deprecated we are trying to upgrade to Azure.Storage.Files.Shares.
We were able to port our existing code to read files from the file share, but we are not being able to save changes to an existing file.
Using "Microsoft.Azure.Storage.File" we'd pass a stream to CloudFile.UploadFromStreamAsync and it would work as expected.
In "Azure.Storage.Files.Shares" we've tried to use both ShareFileClient.OpenWriteAsync and ShareFileClient.UploadAsync with no succes.
//This seems to empty the file
public void SaveStream(Stream stream)
{
var openOptions = new ShareFileOpenWriteOptions() { MaxSize = stream.Length + 1 };
using (var fileStream = File.OpenWrite(true, 0, openOptions))
{
stream.CopyToAsync(fileStream);
}
}
//This corrupts the file (it seems like it appends to it)
public void SaveStream(Stream stream)
{
File.UploadAsync(stream);
}
stream is a MemoryStream, File is an instance of ShareFileClient. We're on .NET Standard 2.0.
Anyone with more experience with this, that could shine some more light on the subject?
Please try by changing the following code:
var openOptions = new ShareFileOpenWriteOptions() { MaxSize = stream.Length + 1 };
with
var openOptions = new ShareFileOpenWriteOptions() { MaxSize = stream.Length };
With this change I was able to update a file. Also, please ensure that before using, the position of your source stream is set at 0
i.e. stream is positioned at the beginning.
Here's the complete code I used:
using System;
using System.IO;
using System.Text;
using Azure.Storage.Files.Shares;
using Azure.Storage.Files.Shares.Models;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string connectionString =
"DefaultEndpointsProtocol=https;AccountName=account-name;AccountKey=account-key";
string shareName = "share-name";
ShareClient shareClient = new ShareClient(connectionString, shareName);
ShareDirectoryClient directoryClient = shareClient.GetRootDirectoryClient();
ShareFileClient fileClient = directoryClient.GetFileClient("sample.text");
string updatedContent = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus pulvinar auctor vehicula. Proin vitae ante risus. Quisque fringilla orci eros, nec fermentum ipsum blandit et. Curabitur imperdiet tristique magna non vehicula. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Sed efficitur magna nisl, vitae venenatis leo semper nec. Nulla consequat lorem sapien, sed aliquam lectus dictum non. Morbi ac pulvinar justo, sit amet cursus turpis. In dictum odio non tellus aliquam viverra. Nunc vel vestibulum nulla. Ut mollis ultrices dignissim. Donec tellus nibh, bibendum suscipit felis sed, elementum auctor est. Donec ex nibh, pellentesque vitae odio ut, ornare pulvinar odio.";
using MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(updatedContent));
var openOptions = new ShareFileOpenWriteOptions() { MaxSize = ms.Length };
using (var fs = fileClient.OpenWrite(true, 0, openOptions))
{
ms.CopyTo(fs);
}
}
}
}