public async Task Set <T>(StringPath path, T item) { using (var memStream = new MemoryStream()) { using (var zipWriter = new GZipStream(memStream, CompressionLevel.Optimal, true)) using (var tw = new StreamWriter(zipWriter, Encoding.UTF8)) JsonExtensions.DefaultSerializer.Serialize(new JsonTextWriter(tw), item); HttpRequestMessage Request() { memStream.Seek(0, SeekOrigin.Begin); var fullPath = path.WithExtension(".json.gz"); var req = BlobUri(fullPath).Put().WithStreamContent(memStream).WithBlobHeaders(Storage); return(req); } try { await Policy .Handle <HttpRequestException>() .OrResult <HttpResponseMessage>(r => r.StatusCode.IsTransient()) .RetryAsync(3, (r, i) => i.ExponentialBackoff()) .ExecuteAsync(() => H.SendAsync(Request())); } catch (Exception ex) { throw new InvalidOperationException($"Unable to write to blob storage '{Request().RequestUri}'", ex); } } }
public async Task Set <T>(StringPath path, T item) { using (var memStream = new MemoryStream()) { using (var zipWriter = new GZipStream(memStream, CompressionLevel.Optimal, true)) using (var tw = new StreamWriter(zipWriter, Encoding.UTF8)) { JsonExtensions.DefaultSerializer.Serialize(new JsonTextWriter(tw), item); } memStream.Seek(0, SeekOrigin.Begin); var req = BlobUri(path.WithExtension(".json.gz")).Put().WithStreamContent(memStream).WithBlobHeaders(Storage); var res = await H.SendAsync(req); res.EnsureSuccessStatusCode(); } }
public async Task <T> Get <T>(StringPath path) where T : class { var req = BlobUri(path.WithExtension(".json.gz")).Get().WithBlobHeaders(Storage); var res = await H.SendAsync(req); if (res.StatusCode == HttpStatusCode.NotFound) { return(null); } res.EnsureSuccessStatusCode(); using (var stream = await res.Content.ReadAsStreamAsync()) using (var zr = new GZipStream(stream, CompressionMode.Decompress)) using (var tr = new StreamReader(zr, Encoding.UTF8)) { var jObject = await JObject.LoadAsync(new JsonTextReader(tr)); var r = jObject.ToObject <T>(JsonExtensions.DefaultSerializer); return(r); } }