Esempio n. 1
0
        public async Task<TBlob> GetBlobAsync<TBlob, TBlobDocument>(string name)
            where TBlob : class, IBlobModel<TBlobDocument>
            where TBlobDocument : class
        {
            var containerName = BlobContainerAttribute.GetContainerName<TBlobDocument>();
            var blobContainerClient = GetOrCreateBlobContainer(containerName);
            var blobClient = blobContainerClient.GetBlobClient(name);
            var exists = await blobClient.ExistsAsync();
            if (!exists) return default;

            var response = await blobClient.DownloadAsync();
            var download = response.Value;
            var reader = new StreamReader(download.Content);
            var jsonContent = await reader.ReadToEndAsync();
            var content = JsonConvert.DeserializeObject<TBlobDocument>(jsonContent);
            var metadata = download.Details.Metadata;

            var blobModel = new BlobModel<TBlobDocument>
            {
                Name = name,
                Content = content,
                Metadata = metadata
            };

            return blobModel as TBlob;
        }
Esempio n. 2
0
 public async Task SaveBlobAsync<TBlob, TBlobDocument>(TBlob blob)
     where TBlob : class, IBlobModel<TBlobDocument>
     where TBlobDocument : class
 {
     var containerName = BlobContainerAttribute.GetContainerName<TBlobDocument>();
     var blobContainerClient = GetOrCreateBlobContainer(containerName);
     var blobClient = blobContainerClient.GetBlobClient(blob.Name);
     var exists = await blobClient.ExistsAsync();
     if (exists)
     {
         throw new ArgumentException($"Blob {blob.Name} already exists!");
     }
     using (var stream = SerializeToStream(blob.Content))
     {
         await blobClient.UploadAsync(stream, metadata: blob.Metadata);
     }
 }