Last active
June 24, 2022 08:24
-
-
Save ichiroku11/1ca5318bf193c6b9d6262636f67a43b0 to your computer and use it in GitHub Desktop.
Azure Blobのサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Microsoft.WindowsAzure.Storage; | |
using Microsoft.WindowsAzure.Storage.Blob; | |
namespace ConsoleApp { | |
// Blobサンプル | |
class BlobSample { | |
// ストレージアカウント | |
private readonly CloudStorageAccount _account; | |
public BlobSample(CloudStorageAccount account) { | |
_account = account; | |
} | |
// サンプルを実行 | |
public async Task<bool> RunAsync() { | |
// Blobクライアントを作成 | |
var client = _account.CreateCloudBlobClient(); | |
// Blobコンテナ"sample"の参照を取得 | |
var container = client.GetContainerReference("sample"); | |
// Blobコンテナが存在していなかったら作成 | |
await container.CreateIfNotExistsAsync(); | |
// Blobを追加 | |
{ | |
// "file1.txt"を作成 | |
var blob1 = container.GetBlockBlobReference("file1.txt"); | |
await blob1.UploadTextAsync("file1 content"); | |
// "file2.txt"を作成 | |
var blob2 = container.GetBlockBlobReference("file2.txt"); | |
await blob2.UploadTextAsync("file2 content"); | |
} | |
// Blobを取得 | |
{ | |
// "file1.txt"を取得 | |
var blob1 = container.GetBlockBlobReference("file1.txt"); | |
Console.WriteLine($"file1.txt content: {await blob1.DownloadTextAsync()}"); | |
// file1.txt content: file1 content | |
// "file2.txt"を取得 | |
var blob2 = container.GetBlockBlobReference("file2.txt"); | |
Console.WriteLine($"file2.txt content: {await blob2.DownloadTextAsync()}"); | |
// file2.txt content: file2 content | |
} | |
// Blob一覧を取得 | |
var token = default(BlobContinuationToken); | |
do { | |
// Blob一覧を取得 | |
var segment = await container.ListBlobsSegmentedAsync(token); | |
token = segment.ContinuationToken; | |
// Blobの名前と中身をConsoleに表示 | |
foreach (var blob in segment.Results.OfType<CloudBlockBlob>()) { | |
Console.WriteLine($"name: {blob.Name}"); | |
Console.WriteLine($"content: {await blob.DownloadTextAsync()}"); | |
} | |
// name: file1.txt | |
// content: file1 content | |
// name: file2.txt | |
// content: file2 content | |
} while (token != null); | |
// Blobを更新 | |
{ | |
// "file1.txt"を取得 | |
var blob1 = container.GetBlockBlobReference("file1.txt"); | |
Console.WriteLine($"file1.txt content: {await blob1.DownloadTextAsync()}"); | |
// file1.txt content: file1 content | |
// "file1.txt"を更新 | |
await blob1.UploadTextAsync("file1 content updated"); | |
// "file1.txt"を再取得 | |
Console.WriteLine($"file1.txt content: {await blob1.DownloadTextAsync()}"); | |
// file1.txt content: file1 content updated | |
} | |
// Blobを削除 | |
{ | |
// "file1.txt"を削除 | |
var blob1 = container.GetBlobReference("file1.txt"); | |
await blob1.DeleteIfExistsAsync(); | |
// "file2.txt"を削除 | |
var blob2 = container.GetBlobReference("file2.txt"); | |
await blob2.DeleteIfExistsAsync(); | |
} | |
// Blobコンテナを削除 | |
await container.DeleteIfExistsAsync(); | |
return true; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Microsoft.WindowsAzure.Storage; | |
namespace ConsoleApp { | |
class Program { | |
static void Main(string[] args) { | |
// 接続文字列(ほんとはコードに埋めないように) | |
var connectionString = "ここに接続文字列"; | |
// ストレージアカウントを作成 | |
var account = CloudStorageAccount.Parse(connectionString); | |
// Blobサンプルを実行 | |
new BlobSample(account).RunAsync().Wait(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment