제목 : 코드샘플: BLOB 스토리지 사용 관련 코드 모음(컨테이너, 업로드, 다운로드, 삭제, 리스트)
글번호:
|
|
11
|
작성자:
|
|
Administrator ( 레드플러스 / redplus@live.com )
|
작성일:
|
|
2013/01/16 오후 7:07:51
|
조회수:
|
|
5712
|
// 참고 URL : http://www.windowsazure.com/en-us/develop/net/how-to-guides/blob-storage/
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindowsAzureBlobStorageTest
{
class Program
{
private static string connectionString = "DefaultEndpointsProtocol=https;AccountName=[스토리지계정명];AccountKey=[액세스키]";
static void Main(string[] args)
{
// 스토리지 계정 -> 컨테이너 -> BLOB(파일)
//CreateContainer();
//UploadFile();
ListFiles();
//DownloadBlob();
//DeleteBlob();
//DownloadText();
}
private static void DownloadText()
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("redpluscontainer");
CloudBlockBlob blockBlob = container.GetBlockBlobReference("A.txt");
// 텍스트로 다운
string text = String.Empty;
using (var memoryStream = new MemoryStream())
{
blockBlob.DownloadToStream(memoryStream);
text = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
}
Console.WriteLine(text);
}
private static void DeleteBlob()
{
ListFiles();
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("redpluscontainer");
CloudBlockBlob blockBlob = container.GetBlockBlobReference("B.txt");
blockBlob.Delete(); // 삭제
ListFiles();
}
private static void DownloadBlob()
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("redpluscontainer");
CloudBlockBlob blockBlob = container.GetBlockBlobReference("B.txt");
// 파일로 다운
using (var fileStream = System.IO.File.OpenWrite(@"C:\Temp\Download\BB.txt"))
{
blockBlob.DownloadToStream(fileStream); // 다운로드
}
}
private static void ListFiles()
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("redpluscontainer");
foreach (IListBlobItem item in container.ListBlobs(null, false))
{
if (item.GetType() == typeof(CloudBlockBlob))
{
CloudBlockBlob blob = (CloudBlockBlob)item;
Console.WriteLine("블록 블랍 리스트 : {0} : {1}", blob.Properties.Length, blob.Uri);
}
else if (item.GetType() == typeof(CloudPageBlob))
{
CloudPageBlob page = (CloudPageBlob)item;
Console.WriteLine("페이지 블랍 리스트 : {0} : {1}", page.Properties.Length, page.Uri);
}
else if (item.GetType() == typeof(CloudBlobDirectory))
{
CloudBlobDirectory dir = (CloudBlobDirectory)item;
Console.WriteLine("디렉터리 : {0}", dir.Uri);
}
}
}
private static void UploadFile()
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("redpluscontainer");
CloudBlockBlob blockBlob = container.GetBlockBlobReference("B.txt");
using (var fileStream = System.IO.File.OpenRead(@"C:\Temp\Upload\B.txt"))
{
blockBlob.UploadFromStream(fileStream); // 업로드 진행
}
}
private static void CreateContainer()
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("redpluscontainer");
container.CreateIfNotExists(); // 컨테이너 생성
container.SetPermissions(
new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
}
}
}