Skip to content

Instantly share code, notes, and snippets.

@icebeam7
Created December 3, 2018 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icebeam7/69b61ba57d079771a167b3c858d162d4 to your computer and use it in GitHub Desktop.
Save icebeam7/69b61ba57d079771a167b3c858d162d4 to your computer and use it in GitHub Desktop.
OnlineAlbumMobileApp: BlobStorageService.cs
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using OnlineAlbumMobileApp.Helpers;
using OnlineAlbumMobileApp.Models;
namespace OnlineAlbumMobileApp.Services
{
public static class BlobStorageService
{
readonly static CloudStorageAccount cloudStorageAccount =
CloudStorageAccount.Parse(Constants.StorageAccountConnectionString);
readonly static CloudBlobClient blobClient =
cloudStorageAccount.CreateCloudBlobClient();
readonly static CloudBlobContainer blobContainer =
blobClient.GetContainerReference(Constants.ContainerName);
public static async Task<List<OnlineImage>> GetBlobList()
{
await blobContainer.CreateIfNotExistsAsync(BlobContainerPublicAccessType.Container, null, null);
BlobContinuationToken token = null;
List<OnlineImage> blobList = new List<OnlineImage>();
if (blobContainer != null)
{
do
{
var results = await blobContainer.ListBlobsSegmentedAsync(null, token);
token = results.ContinuationToken;
foreach (IListBlobItem item in results.Results)
{
blobList.Add(new OnlineImage()
{
ImageURL = item.Uri.AbsoluteUri,
FileName = item.Uri.Segments.Last()
});
}
} while (token != null);
}
return blobList;
}
public static async Task<string> UploadBlob(Stream file, string extension)
{
await blobContainer.CreateIfNotExistsAsync(BlobContainerPublicAccessType.Container, null, null);
if (blobContainer != null)
{
var fileName = $"{Guid.NewGuid()}{extension}";
var blob = blobContainer.GetBlockBlobReference(fileName);
await blob.UploadFromStreamAsync(file);
return blob.Uri.AbsoluteUri;
}
return string.Empty;
}
public static async Task<bool> DeleteBlob(string fileName)
{
await blobContainer.CreateIfNotExistsAsync(BlobContainerPublicAccessType.Container, null, null);
if (blobContainer != null)
{
var blob = blobContainer.GetBlockBlobReference(fileName);
return await blob.DeleteIfExistsAsync();
}
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment