using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure; // Namespace
for CloudConfigurationManager
using Microsoft.WindowsAzure.Storage; // Namespace
for CloudStorageAccount
using Microsoft.WindowsAzure.Storage.Blob;
using System.IO; // Namespace for Blob storage types
namespace LabAzureStorageBlob
{
class Program
{
public static List<string>
GetContainers()
{
List<string> blobs = new List<string>();
//
Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
//
Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
//Get
the list of the blob from the above container
IEnumerable<CloudBlobContainer> containers =
blobClient.ListContainers();
foreach
(CloudBlobContainer item in containers)
{
blobs.Add(item.Uri.AbsoluteUri);
}
return blobs;
}
/// <summary>
/// El nombre
debe tener solo minusculas
/// </summary>
/// <param
name="containerName"></param>
/// <param
name="IsPublic"></param>
public static void
CreateContainer(string containerName,bool IsPublic)
{
//
Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
//
Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
//
Retrieve a reference to a container.
CloudBlobContainer container =
blobClient.GetContainerReference(containerName);
//
Create the container if it doesn't already exist.
container.CreateIfNotExists();
if (IsPublic)
{
container.SetPermissions(new
BlobContainerPermissions { PublicAccess =
BlobContainerPublicAccessType.Container });
}
}
/// <summary>
///
/// </summary>
/// <param
name="disk">Ejemplo: d: </param>
/// <param
name="fileNameWithExtension"> File.txt </param>
/// <param
name="containerName"></param>
/// <param
name="IsPublic">Aunque el Blob sea Privado, si el Container es publico, el permiso se
sobreescribe</param>
public static void UploadBlob(string disk, string fileNameWithExtension, string
containerName, bool IsPublic)
{
//
Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
//
Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
//
Retrieve reference to a previously created container.
CloudBlobContainer container =
blobClient.GetContainerReference(containerName);
//
Retrieve reference to a blob named "myblob".
CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileNameWithExtension);
//
Create or overwrite the "myblob" blob with contents from a local
file.
using (var fileStream
= System.IO.File.OpenRead("" + disk + "//" +
fileNameWithExtension + ""))
{
blockBlob.UploadFromStream(fileStream);
}
if (IsPublic)
{
container.SetPermissions(new
BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob
});
}
}
public static void DeleteBlob(string
containerName, string blobName)
{
//
Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
//
Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
//
Retrieve reference to a previously created container.
CloudBlobContainer container =
blobClient.GetContainerReference(containerName);
//
Retrieve reference to a blob named "myblob.txt".
CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName);
//
Delete the blob.
blockBlob.Delete();
}
public static List<string>
GetBlobs(string containerName)
{
List<string> blobList = new List<string>();
//
Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
//
Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
//
Retrieve reference to a previously created container.
CloudBlobContainer container =
blobClient.GetContainerReference(containerName);
//
Loop over items within the container and output the length and URI.
foreach
(IListBlobItem item in container.ListBlobs(null, false))
{
if (item.GetType() == typeof(CloudBlockBlob))
{
CloudBlockBlob blob = (CloudBlockBlob)item;
blobList.Add(blob.Uri.AbsoluteUri);
}
else if (item.GetType() == typeof(CloudPageBlob))
{
CloudPageBlob pageBlob =
(CloudPageBlob)item;
blobList.Add(pageBlob.Uri.AbsoluteUri);
}
else if (item.GetType() == typeof(CloudBlobDirectory))
{
CloudBlobDirectory
directory = (CloudBlobDirectory)item;
blobList.Add(directory.Uri.AbsoluteUri);
}
}
return blobList;
}
public static void
DownloadBlob(string disk, string fileNameWithExtension, string containerName)
{
//
Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
//
Retrieve reference to a previously created container.
CloudBlobContainer container =
blobClient.GetContainerReference(containerName);
//
Retrieve reference to a blob named "photo1.jpg".
CloudBlockBlob blockBlob =
container.GetBlockBlobReference(fileNameWithExtension);
//
Save blob contents to a file.
using (var fileStream
= System.IO.File.OpenWrite("" + disk + "//" +
fileNameWithExtension + ""))
{
blockBlob.DownloadToStream(fileStream);
}
}
public static byte[]
DownloadBlob(string fileNameWithExtension, string containerName)
{
//
Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
//
Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
//
Retrieve reference to a previously created container.
CloudBlobContainer container =
blobClient.GetContainerReference(containerName);
//
Retrieve reference to a blob named "myblob.txt"
CloudBlockBlob blockBlob2 =
container.GetBlockBlobReference(fileNameWithExtension);
using (var
memoryStream = new MemoryStream())
{
blockBlob2.DownloadToStream(memoryStream);
return (memoryStream.ToArray());
}
}
public static void
DeleteContainer(string containerName)
{
try
{
// Retrieve storage account from
connection string.
CloudStorageAccount
storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
// Create the blob client.
CloudBlobClient blobClient =
storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously
created container.
CloudBlobContainer container =
blobClient.GetContainerReference(containerName);
container.FetchAttributes();
container.Delete();
}
catch (Exception)
{
//existe excepción cuando se trata de
eliminar un container que no existe
//no es necesario enviar la excepción
}
}
static void Main(string[] args)
{
}
}
}