Skip to content

Commit 4607949

Browse files
Dispose of streams that are created (#44510)
1 parent 335b888 commit 4607949

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed

sdk/storage/Azure.Storage.Blobs/src/BlobClient.cs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,13 +1042,18 @@ public virtual Response<BlobContentInfo> Upload(
10421042
public virtual Response<BlobContentInfo> Upload(
10431043
BinaryData content,
10441044
BlobUploadOptions options,
1045-
CancellationToken cancellationToken = default) =>
1046-
StagedUploadInternal(
1047-
content.ToStream(),
1048-
options,
1049-
async: false,
1050-
cancellationToken: cancellationToken)
1051-
.EnsureCompleted();
1045+
CancellationToken cancellationToken = default)
1046+
{
1047+
using (var stream = content.ToStream())
1048+
{
1049+
return StagedUploadInternal(
1050+
stream,
1051+
options,
1052+
async: false,
1053+
cancellationToken: cancellationToken)
1054+
.EnsureCompleted();
1055+
}
1056+
}
10521057

10531058
/// <summary>
10541059
/// The <see cref="Upload(Stream, BlobHttpHeaders, Metadata, BlobRequestConditions, IProgress{long}, AccessTier?, StorageTransferOptions, CancellationToken)"/>
@@ -1360,13 +1365,18 @@ await StagedUploadInternal(
13601365
public virtual async Task<Response<BlobContentInfo>> UploadAsync(
13611366
BinaryData content,
13621367
BlobUploadOptions options,
1363-
CancellationToken cancellationToken = default) =>
1364-
await StagedUploadInternal(
1365-
content.ToStream(),
1366-
options,
1367-
async: true,
1368-
cancellationToken: cancellationToken)
1369-
.ConfigureAwait(false);
1368+
CancellationToken cancellationToken = default)
1369+
{
1370+
using (var stream = content.ToStream())
1371+
{
1372+
return await StagedUploadInternal(
1373+
stream,
1374+
options,
1375+
async: true,
1376+
cancellationToken: cancellationToken)
1377+
.ConfigureAwait(false);
1378+
}
1379+
}
13701380

13711381
/// <summary>
13721382
/// The <see cref="UploadAsync(Stream, BlobHttpHeaders, Metadata, BlobRequestConditions, IProgress{long}, AccessTier?, StorageTransferOptions, CancellationToken)"/>

sdk/storage/Azure.Storage.Blobs/src/BlockBlobClient.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3238,14 +3238,18 @@ await client.StageBlockInternal(
32383238
LeaseId = args.Conditions.LeaseId
32393239
};
32403240
}
3241-
await client.StageBlockInternal(
3242-
Shared.StorageExtensions.GenerateBlockId(offset),
3243-
content.ToStream(),
3244-
validationOptions,
3245-
conditions,
3246-
progressHandler,
3247-
async,
3248-
cancellationToken).ConfigureAwait(false);
3241+
3242+
using (var stream = content.ToStream())
3243+
{
3244+
await client.StageBlockInternal(
3245+
Shared.StorageExtensions.GenerateBlockId(offset),
3246+
stream,
3247+
validationOptions,
3248+
conditions,
3249+
progressHandler,
3250+
async,
3251+
cancellationToken).ConfigureAwait(false);
3252+
}
32493253
},
32503254
CommitPartitionedUpload = async (partitions, args, async, cancellationToken)
32513255
=> await client.CommitBlockListInternal(

0 commit comments

Comments
 (0)