-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGcsWriteStream.cs
More file actions
190 lines (164 loc) · 5.09 KB
/
GcsWriteStream.cs
File metadata and controls
190 lines (164 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using System.Diagnostics.CodeAnalysis;
namespace Ramstack.FileSystem.Google;
/// <summary>
/// Represents a temporary write-only stream that buffers data to a temporary file before uploading it to Google Cloud Storage.
/// Data is committed to the storage bucket when the stream is disposed or closed.
/// </summary>
internal sealed class GcsWriteStream : Stream
{
private readonly GoogleFileSystem _fs;
private readonly string _objectName;
private readonly FileStream _stream;
private bool _disposed;
/// <inheritdoc />
public override bool CanRead => false;
/// <inheritdoc />
public override bool CanSeek => false;
/// <inheritdoc />
public override bool CanWrite => true;
/// <inheritdoc />
public override long Length
{
get
{
Error_NotSupported();
return 0;
}
}
/// <inheritdoc />
public override long Position
{
get
{
Error_NotSupported();
return 0;
}
// ReSharper disable once ValueParameterNotUsed
set => Error_NotSupported();
}
/// <summary>
/// Initializes a new instance of the <see cref="GcsWriteStream"/> class.
/// </summary>
/// <param name="fs">The <see cref="GoogleFileSystem"/> instance.</param>
/// <param name="objectName">The name of the object.</param>
public GcsWriteStream(GoogleFileSystem fs, string objectName)
{
_fs = fs;
_objectName = objectName;
_stream = new FileStream(
Path.Combine(
Path.GetTempPath(),
Path.GetRandomFileName()),
FileMode.CreateNew,
FileAccess.ReadWrite,
FileShare.None,
bufferSize: 4096,
FileOptions.DeleteOnClose
| FileOptions.Asynchronous);
}
/// <inheritdoc />
public override int Read(byte[] array, int offset, int count)
{
Error_NotSupported();
return 0;
}
/// <inheritdoc />
public override int Read(Span<byte> buffer)
{
Error_NotSupported();
return 0;
}
/// <inheritdoc />
public override void Write(byte[] buffer, int offset, int count) =>
Write(buffer.AsSpan(offset, count));
/// <inheritdoc />
public override void Write(ReadOnlySpan<byte> buffer)
{
try
{
_stream.Write(buffer);
}
catch
{
_disposed = true;
_stream.Close();
throw;
}
}
/// <inheritdoc />
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) =>
WriteAsync(buffer.AsMemory(offset, count), cancellationToken).AsTask();
/// <inheritdoc />
public override async ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default)
{
try
{
await _stream.WriteAsync(buffer, cancellationToken).ConfigureAwait(false);
}
catch
{
_disposed = true;
_stream.Close();
throw;
}
}
/// <inheritdoc />
public override long Seek(long offset, SeekOrigin origin)
{
Error_NotSupported();
return 0;
}
/// <inheritdoc />
public override void SetLength(long value) =>
Error_NotSupported();
/// <inheritdoc />
public override void Flush()
{
}
/// <inheritdoc />
public override Task FlushAsync(CancellationToken cancellationToken) =>
Task.CompletedTask;
/// <inheritdoc />
protected override void Dispose(bool disposing)
{
if (!_disposed && disposing)
{
try
{
_stream.Position = 0;
var destination = new global::Google.Apis.Storage.v1.Data.Object { Bucket = _fs.BucketName, Name = _objectName };
_fs.StorageClient.UploadObject(destination, _stream);
}
finally
{
_disposed = true;
_stream.Close();
base.Dispose(disposing);
}
}
}
/// <inheritdoc />
public override async ValueTask DisposeAsync()
{
if (!_disposed)
{
try
{
_stream.Position = 0;
var destination = new global::Google.Apis.Storage.v1.Data.Object { Bucket = _fs.BucketName, Name = _objectName };
await _fs.StorageClient
.UploadObjectAsync(destination, _stream)
.ConfigureAwait(false);
}
finally
{
_disposed = true;
await _stream.DisposeAsync().ConfigureAwait(false);
await base.DisposeAsync().ConfigureAwait(false);
}
}
}
[DoesNotReturn]
private static void Error_NotSupported() =>
throw new NotSupportedException();
}