Skip to content

409 When overwriting existing Blobs with BlobContainerClient #43

Description

@Appli4Ever

I have noticed that this method:

public override async Task WriteFileAsync(string virtualPath, Stream contents, bool overwrite = false, CancellationToken cancellationToken = default)
{
    if (!overwrite && await FileExistsAsync(virtualPath, cancellationToken))
    {
        throw new FileExistsException(GetPath(virtualPath), Prefix);
    }

    var path = GetPath(virtualPath);

    try
    {
        contents.Seek(0, SeekOrigin.Begin);

        await client.UploadBlobAsync(path, contents, cancellationToken);
    }
    catch (Exception exception)
    {
        throw Exception(exception);
    }
}

Always throws an Exception if the Blob already Exists. Even though overwrite is true.

Azure Blob Storage suggests not to use the BlobContainerClient.UploadBlobAsync() method when trying to overwrite an existing Blob:
suggestion

Insted something like this works:

    public override async Task WriteFileAsync(
        string virtualPath,
        Stream contents,
        bool overwrite = false,
        CancellationToken cancellationToken = new CancellationToken())
    {
        var path = this.GetPath(virtualPath);
        var blobClient = this.containerClient.GetBlobClient(path);

        if (!overwrite && await blobClient.ExistsAsync(cancellationToken))
        {
            throw new FileExistsException(this.GetPath(virtualPath), this.Prefix);
        }

        try
        {
            contents.Seek(0, SeekOrigin.Begin);

            await blobClient.UploadAsync(contents, overwrite: overwrite, cancellationToken);
        }
        catch (Exception exception)
        {
            throw this.Exception(exception);
        }
    }

Calling the Upload method on the BlobClient.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions