Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public async Task<Stream> OpenWriteAsync(bool overwrite, BlobOpenWriteOptions? o

using var blob = AcquireBlob(cancellationToken);

if (!blob.Value.TryOpenWrite(options?.OpenConditions, options?.BufferSize, options?.Metadata, out var stream, out var error))
if (!blob.Value.TryOpenWrite(options?.OpenConditions, options?.BufferSize, options?.Metadata, options?.HttpHeaders, out var stream, out var error))
{
throw error.GetClientException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,13 @@ public bool TryGetBlockList(
return true;
}

public bool TryOpenWrite(RequestConditions? conditions, long? bufferSize, IDictionary<string, string>? metadata, [NotNullWhen(true)] out BlobWriteStream? stream, [NotNullWhen(false)] out OpenWriteError? error)
public bool TryOpenWrite(
RequestConditions? conditions,
long? bufferSize,
IDictionary<string, string>? metadata,
BlobHttpHeaders? httpHeaders,
[NotNullWhen(true)] out BlobWriteStream? stream,
[NotNullWhen(false)] out OpenWriteError? error)
{
if (ShouldThrowBlobAlreadyExistsError(conditions))
{
Expand All @@ -271,7 +277,7 @@ public bool TryOpenWrite(RequestConditions? conditions, long? bufferSize, IDicti
return false;
}

SetCommitedState(null, metadata, []);
SetCommitedState(httpHeaders, metadata, []);

var client = InMemoryBlockBlobClient.FromAccount(Container.Service.Account, ContainerName, Name);

Expand Down
47 changes: 46 additions & 1 deletion tests/Tests/Storage/Blobs/BlobClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,39 @@ public void OpenWrite_Should_Create_Blob_With_Content(BlobClientType clientType)
ShouldHaveBlocks(containerClient.GetBlockBlobClient(blobName), commited: 1, uncommited: 0);
}

[TestMethod]
[TestCategory(TestCategory.AzureInfra)]
[DataRow(BlobClientType.Generic)]
[DataRow(BlobClientType.Block)]
public void OpenWrite_Should_Create_Blob_With_Headers(BlobClientType clientType)
{
var containerClient = ImplementationProvider.GetBlobContainerClient();

containerClient.CreateIfNotExists();

var blobName = Guid.NewGuid().ToString();

var blobClient = containerClient.GetBlobBaseClient(blobName, clientType);

var headers = new BlobHttpHeaders
{
ContentType = "application/json",
ContentEncoding = "br"
};

using (var stream = OpenWrite(blobClient, true, headers: headers))
using (var streamWriter = new StreamWriter(stream))
{
streamWriter.Write("test-data");
}

blobClient.DownloadContent().Value.Content.ToString().Should().Be("test-data");
blobClient.GetProperties().Value.ContentType.Should().Be("application/json");
blobClient.GetProperties().Value.ContentEncoding.Should().Be("br");

ShouldHaveBlocks(containerClient.GetBlockBlobClient(blobName), commited: 1, uncommited: 0);
}

[TestMethod]
[TestCategory(TestCategory.AzureInfra)]
[DataRow(BlobClientType.Generic)]
Expand Down Expand Up @@ -812,7 +845,7 @@ private static void Upload(BlobBaseClient blobClient, byte[] content, BlobUpload
}
}

private static Stream OpenWrite(BlobBaseClient blobClient, bool overwrite, IDictionary<string, string>? metadata = null, BlobRequestConditions? conditions = null)
private static Stream OpenWrite(BlobBaseClient blobClient, bool overwrite, IDictionary<string, string>? metadata = null, BlobHttpHeaders? headers = null, BlobRequestConditions? conditions = null)
{
if (blobClient is BlobClient genericClient)
{
Expand All @@ -830,6 +863,12 @@ private static Stream OpenWrite(BlobBaseClient blobClient, bool overwrite, IDict
options.OpenConditions = conditions;
}

if (headers is not null)
{
options ??= new();
options.HttpHeaders = headers;
}

return genericClient.OpenWrite(overwrite, options);
}

Expand All @@ -849,6 +888,12 @@ private static Stream OpenWrite(BlobBaseClient blobClient, bool overwrite, IDict
options.OpenConditions = conditions;
}

if (headers is not null)
{
options ??= new();
options.HttpHeaders = headers;
}

return blockClient.OpenWrite(overwrite, options);
}

Expand Down
Loading