如何在Azure dll的Azure.Storage.Blobs v12.x.x中处理乐观并发

如何在Azure dll的Azure.Storage.Blobs v12.x.x中处理乐观并发,azure,blob,storage,Azure,Blob,Storage,我正在尝试实现学习路径中共享的示例 我正在尝试使用v12 dll,它是Azure.Storage.Blobs 这是我的密码 public static async Task Main() { BlobContainerClient container; try { container = new BlobServiceClient(connectio

我正在尝试实现学习路径中共享的示例

我正在尝试使用v12 dll,它是Azure.Storage.Blobs 这是我的密码

public static async Task Main()
        {
            
            BlobContainerClient container;
            try
            {
                container = new BlobServiceClient(connectionString).GetBlobContainerClient(containerName);
                await container.CreateIfNotExistsAsync(PublicAccessType.None);

            }
            catch (Exception)
            {
                var msg = $"Storage account not found. Ensure that the environment variable " +
                    " is set to a valid Azure Storage connection string and that the storage account exists.";
                Console.WriteLine(msg);
                return;
            }

            // First, the newsroom chief writes the story notes to the blob
            await SimulateChief();
            Console.WriteLine();

            await Task.Delay(TimeSpan.FromSeconds(2));

            // Next, two reporters begin work on the story at the same time, one starting soon after the other
            var reporterA = SimulateReporter("Reporter A", writingTime: TimeSpan.FromSeconds(12));
            await Task.Delay(TimeSpan.FromSeconds(4));
            var reporterB = SimulateReporter("Reporter B", writingTime: TimeSpan.FromSeconds(4));

            await Task.WhenAll(reporterA, reporterB);
            await Task.Delay(TimeSpan.FromSeconds(2));

            Console.WriteLine();
            Console.WriteLine("=============================================");
            Console.WriteLine();
            Console.WriteLine("Reporters have finished, here's the story saved to the blob:");

            BlobDownloadInfo story = await container.GetBlobClient(blobName).DownloadAsync();

            Console.WriteLine(new StreamReader(story.Content).ReadToEnd());
        }

        private static async Task SimulateReporter(string authorName, TimeSpan writingTime)
        {
            // First, the reporter retrieves the current contents
            Console.WriteLine($"{authorName} begins work");
            var blob = new BlobContainerClient(connectionString, containerName).GetBlobClient(blobName);

            var contents = await blob.DownloadAsync();

            Console.WriteLine($"{authorName} loads the file and sees the following content: \"{new StreamReader(contents.Value.Content).ReadToEnd()}\"");

            // Store the current ETag
            var properties = await blob.GetPropertiesAsync();
            var currentETag = properties.Value.ETag;
            Console.WriteLine($"\"{contents}\" has this ETag: {properties.Value.ETag}");

            // Next, the author writes their story. This takes some time.
            Console.WriteLine($"{authorName} begins writing their story...");
            await Task.Delay(writingTime);
            Console.WriteLine($"{authorName} has finished writing their story");

            try
            {
                // Finally, they save their story back to the blob.
                var story = $"[[{authorName.ToUpperInvariant()}'S STORY]]";
                await uploadDatatoBlob(blob, story);
                Console.WriteLine($"{authorName} has saved their story to Blob storage. New blob contents: \"{story}\"");
            }
            catch (RequestFailedException e)
            {
                // Catch error if the ETag has changed it's value since opening the file
                Console.WriteLine($"{authorName} sorry cannot save the file as server returned an error: {e.Message}");
            }
        }

        private static async Task SimulateChief()
        {
            var blob = new BlobContainerClient(connectionString, containerName).GetBlobClient(blobName);

            var notes = "[[CHIEF'S STORY NOTES]]";
            await uploadDatatoBlob(blob, notes);
            Console.WriteLine($"The newsroom chief has saved story notes to the blob {containerName}/{blobName}");
        }

        private static async Task uploadDatatoBlob(BlobClient blob, string notes)
        {
            byte[] byteArray = Encoding.UTF8.GetBytes(notes);
            MemoryStream stream = new MemoryStream(byteArray);
            await blob.UploadAsync(stream, overwrite: true);
        }
我需要在上传之前修改UploadAsync以检查ETag。 在旧版本的Azure.Net CLI中,我们有Microsoft.Azure.Storage.Blob dll,现在它通过

await blob.UploadTextAsync(story, null, accessCondition: AccessCondition.GenerateIfMatchCondition(currentETag), null, null);
如何在v12 dll中执行此操作。
感谢您的帮助。

请使用以下替代方法:


UploadAsync(Stream、BlobHttpHeaders、IDictionary、BlobRequestConditions、IProgress、Nullable参数。

你能分享一些例子吗?我仍在努力工作。请编辑你的问题并包含你编写的代码。我会看一看。我已经用详细代码更新了问题。请现在查看一次。我相信你需要通过一个add
uploadDatatoBlob
中etag的传统参数,然后使用此etag值设置
BlobRequestConditions
。考虑到编辑器也调用了相同的方法,您可能希望为etag传递一个空值,然后如果etag为空,则跳过设置BlobRequestConditions。尝试一下。它应该可以工作。
UploadAsync(Stream, BlobHttpHeaders, IDictionary<String,String>, BlobRequestConditions, IProgress<Int64>, Nullable<AccessTier>, StorageTransferOptions, CancellationToken)