Hi Zhaoxing,
if you read my original posting, there is no exception generated and thus no stack trace to report. If you use the await appendBlob.AppendTextAsync method call the existing blob data gets overwritten (this is incorrect behaviour), but if you use the appendBlob.AppendText
method call the data gets appended to the existing blob (as expected).
Run the code below as a console app and it demonstrates what I am seeing. You should see these results below, which shows the append async method has overwritten, not appended:
Append blob of name appendblob, length 200, contents: Appended Text No 0, Appended Text No 1, Appended Text No 2, Appended Text No 3, Appended Text No 4, Appended Text No 5, Appended Text No 6, Appended Text No 7, Appended Text No 8, Appended Text No 9,
Append blob of name appendblobasync, length 26, contents: Appended Async Text No 9,
Many Thanks,
Roger
private static void Main(string[] args)
{
const string appendAsyncBlobName = "appendblobasync";
const string appendBlobName = "appendblob";
var connString = "Add your storage connection string here";
var storageAccount = CloudStorageAccount.Parse(connString);
var blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve a reference to a container.
var blockContainer = blobClient.GetContainerReference("appendblobtest");
// Create the container if it doesn't already exist.
blockContainer.CreateIfNotExists();
// append data to the blob asynchronously
AppendTextAsync(blockContainer, appendAsyncBlobName).Wait();
// now do the same for the same but use the non-async method
AppendTextNonAsync(blockContainer, appendBlobName);
DisplayContainerContents(blockContainer);
Console.ReadKey();
}
/// <summary>
/// Appends the text asynchronously
/// </summary>
/// <param name="blockContainer"></param>
/// <param name="appendAsyncBlobName"></param>
/// <returns></returns>
private static async Task AppendTextAsync(CloudBlobContainer blockContainer, string appendAsyncBlobName)
{
var appendAsyncBlob = blockContainer.GetAppendBlobReference(appendAsyncBlobName);
// delete the append blob
appendAsyncBlob.CreateOrReplace();
const int loopCount = 10;
for (int i = 0; i < loopCount; i++)
{
await appendAsyncBlob.AppendTextAsync($"Appended Async Text No {i}, ");
}
}
/// <summary>
/// Appends the text non-asynchronously
/// </summary>
/// <param name="blockContainer"></param>
/// <param name="appendBlobName"></param>
/// <returns></returns>
private static void AppendTextNonAsync(CloudBlobContainer blockContainer, string appendBlobName)
{
var appendBlob = blockContainer.GetAppendBlobReference(appendBlobName);
// delete the append blob
appendBlob.CreateOrReplace();
const int loopCount = 10;
for (int i = 0; i < loopCount; i++)
{
appendBlob.AppendText($"Appended Text No {i}, ");
}
}
/// <summary>
/// Displays the contest of the blobs in the container
/// </summary>
/// <param name="container"></param>
private static void DisplayContainerContents(CloudBlobContainer container)
{
// Loop over items within the container and output the length and URI.
foreach (IListBlobItem item in container.ListBlobs(null, true))
{
if (item is CloudAppendBlob)
{
var blob = (CloudAppendBlob) item;
Console.WriteLine(
$"Append blob of name {blob.Name}, length {blob.Properties.Length}, contents: {blob.DownloadText()}");
}
else if (item is CloudBlockBlob)
{
Console.WriteLine("This is a block blob");
}
else if (item is CloudPageBlob)
{
Console.WriteLine("This is a page blob");
}
else if (item is CloudBlobDirectory)
{
Console.WriteLine("This is a blob directory");
}
}
}