33namespace Ramstack . FileSystem . Google ;
44
55/// <summary>
6- /// Represents a temporary write-only stream for Google Cloud Storage operations, redirecting all write operations to a temporary file .
7- /// Upon disposing or closing the stream, the data is transferred to the Azure Blob storage .
6+ /// Represents a temporary write-only stream that buffers data to a temporary file before uploading it to Google Cloud Storage .
7+ /// Data is committed to the storage bucket when the stream is disposed or closed .
88/// </summary>
99internal sealed class GcsWriteStream : Stream
1010{
1111 private readonly GoogleFileSystem _fs ;
1212 private readonly string _objectName ;
13- private readonly FileStream _stream = CreateTempFileStream ( ) ;
13+ private readonly FileStream _stream ;
1414 private bool _disposed ;
1515
1616 /// <inheritdoc />
@@ -53,6 +53,16 @@ public GcsWriteStream(GoogleFileSystem fs, string objectName)
5353 {
5454 _fs = fs ;
5555 _objectName = objectName ;
56+ _stream = new FileStream (
57+ Path . Combine (
58+ Path . GetTempPath ( ) ,
59+ Path . GetRandomFileName ( ) ) ,
60+ FileMode . CreateNew ,
61+ FileAccess . ReadWrite ,
62+ FileShare . None ,
63+ bufferSize : 4096 ,
64+ FileOptions . DeleteOnClose
65+ | FileOptions . Asynchronous ) ;
5666 }
5767
5868 /// <inheritdoc />
@@ -84,7 +94,6 @@ public override void Write(ReadOnlySpan<byte> buffer)
8494 {
8595 _disposed = true ;
8696 _stream . Close ( ) ;
87-
8897 throw ;
8998 }
9099 }
@@ -104,7 +113,6 @@ public override async ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, Cancella
104113 {
105114 _disposed = true ;
106115 _stream . Close ( ) ;
107-
108116 throw ;
109117 }
110118 }
@@ -176,15 +184,6 @@ await _fs.StorageClient
176184 }
177185 }
178186
179- private static FileStream CreateTempFileStream ( )
180- {
181- const int BufferSize = 4096 ;
182- const FileOptions Options = FileOptions . DeleteOnClose | FileOptions . Asynchronous ;
183-
184- var path = Path . Combine ( Path . GetTempPath ( ) , Path . GetRandomFileName ( ) ) ;
185- return new FileStream ( path , FileMode . Create , FileAccess . ReadWrite , FileShare . Read , BufferSize , Options ) ;
186- }
187-
188187 [ DoesNotReturn ]
189188 private static void Error_NotSupported ( ) =>
190189 throw new NotSupportedException ( ) ;
0 commit comments