Tuesday, June 27, 2017

push up a new file to Azure?

I'm not 100% confident this works yet.

using System;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
namespace Whatever.Services
{
   public static class AzureService
   {
      public static void Upload(string storageDirectory, string connectionString,
         byte[] fileData, string fileName, long fileLength, string contentType, string userId)
      {
         CloudBlockBlob blob = null;
         var storageAccount = CloudStorageAccount.Parse(connectionString);
         var blobStorage = storageAccount.CreateCloudBlobClient();
         CloudBlobContainer container =
               blobStorage.GetContainerReference("dummyfiles");
         if (container.CreateIfNotExists())
         {
            var permissions = container.GetPermissions();
            permissions.PublicAccess = BlobContainerPublicAccessType.Off;
            container.SetPermissions(permissions);
         }
         string uniqueBlobName = string.Format("{0}/{1}_{2}", storageDirectory,
               Guid.NewGuid(), fileName);
         blob = container.GetBlockBlobReference(uniqueBlobName);
         blob.Properties.ContentType = contentType;
         blob.UploadFromByteArray(fileData, 0, (int) fileLength,
               AccessCondition.GenerateEmptyCondition());
      }
   }
}

No comments:

Post a Comment