Page 1 of 1

Azure Blob -Upload sorgen für vollständig

Posted: 23 May 2025, 00:30
by Anonymous
Ich lade eine Datei mit diesem Code in den Azure -Blob -Speicher hoch. Wie kann ich sicherstellen, dass der Upload abgeschlossen ist? Möglicherweise mit einem Status des Fortschritts. < /P>

Code: Select all

    public async Task PutBlobAsync(string containerName, string blobName, byte[] content, bool allowOverwrite = false, string contentType = null)
{
try
{

var containerClient = CreateAndGetContainer(containerName);
var blobClient = containerClient.GetBlobClient(blobName);

var blobUploadOptions = new BlobUploadOptions();

if (!String.IsNullOrEmpty(contentType))
blobUploadOptions.HttpHeaders = new BlobHttpHeaders { ContentType = contentType };

if (!allowOverwrite)
blobUploadOptions.Conditions = new BlobRequestConditions { IfNoneMatch = ETag.All };

using (var stream = new MemoryStream())
{
stream.Write(content, 0, content.Length);
stream.Position = 0;
await blobClient.UploadAsync(stream, blobUploadOptions);
}

return true;
}
catch (RequestFailedException e)
{
Logger.Error(e, "PutBlobAsync");

switch (e.Status)
{
case (int)HttpStatusCode.NotFound:
case (int)HttpStatusCode.PreconditionFailed:
return false;
}
throw;
}

}