Cara Membuat Azure Blob Storage

18. 12. 26
posted by: Admin
Hits: 16

Cara Membuat Azure Blob Storage

- Pada portal azure, buka menu Storage Account
- Lalu pilih Azure blob storage
- Pilih locally (LRS)
- Yang perlu ditambahkan ke coding adalah StorageAccountName & StorageAccountKey

- Tambahkan Nuget WindowsAzure.Storage

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.Blob.Protocol;

namespace WebApplication4
{
public class Program
{
const string StorageAccountName = "abc";
const string StorageAccountKey = "def";
const string FolderPath = @"C:\FolderTempatFile\FolderIsiFile";

public static void Main(string[] args)
{
var storageAccount = new CloudStorageAccount(new StorageCredentials(StorageAccountName, StorageAccountKey), true);

var blobClient = storageAccount.CreateCloudBlobClient();

var container = blobClient.GetContainerReference("my-container");
container.CreateIfNotExistsAsync();

container.SetPermissionsAsync(new BlobContainerPermissions()
{
PublicAccess=BlobContainerPublicAccessType.Blob
});

foreach(var filePath in Directory.GetFiles(FolderPath, ".", SearchOption.AllDirectories))
{
var blob = container.GetBlockBlobReference(filePath);
blob.UploadFromFileAsync(filePath);

Console.WriteLine("Uploaded {0}", filePath);
}

Console.WriteLine("Completed");

CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
}

U/ melihat hasil file yg telah diupload bisa dilihat lewat portal azure atau lewat aplikasi desktop Microsoft Azure Storage Explorer yg dapat didownload di
https://azure.microsoft.com/en-us/features/storage-explorer/

Add comment


Security code
Refresh