public void SetBlobContentWithProperties(StorageBlob.BlobType blobType)
        {
            string filePath = FileUtil.GenerateOneTempTestFile();

            StorageBlob.CloudBlobContainer container = blobUtil.CreateContainer();
            Hashtable properties = new Hashtable();

            properties.Add("CacheControl", Utility.GenNameString(string.Empty));
            properties.Add("ContentEncoding", Utility.GenNameString(string.Empty));
            properties.Add("ContentLanguage", Utility.GenNameString(string.Empty));
            properties.Add("ContentMD5", Utility.GenNameString(string.Empty));
            properties.Add("ContentType", Utility.GenNameString(string.Empty));

            try
            {
                Test.Assert(agent.SetAzureStorageBlobContent(filePath, container.Name, blobType, string.Empty, true, -1, properties), "set blob content with property should succeed");
                StorageBlob.ICloudBlob blob = container.GetBlobReferenceFromServer(Path.GetFileName(filePath));
                blob.FetchAttributes();
                ExpectEqual(properties["CacheControl"].ToString(), blob.Properties.CacheControl, "Cache control");
                ExpectEqual(properties["ContentEncoding"].ToString(), blob.Properties.ContentEncoding, "Content Encoding");
                ExpectEqual(properties["ContentLanguage"].ToString(), blob.Properties.ContentLanguage, "Content Language");
                ExpectEqual(properties["ContentMD5"].ToString(), blob.Properties.ContentMD5, "Content MD5");
                ExpectEqual(properties["ContentType"].ToString(), blob.Properties.ContentType, "Content Type");
            }
            finally
            {
                blobUtil.RemoveContainer(container.Name);
                FileUtil.RemoveFile(filePath);
            }
        }
        public void SetPageBlobWithInvalidFileSize()
        {
            string fileName = Utility.GenNameString("tinypageblob");
            string filePath = Path.Combine(uploadDirRoot, fileName);
            int    fileSize = 480;

            Helper.GenerateTinyFile(filePath, fileSize);
            string containerName = Utility.GenNameString("container");

            StorageBlob.CloudBlobContainer container = blobUtil.CreateContainer(containerName);

            try
            {
                List <StorageBlob.IListBlobItem> blobLists = container.ListBlobs(string.Empty, true, StorageBlob.BlobListingDetails.All).ToList();
                Test.Assert(blobLists.Count == 0, string.Format("container {0} should contain {1} blobs, and actually it contain {2} blobs", containerName, 0, blobLists.Count));
                Test.Assert(!agent.SetAzureStorageBlobContent(filePath, containerName, StorageBlob.BlobType.PageBlob), "upload page blob with invalid file size should be failed.");
                string expectedErrorMessage = "The page blob size must be a multiple of 512 bytes.";
                Test.Assert(agent.ErrorMessages[0].StartsWith(expectedErrorMessage), expectedErrorMessage);
                blobLists = container.ListBlobs(string.Empty, true, StorageBlob.BlobListingDetails.All).ToList();
                Test.Assert(blobLists.Count == 0, string.Format("container {0} should contain {1} blobs, and actually it contain {2} blobs", containerName, 0, blobLists.Count));
            }
            finally
            {
                blobUtil.RemoveContainer(containerName);
                FileUtil.RemoveFile(filePath);
            }
        }
Example #3
0
        public BlobManager(){
            try
            {
                storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString"));

                blobClient = storageAccount.CreateCloudBlobClient();
                container = blobClient.GetContainerReference("supstorage");
                container.CreateIfNotExists();
            }
            catch (ArgumentNullException)
            {
                Trace.TraceInformation("CloudStorageAccount Exception null ou vide");
                // Use Application Local Storage Account String
            }
            catch (NullReferenceException)
            {
                Trace.TraceInformation("CloudBlobClient Or CloudBlobContainer Exception");
                // Create Container 
            }
            catch (FormatException)
            {
                Trace.TraceInformation("CloudStorageAccount Exception Connection String Invalid");
            }
            catch (ArgumentException)
            {
                Trace.TraceInformation("CloudStorageAccount Exception connectionString ne peut pas être analysée");
            }
        }
        internal void UploadFromString(string accountName, string accountKey, string containerName, string fileName, string sourceFileName, string fileContentType, string content)   //, string name, string fileDescription) {
        {
            Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount =
                Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(
                    string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1};BlobEndpoint=https://{0}.blob.core.windows.net/", accountName, accountKey)
                    );
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient    blobClient = storageAccount.CreateCloudBlobClient();
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container  = blobClient.GetContainerReference(containerName);
            container.CreateIfNotExists();
            string ext = System.IO.Path.GetExtension(sourceFileName);

            //string fileName = String.Format(

            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
            blockBlob.Properties.ContentType = fileContentType;
            //blockBlob.Metadata.Add("name", name);
            blockBlob.Metadata.Add("originalfilename", sourceFileName);
            //blockBlob.Metadata.Add("userid", userId.ToString());
            //blockBlob.Metadata.Add("ownerid", userId.ToString());
            DateTime created = DateTime.UtcNow;

            // https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
            // http://stackoverflow.com/questions/114983/given-a-datetime-object-how-do-i-get-a-iso-8601-date-in-string-format
            //blockBlob.Metadata.Add("username", userName);
            blockBlob.Metadata.Add("created", created.ToString("yyyy-MM-ddTHH:mm:ss"));  // "yyyy-MM-ddTHH:mm:ssZ"
            blockBlob.Metadata.Add("modified", created.ToString("yyyy-MM-ddTHH:mm:ss")); // "yyyy-MM-ddTHH:mm:ssZ"
            blockBlob.Metadata.Add("fileext", ext);

            blockBlob.UploadText(content, Encoding.UTF8); // .UploadFromStream(fileInputStream);

            blockBlob.SetMetadata();
        }
 private static void InitStorage()
 {
     var credentials = new StorageCredentials(resources.AccountName, resources.AccountKey);
     var storageAccount = new CloudStorageAccount(credentials, true);
     var blobClient = storageAccount.CreateCloudBlobClient();
     imagesContainer = blobClient.GetContainerReference("images");
 }
Example #6
0
        public void ListBlobsWithBlobPermission()
        {
            string containerName = Utility.GenNameString(ContainerPrefix);

            StorageBlob.CloudBlobContainer container = blobUtil.CreateContainer(containerName, StorageBlob.BlobContainerPublicAccessType.Blob);

            try
            {
                string pageBlobName              = Utility.GenNameString("pageblob");
                string blockBlobName             = Utility.GenNameString("blockblob");
                StorageBlob.ICloudBlob blockBlob = blobUtil.CreateBlockBlob(container, blockBlobName);
                StorageBlob.ICloudBlob pageBlob  = blobUtil.CreatePageBlob(container, pageBlobName);

                Test.Assert(agent.GetAzureStorageBlob(blockBlobName, containerName), Utility.GenComparisonData("Get-AzureStorageBlob", true));
                agent.OutputValidation(new List <StorageBlob.ICloudBlob> {
                    blockBlob
                });
                Test.Assert(agent.GetAzureStorageBlob(pageBlobName, containerName), Utility.GenComparisonData("Get-AzureStorageBlob", true));
                agent.OutputValidation(new List <StorageBlob.ICloudBlob> {
                    pageBlob
                });
            }
            finally
            {
                blobUtil.RemoveContainer(containerName);
            }
        }
Example #7
0
        internal void GetContainerTest(Agent agent)
        {
            string NEW_CONTAINER_NAME = Utility.GenNameString("astoria-");

            Dictionary <string, object> dic = Utility.GenComparisonData(StorageObjectType.Container, NEW_CONTAINER_NAME);

            // create container if it does not exist
            StorageBlob.CloudBlobContainer container = CommonStorageAccount.CreateCloudBlobClient().GetContainerReference(NEW_CONTAINER_NAME);
            container.CreateIfNotExists();

            Collection <Dictionary <string, object> > comp = new Collection <Dictionary <string, object> > {
                dic
            };

            try
            {
                //--------------Get operation--------------
                Test.Assert(agent.GetAzureStorageContainer(NEW_CONTAINER_NAME), Utility.GenComparisonData("GetAzureStorageContainer", true));
                // Verification for returned values
                container.FetchAttributes();
                dic.Add("CloudBlobContainer", container);
                CloudBlobUtil.PackContainerCompareData(container, dic);

                agent.OutputValidation(comp);
            }
            finally
            {
                // clean up
                container.DeleteIfExists();
            }
        }
Example #8
0
        /// <summary>
        /// Parameters:
        ///     Block:
        ///         True for BlockBlob, false for PageBlob
        /// </summary>
        internal void UploadBlobTest(Agent agent, string UploadFilePath, Microsoft.WindowsAzure.Storage.Blob.BlobType Type)
        {
            string NEW_CONTAINER_NAME = Utility.GenNameString("upload-");
            string blobName           = Path.GetFileName(UploadFilePath);

            Collection <Dictionary <string, object> > comp = new Collection <Dictionary <string, object> >();
            Dictionary <string, object> dic = Utility.GenComparisonData(StorageObjectType.Blob, blobName);

            dic["BlobType"] = Type;
            comp.Add(dic);

            // create the container
            StorageBlob.CloudBlobContainer container = CommonStorageAccount.CreateCloudBlobClient().GetContainerReference(NEW_CONTAINER_NAME);
            container.CreateIfNotExists();

            try
            {
                //--------------Upload operation--------------
                Test.Assert(agent.SetAzureStorageBlobContent(UploadFilePath, NEW_CONTAINER_NAME, Type), Utility.GenComparisonData("SendAzureStorageBlob", true));

                StorageBlob.ICloudBlob blob = CommonBlobHelper.QueryBlob(NEW_CONTAINER_NAME, blobName);
                CloudBlobUtil.PackBlobCompareData(blob, dic);
                // Verification for returned values
                agent.OutputValidation(comp);
                Test.Assert(blob != null && blob.Exists(), "blob " + blobName + " should exist!");
            }
            finally
            {
                // cleanup
                container.DeleteIfExists();
            }
        }
Example #9
0
        internal void NewContainerTest(Agent agent)
        {
            string NEW_CONTAINER_NAME = Utility.GenNameString("astoria-");

            Dictionary <string, object> dic = Utility.GenComparisonData(StorageObjectType.Container, NEW_CONTAINER_NAME);
            Collection <Dictionary <string, object> > comp = new Collection <Dictionary <string, object> > {
                dic
            };

            // delete container if it exists
            StorageBlob.CloudBlobContainer container = CommonStorageAccount.CreateCloudBlobClient().GetContainerReference(NEW_CONTAINER_NAME);
            container.DeleteIfExists();

            try
            {
                //--------------New operation--------------
                Test.Assert(agent.NewAzureStorageContainer(NEW_CONTAINER_NAME), Utility.GenComparisonData("NewAzureStorageContainer", true));
                // Verification for returned values
                CloudBlobUtil.PackContainerCompareData(container, dic);
                agent.OutputValidation(comp);
                Test.Assert(container.Exists(), "container {0} should exist!", NEW_CONTAINER_NAME);
            }
            finally
            {
                // clean up
                container.DeleteIfExists();
            }
        }
Example #10
0
        public void GetCopyStateFromMultiBlobsTest()
        {
            StorageBlob.CloudBlobContainer srcContainer  = blobUtil.CreateContainer();
            StorageBlob.CloudBlobContainer destContainer = blobUtil.CreateContainer();

            List <StorageBlob.ICloudBlob> blobs = blobUtil.CreateRandomBlob(srcContainer);

            try
            {
                ((PowerShellAgent)agent).AddPipelineScript(String.Format("Get-AzureStorageBlob -Container {0}", srcContainer.Name));
                ((PowerShellAgent)agent).AddPipelineScript(String.Format("Start-AzureStorageBlobCopy -DestContainer {0}", destContainer.Name));

                Test.Assert(agent.GetAzureStorageBlobCopyState(string.Empty, string.Empty, true), "Get copy state for many blobs should be successed.");
                Test.Assert(agent.Output.Count == blobs.Count, String.Format("Expected get {0} copy state, and actually get {1} copy state", blobs.Count, agent.Output.Count));
                List <StorageBlob.IListBlobItem> destBlobs = destContainer.ListBlobs().ToList();
                Test.Assert(destBlobs.Count == blobs.Count, String.Format("Expected get {0} copied blobs, and actually get {1} copy state", blobs.Count, destBlobs.Count));

                for (int i = 0, count = agent.Output.Count; i < count; i++)
                {
                    AssertFinishedCopyState(blobs[i].Uri, i);
                }
            }
            finally
            {
                blobUtil.RemoveContainer(srcContainer.Name);
                blobUtil.RemoveContainer(destContainer.Name);
            }
        }
Example #11
0
        public void GetCopyStateFromCrossAccountCopyTest()
        {
            CloudStorageAccount secondaryAccount = TestBase.GetCloudStorageAccountFromConfig("Secondary");
            object        destContext            = PowerShellAgent.GetStorageContext(secondaryAccount.ToString(true));
            CloudBlobUtil destBlobUtil           = new CloudBlobUtil(secondaryAccount);
            string        destContainerName      = Utility.GenNameString("secondary");

            StorageBlob.CloudBlobContainer destContainer = destBlobUtil.CreateContainer(destContainerName);
            blobUtil.SetupTestContainerAndBlob();
            //remove the same name container in source storage account, so we could avoid some conflicts.
            blobUtil.RemoveContainer(destContainer.Name);

            try
            {
                Test.Assert(agent.StartAzureStorageBlobCopy(blobUtil.Blob, destContainer.Name, string.Empty, destContext), "Start cross account copy should successed");
                int expectedBlobCount = 1;
                Test.Assert(agent.Output.Count == expectedBlobCount, String.Format("Expected get {0} copy blob, and actually it's {1}", expectedBlobCount, agent.Output.Count));
                StorageBlob.ICloudBlob destBlob = (StorageBlob.ICloudBlob)agent.Output[0]["ICloudBlob"];
                //make sure this context is different from the PowerShell.Context
                object context = agent.Output[0]["Context"];
                Test.Assert(PowerShellAgent.Context != context, "make sure you are using different context for cross account copy");
                Test.Assert(agent.GetAzureStorageBlobCopyState(destBlob, context, true), "Get copy state in dest container should be successed.");
                AssertFinishedCopyState(blobUtil.Blob.Uri);
            }
            finally
            {
                blobUtil.CleanupTestContainerAndBlob();
                destBlobUtil.RemoveContainer(destContainer.Name);
            }
        }
Example #12
0
        public void AnonymousContextWithEndPoint()
        {
            string containerName = Utility.GenNameString(ContainerPrefix);

            StorageBlob.CloudBlobContainer container = blobUtil.CreateContainer(containerName, StorageBlob.BlobContainerPublicAccessType.Blob);

            try
            {
                string pageBlobName              = Utility.GenNameString("pageblob");
                string blockBlobName             = Utility.GenNameString("blockblob");
                StorageBlob.ICloudBlob blockBlob = blobUtil.CreateBlockBlob(container, blockBlobName);
                StorageBlob.ICloudBlob pageBlob  = blobUtil.CreatePageBlob(container, pageBlobName);

                agent.UseContextParam = false;
                string cmd = string.Format("new-azurestoragecontext -StorageAccountName {0} " +
                                           "-Anonymous -EndPoint {1}", StorageAccountName, StorageEndPoint);
                ((PowerShellAgent)agent).AddPipelineScript(cmd);
                Test.Assert(agent.GetAzureStorageBlob(blockBlobName, containerName), Utility.GenComparisonData("Get-AzureStorageBlob", true));
                agent.OutputValidation(new List <StorageBlob.ICloudBlob> {
                    blockBlob
                });
                ((PowerShellAgent)agent).AddPipelineScript(cmd);
                Test.Assert(agent.GetAzureStorageBlob(pageBlobName, containerName), Utility.GenComparisonData("Get-AzureStorageBlob", true));
                agent.OutputValidation(new List <StorageBlob.ICloudBlob> {
                    pageBlob
                });
            }
            finally
            {
                blobUtil.RemoveContainer(containerName);
            }
        }
 protected void SetContainerPermission(CloudBlobContainer container, BlobContainerPublicAccessType perimssion)
 {
     container.SetPermissionsAsync(new BlobContainerPermissions
     {
         PublicAccess = perimssion
     });
 }
Example #14
0
        public BlobContainer(string name)
        {
            // hämta connectionsträngen från config // RoleEnviroment bestämmer settingvalue runtime
            //var connectionString = RoleEnvironment.GetConfigurationSettingValue("PhotoAppStorage");
            //var connectionString = CloudConfigurationManager.GetSetting("CloudStorageApp");
            // hämtar kontot utfrån connectionsträngens värde
            //var account = CloudStorageAccount.Parse(connectionString);

            //var account = CloudStorageAccount.DevelopmentStorageAccount;

            var cred = new StorageCredentials("jholm",
                "/bVipQ2JxjWwYrZQfHmzhaBx1p1s8BoD/wX6VWOmg4/gpVo/aALrjsDUKqzXsFtc9utepPqe65NposrXt9YsyA==");
            var account = new CloudStorageAccount(cred, true);

            // skapar en blobclient
            _client = account.CreateCloudBlobClient();

            m_BlobContainer = _client.GetContainerReference(name);

            // Om det inte finns någon container med det namnet
            if (!m_BlobContainer.Exists())
            {
                // Skapa containern
                m_BlobContainer.Create();
                var permissions = new BlobContainerPermissions()
                {
                    PublicAccess = BlobContainerPublicAccessType.Blob
                };
                // Sätter public access till blobs
                m_BlobContainer.SetPermissions(permissions);
            }
        }
        public static async Task<List<string>> CreateBlobsAsync(CloudBlobContainer container, int count, BlobType type)
        {
            string name;
            List<string> blobs = new List<string>();
            for (int i = 0; i < count; i++)
            {
                switch (type)
                {
                    case BlobType.BlockBlob:
                        name = "bb" + Guid.NewGuid().ToString();
                        CloudBlockBlob blockBlob = container.GetBlockBlobReference(name);
                        await blockBlob.PutBlockListAsync(new string[] { });
                        blobs.Add(name);
                        break;

                    case BlobType.PageBlob:
                        name = "pb" + Guid.NewGuid().ToString();
                        CloudPageBlob pageBlob = container.GetPageBlobReference(name);
                        await pageBlob.CreateAsync(0);
                        blobs.Add(name);
                        break;

                    case BlobType.AppendBlob:
                        name = "ab" + Guid.NewGuid().ToString();
                        CloudAppendBlob appendBlob = container.GetAppendBlobReference(name);
                        await appendBlob.CreateOrReplaceAsync();
                        blobs.Add(name);
                        break;
                }
            }
            return blobs;
        }
        /// <summary>
        /// create a container with random properties and metadata
        /// </summary>
        /// <param name="containerName">container name</param>
        /// <returns>the created container object with properties and metadata</returns>
        public StorageBlob.CloudBlobContainer CreateContainer(string containerName = "")
        {
            if (String.IsNullOrEmpty(containerName))
            {
                containerName = Utility.GenNameString("container");
            }

            StorageBlob.CloudBlobContainer container = client.GetContainerReference(containerName);
            container.CreateIfNotExists();

            //there is no properties to set
            container.FetchAttributes();

            int minMetaCount       = 1;
            int maxMetaCount       = 5;
            int minMetaValueLength = 10;
            int maxMetaValueLength = 20;
            int count = random.Next(minMetaCount, maxMetaCount);

            for (int i = 0; i < count; i++)
            {
                string metaKey     = Utility.GenNameString("metatest");
                int    valueLength = random.Next(minMetaValueLength, maxMetaValueLength);
                string metaValue   = Utility.GenNameString("metavalue-", valueLength);
                container.Metadata.Add(metaKey, metaValue);
            }

            container.SetMetadata();

            Test.Info(string.Format("create container '{0}'", containerName));
            return(container);
        }
Example #17
0
        /// <summary>
        /// Initiates the SolCat Azure blob data sync.  
        /// </summary>
        public static void CopyBlobData()
        {
            // Authentication Credentials for Azure Storage:
            var credsSrc
                = new StorageCredentials(
                    ConfigHelper.GetConfigValue("HubContainerName"),
                    ConfigHelper.GetConfigValue("HubContainerKey"));

            var credsDest
                = new StorageCredentials(
                    ConfigHelper.GetConfigValue("NodeContainerKey"),
                    ConfigHelper.GetConfigValue("NodeContainerKey"));

            // Source Container: Hub (Development)
            _srcContainer =
                new CloudBlobContainer(
                    new Uri(ConfigHelper.GetConfigValue("HubContainerUri")),
                    credsSrc);

            // Destination Container: Node (Production)
            _destContainer =
                new CloudBlobContainer(
                    new Uri(ConfigHelper.GetConfigValue("NodeContainerUri")),
                    credsDest);

            // Set permissions on the container:
            var permissions = new BlobContainerPermissions {PublicAccess = BlobContainerPublicAccessType.Blob};
            _srcContainer.SetPermissions(permissions);
            _destContainer.SetPermissions(permissions);

            // Call the blob copy master method:
            CopyBlobs(_srcContainer, _destContainer);
        }
        public ContainerElementViewModel(CloudBlobContainer container)
        {
            if (container == null)
                throw new ArgumentNullException(nameof(container));

            _container = container;
        }
Example #19
0
 public BlobFile2(FileEntity entity)
 {
     _username = entity.PartitionKey;
     _path = new FilePath(entity.Path);
     _entity = entity;
     _container = AzureServiceHelper.GetUserContainer(_username);
 }
Example #20
0
        /// <summary> Initialization function for this storage provider. </summary>
        /// <see cref="IProvider.Init"/>
        public async Task Init(string name, IProviderRuntime providerRuntime, IProviderConfiguration config)
        {
            Log = providerRuntime.GetLogger("Storage.AzureBlobStorage");

            try
            {
                this.Name = name;
                ConfigureJsonSerializerSettings(config);

                if (!config.Properties.ContainsKey("DataConnectionString")) throw new BadProviderConfigException("The DataConnectionString setting has not been configured in the cloud role. Please add a DataConnectionString setting with a valid Azure Storage connection string.");

                var account = CloudStorageAccount.Parse(config.Properties["DataConnectionString"]);
                var blobClient = account.CreateCloudBlobClient();
                var containerName = config.Properties.ContainsKey("ContainerName") ? config.Properties["ContainerName"] : "grainstate";
                container = blobClient.GetContainerReference(containerName);
                await container.CreateIfNotExistsAsync().ConfigureAwait(false);

                Log.Info((int)AzureProviderErrorCode.AzureBlobProvider_InitProvider, "Init: Name={0} ServiceId={1} {2}", name, providerRuntime.ServiceId.ToString(), string.Join(" ", FormatPropertyMessage(config)));
                Log.Info((int)AzureProviderErrorCode.AzureBlobProvider_ParamConnectionString, "AzureBlobStorage Provider is using DataConnectionString: {0}", ConfigUtilities.PrintDataConnectionInfo(config.Properties["DataConnectionString"]));
            }
            catch (Exception ex)
            {
                Log.Error((int)AzureProviderErrorCode.AzureBlobProvider_InitProvider, ex.ToString(), ex);
                throw;
            }
        }
 public AzureAsyncAppender(CloudStorageAccount storage, string container, AzureBlobAppenderOptions options = null)
 {
     this.storage = storage;
     this.container = container;
     this.options = options;
     this.client = this.storage.CreateCloudBlobClient().GetContainerReference(container);
 }
Example #22
0
 public void InitializeContainer(string container)
 {
     var blobClient = StorageAccount.CreateCloudBlobClient();
     blobContainer = blobClient.GetContainerReference(container);
     blobContainer.CreateIfNotExists();
     blobContainer.SetPermissions(new BlobContainerPermissions() { PublicAccess = BlobContainerPublicAccessType.Blob });
 }
        public BlobStorage(BlobSettings appSettings)
        {
            try
            {
                if (appSettings.ImageSize != null)
                    ResizeLayer = new ResizeLayer(appSettings.ImageSize, ResizeMode.Min);

                UploadThumbnail = appSettings.UploadThumbnail;

                StorageAccountName = appSettings.StorageAccountName;
                StorageAccountAccessKey = appSettings.StorageAccountAccessKey;

                // Create a blob client and retrieve reference to images container
                BlobClient = StorageAccount.CreateCloudBlobClient();
                Container = BlobClient.GetContainerReference(appSettings.ContainerName);

                // Create the "images" container if it doesn't already exist.
                if (Container.CreateIfNotExists())
                {
                    // Enable public access on the newly created "images" container
                    Container.SetPermissions(
                        new BlobContainerPermissions
                        {
                            PublicAccess =
                                BlobContainerPublicAccessType.Blob
                        });
                }

            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
        }
Example #24
0
 private void Init()
 {
     var connectionString = ConfigurationManager.ConnectionStrings["BlobStorage"].ConnectionString;
     var storageAccount = CloudStorageAccount.Parse(connectionString);
     _blobClient = storageAccount.CreateCloudBlobClient();
     _blobContainer = _blobClient.GetContainerReference(ConfigurationManager.AppSettings["PhotoContainer"]);
 }
Example #25
0
        public override bool OnStart()
        {
            ServicePointManager.DefaultConnectionLimit = 12;

            var dbConnString = CloudConfigurationManager.GetSetting("PhotoGalleryDbConnectionString");
            db = new PhotoGalleryContext(dbConnString);

            var storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("StorageConnectionString"));

            Trace.TraceInformation("Creating images blob container");
            var blobClient = storageAccount.CreateCloudBlobClient();
            imagesBlobContainer = blobClient.GetContainerReference("images");
            if (imagesBlobContainer.CreateIfNotExists())
            {

                imagesBlobContainer.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
            }

            Trace.TraceInformation("Creating images queue");
            CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
            imagesQueue = queueClient.GetQueueReference("images");
            imagesQueue.CreateIfNotExists();

            Trace.TraceInformation("Storage initialized");
            return base.OnStart();
        }
Example #26
0
 private void GetClientAndReference()
 {    
     var account = CloudStorageAccount.Parse(_source.ConnectionString);
     var client = account.CreateCloudBlobClient();
     _containerReference = client.GetContainerReference(_source.ContainerName);
     _containerReference.CreateIfNotExists();
 }
 private static void InitStorage()
 {
     var credentials = new StorageCredentials(AppKeys.Storage_Account_Name, AppKeys.PrimaryAccessKey);
     var storageAccount = new CloudStorageAccount(credentials, true);
     var blobClient = storageAccount.CreateCloudBlobClient();
     imagesContainer = blobClient.GetContainerReference("images");
 }
        public static void ParallelUploadFile(CloudBlobContainer blobContainer, string blobName, string filePath, string contentType = null)
        {
            var start = DateTime.Now;

              Console.WriteLine("\nUpload file in parallel.");
              CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(blobName);

              // 2M blocks for demo
              int blockLength = 2 * 1000 * 1024;
              byte[] dataToUpload = File.ReadAllBytes(filePath);
              int numberOfBlocks = (dataToUpload.Length / blockLength) + 1;
              string[] blockIds = new string[numberOfBlocks];

              Parallel.For(0, numberOfBlocks, x =>
              {
            var blockId = Convert.ToBase64String(Guid.NewGuid().ToByteArray());
            var currentLength = Math.Min(blockLength, dataToUpload.Length - (x * blockLength));

            using (var memStream = new MemoryStream(dataToUpload, x * blockLength, currentLength))
            {
              blockBlob.PutBlock(blockId, memStream, null);
            }
            blockIds[x] = blockId;
            Console.WriteLine("BlockId:{0}", blockId);
              });

              if (!String.IsNullOrEmpty(contentType)) { blockBlob.Properties.ContentType = contentType; }
              blockBlob.PutBlockList(blockIds);
              Console.WriteLine("URL:{0}, ETag:{1}", blockBlob.Uri, blockBlob.Properties.ETag);

              var timespan = DateTime.Now - start;
              Console.WriteLine("{0} seconds.", timespan.Seconds);
        }
        public static void ListBlobs(CloudBlobContainer blobContainer, BlobListing blobListing, String prefix = null)
        {
            if (blobListing == BlobListing.Flat)
              {
            foreach (var blockBlob in blobContainer.ListBlobs(prefix: null, useFlatBlobListing: true))
            {
              Console.WriteLine("{0,-10} - {1}", @"Blob", ((CloudBlockBlob)blockBlob).Name);
              Console.WriteLine("{0,-10} - {1}", String.Empty, blockBlob.Uri);
            }
              }
              else if (blobListing == BlobListing.Hierarchical)
              {
            foreach (var blockBlob in blobContainer.ListBlobs(prefix: prefix, useFlatBlobListing: false))
            {
              if (blockBlob is CloudBlockBlob)
              {
            Console.WriteLine("{0,-10} - {1}", @"Blob", ((CloudBlockBlob)blockBlob).Name);
            Console.WriteLine("{0,-10} - {1}", String.Empty, blockBlob.Uri);
              }
              else if (blockBlob is CloudBlobDirectory)
              {
            Console.WriteLine("{0,-10} - {1}", @"Directory", ((CloudBlobDirectory)blockBlob).Prefix);
            Console.WriteLine("{0,-10} - {1}", String.Empty, blockBlob.Uri);

            ListBlobs(blobContainer, BlobListing.Hierarchical, ((CloudBlobDirectory)blockBlob).Prefix);
              }
            }
              }
        }
        public void SetBlobContentWithInvalidBlobType()
        {
            string containerName = Utility.GenNameString("container");

            StorageBlob.CloudBlobContainer container = blobUtil.CreateContainer(containerName);

            try
            {
                string blobName = files[0];

                List <StorageBlob.IListBlobItem> blobLists = container.ListBlobs(string.Empty, true, StorageBlob.BlobListingDetails.All).ToList();
                Test.Assert(blobLists.Count == 0, string.Format("container {0} should contain {1} blobs, and actually it contain {2} blobs", containerName, 0, blobLists.Count));

                Test.Assert(agent.SetAzureStorageBlobContent(Path.Combine(uploadDirRoot, files[0]), containerName, StorageBlob.BlobType.BlockBlob, blobName), "upload blob should be successful.");
                blobLists = container.ListBlobs(string.Empty, true, StorageBlob.BlobListingDetails.All).ToList();
                Test.Assert(blobLists.Count == 1, string.Format("container {0} should contain {1} blobs, and actually it contain {2} blobs", containerName, 1, blobLists.Count));
                string convertBlobName = blobUtil.ConvertFileNameToBlobName(blobName);
                Test.Assert(((StorageBlob.ICloudBlob)blobLists[0]).Name == convertBlobName, string.Format("blob name should be {0}, actually it's {1}", convertBlobName, ((StorageBlob.ICloudBlob)blobLists[0]).Name));

                Test.Assert(!agent.SetAzureStorageBlobContent(Path.Combine(uploadDirRoot, files[0]), containerName, StorageBlob.BlobType.PageBlob, blobName), "upload blob should be with invalid blob should be failed.");
                string expectedErrorMessage = string.Format("Blob type mismatched, the current blob type of '{0}' is BlockBlob.", ((StorageBlob.ICloudBlob)blobLists[0]).Name);
                Test.Assert(agent.ErrorMessages[0] == expectedErrorMessage, string.Format("Expect error message: {0} != {1}", expectedErrorMessage, agent.ErrorMessages[0]));
            }
            finally
            {
                blobUtil.RemoveContainer(containerName);
            }
        }
 public static void UploadFileToBlob(string fileName, string containerSAS)
 {
     Console.WriteLine("Uploading {0} to {1}", fileName, containerSAS);
     CloudBlobContainer container = new CloudBlobContainer(new Uri(containerSAS));
     CloudBlockBlob blob = container.GetBlockBlobReference(fileName);
     blob.UploadFromStream(new FileStream(fileName, FileMode.Open, FileAccess.Read));
 }
        internal void UploadBlobTestGB(Agent agent, StorageBlob.BlobType blobType)
        {
            string uploadFilePath = @".\" + Utility.GenNameString("gbupload");
            string containerName  = Utility.GenNameString("gbupload-");
            string blobName       = Path.GetFileName(uploadFilePath);

            // create the container
            StorageBlob.CloudBlobContainer container = blobUtil.CreateContainer(containerName);

            // Generate a 512 bytes file which contains GB18030 characters
            File.WriteAllText(uploadFilePath, GB18030String);

            try
            {
                //--------------Upload operation--------------
                Test.Assert(agent.SetAzureStorageBlobContent(uploadFilePath, containerName, blobType),
                            Utility.GenComparisonData("SendAzureStorageBlob", true));

                StorageBlob.ICloudBlob blob = CloudBlobUtil.GetBlob(container, blobName, blobType);
                Test.Assert(blob != null && blob.Exists(), "blob " + blobName + " should exist!");

                // Check MD5
                string localMd5 = Helper.GetFileContentMD5(uploadFilePath);
                blob.FetchAttributes();
                Test.Assert(localMd5 == blob.Properties.ContentMD5,
                            string.Format("blob content md5 should be {0}, and actualy it's {1}", localMd5, blob.Properties.ContentMD5));
            }
            finally
            {
                // cleanup
                container.DeleteIfExists();
                File.Delete(uploadFilePath);
            }
        }
        public AzureIndexInput(AzureDirectory azuredirectory, ICloudBlob blob)
            : base(blob.Name)
        {
            _name = blob.Uri.Segments[blob.Uri.Segments.Length - 1];

            _fileMutex = BlobMutexManager.GrabMutex(_name);
            _fileMutex.WaitOne();

            try
            {
                _azureDirectory = azuredirectory;
                _blobContainer = azuredirectory.BlobContainer;
                _blob = blob;

                string fileName = _name;
                StreamOutput fileStream = _azureDirectory.CreateCachedOutputAsStream(fileName);

                // get the blob
                _blob.DownloadToStream(fileStream);

                fileStream.Flush();
                Debug.WriteLine("GET {0} RETREIVED {1} bytes", _name, fileStream.Length);

                fileStream.Close();

                // and open it as an input
                _indexInput = CacheDirectory.openInput(fileName, IOContext.DEFAULT);
            }
            finally
            {
                _fileMutex.ReleaseMutex();
            }
        }
        private async Task<bool> CloudBlobDirectorySetupWithDelimiterAsync(CloudBlobContainer container, string delimiter = "/")
        {
            try
            {
                for (int i = 1; i < 3; i++)
                {
                    for (int j = 1; j < 3; j++)
                    {
                        for (int k = 1; k < 3; k++)
                        {
                            String directory = "TopDir" + i + delimiter + "MidDir" + j + delimiter + "EndDir" + k + delimiter + "EndBlob" + k;
                            CloudPageBlob blob1 = container.GetPageBlobReference(directory);
                            await blob1.CreateAsync(0);
                        }
                    }

                    CloudPageBlob blob2 = container.GetPageBlobReference("TopDir" + i + delimiter + "Blob" + i);
                    await blob2.CreateAsync(0);
                }

                return true;
            }
            catch (Exception e)
            {
                throw e;
            }

        }
        public async Task CopyContainer(CloudBlobContainer sourceContainer, string destination)
        {
            var uri = new Uri(sourceContainer.Uri.AbsoluteUri.TrimEnd('/') + '/');
            destination = Path.Combine(destination, sourceContainer.Name);

            BlobContinuationToken continuationToken = null;
            do
            {
                var segments = await sourceContainer.ListBlobsSegmentedAsync(prefix: null, useFlatBlobListing: true,
                                                blobListingDetails: BlobListingDetails.Metadata, maxResults: MaxParallelDownloads,
                                                currentToken: continuationToken, options: null, operationContext: null);
                
                var tasks = new BlockingCollection<Task>(MaxParallelDownloads);

                Parallel.ForEach(segments.Results.Cast<CloudBlockBlob>(), srcFile =>
                {
                    var relativePath = uri.MakeRelativeUri(srcFile.Uri);
                    var destLocation = Path.Combine(destination, relativePath.OriginalString);
                    
                    if (File.Exists(destLocation) && File.GetLastWriteTimeUtc(destLocation) == srcFile.Properties.LastModified)
                    {
                        // If the file looks unchanged, skip it.
                        return;
                    }
                    Directory.CreateDirectory(Path.GetDirectoryName(destLocation));
                    tasks.Add(srcFile.DownloadToFileAsync(destLocation, FileMode.Create));
                });

                await Task.WhenAll(tasks);
                continuationToken = segments.ContinuationToken;
            } while (continuationToken != null);
        }
    public async Task Init( string name, IProviderRuntime providerRuntime, IProviderConfiguration config )
    {
      Log = providerRuntime.GetLogger( this.GetType().Name );

      try
      {
        ConfigureJsonSerializerSettings( config );

        if( !config.Properties.ContainsKey( "DataConnectionString" ) )
        {
          throw new BadProviderConfigException(
            "The DataConnectionString setting has not been configured in the cloud role. Please add a DataConnectionString setting with a valid Azure Storage connection string." );
        }
        else
        {
          var account = CloudStorageAccount.Parse( config.Properties[ "DataConnectionString" ] );
          var blobClient = account.CreateCloudBlobClient();
          var containerName = config.Properties.ContainsKey( "ContainerName" ) ? config.Properties[ "ContainerName" ] : "grainstate";
          container = blobClient.GetContainerReference( containerName );
          await container.CreateIfNotExistsAsync();
        }
      }
      catch( Exception ex )
      {
        Log.Error( 0, ex.ToString(), ex );
        throw;
      }
    }
 public static void PackContainerCompareData(StorageBlob.CloudBlobContainer container, Dictionary <string, object> dic)
 {
     StorageBlob.BlobContainerPermissions permissions = container.GetPermissions();
     dic["PublicAccess"] = permissions.PublicAccess;
     dic["Permission"]   = permissions;
     dic["LastModified"] = container.Properties.LastModified;
 }
Example #38
0
        /// <summary>
        /// Occurs when a storage provider operation has completed.
        /// </summary>
        //public event EventHandler<StorageProviderEventArgs> StorageProviderOperationCompleted;

        #endregion

        // Initialiser method
        private void Initialise(string storageAccount, string containerName)
        {
            if (String.IsNullOrEmpty(containerName))
                throw new ArgumentException("You must provide the base Container Name", "containerName");
            
            ContainerName = containerName;

            _account = CloudStorageAccount.Parse(storageAccount);
            _blobClient = _account.CreateCloudBlobClient();
            _container = _blobClient.GetContainerReference(ContainerName);
            try
            {
                _container.FetchAttributes();
            }
            catch (StorageException)
            {
                Trace.WriteLine(string.Format("Create new container: {0}", ContainerName), "Information");
                _container.Create();

                // set new container's permissions
                // Create a permission policy to set the public access setting for the container. 
                BlobContainerPermissions containerPermissions = new BlobContainerPermissions();

                // The public access setting explicitly specifies that the container is private,
                // so that it can't be accessed anonymously.
                containerPermissions.PublicAccess = BlobContainerPublicAccessType.Off;

                //Set the permission policy on the container.
                _container.SetPermissions(containerPermissions);
            }
        }
Example #39
0
        public override bool OnStart()
        {
            ServicePointManager.DefaultConnectionLimit = Environment.ProcessorCount;

            // Read storage account configuration settings
            ConfigureDiagnostics();
            Trace.TraceInformation("Initializing storage account in worker role B");
            var storageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("StorageConnectionString"));

            // Initialize queue storage
            Trace.TraceInformation("Creating queue client.");
            CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
            sendEmailQueue = queueClient.GetQueueReference("azuremailqueue");
            subscribeQueue = queueClient.GetQueueReference("azuremailsubscribequeue");

            // Initialize blob storage
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            blobContainer = blobClient.GetContainerReference("azuremailblobcontainer");

            // Initialize table storage
            var tableClient = storageAccount.CreateCloudTableClient();
            mailingListTable = tableClient.GetTableReference("mailinglist");
            messageTable = tableClient.GetTableReference("message");
            messagearchiveTable = tableClient.GetTableReference("messagearchive");

            Trace.TraceInformation("WorkerB: Creating blob container, queue, tables, if they don't exist.");
            blobContainer.CreateIfNotExists();
            sendEmailQueue.CreateIfNotExists();
            subscribeQueue.CreateIfNotExists();
            this.messageTable.CreateIfNotExists();
            this.mailingListTable.CreateIfNotExists();
            this.messagearchiveTable.CreateIfNotExists();

            return base.OnStart();
        }
Example #40
0
        public AzureFileSystem(string containerName, string root, bool isPrivate, CloudStorageAccount storageAccount) {
            // Setup the connection to custom storage accountm, e.g. Development Storage
            _storageAccount = storageAccount;
            ContainerName = containerName;
            _root = String.IsNullOrEmpty(root) ? "": root + "/";
            _absoluteRoot = Combine(Combine(_storageAccount.BlobEndpoint.AbsoluteUri, containerName), _root);

            //using ( new HttpContextWeaver() ) 
            {

                BlobClient = _storageAccount.CreateCloudBlobClient();
                // Get and create the container if it does not exist
                // The container is named with DNS naming restrictions (i.e. all lower case)
                Container = BlobClient.GetContainerReference(ContainerName);

                Container.CreateIfNotExists();

                Container.SetPermissions(isPrivate
                                             ? new BlobContainerPermissions
                                                   {PublicAccess = BlobContainerPublicAccessType.Off}
                                             : new BlobContainerPermissions
                                                   {PublicAccess = BlobContainerPublicAccessType.Blob}); // deny listing 
            }

        }
        public static List<string> CreateBlobsTask(CloudBlobContainer container, int count, BlobType type)
        {
            string name;
            List<string> blobs = new List<string>();
            for (int i = 0; i < count; i++)
            {
                switch (type)
                {
                    case BlobType.BlockBlob:
                        name = "bb" + Guid.NewGuid().ToString();
                        CloudBlockBlob blockBlob = container.GetBlockBlobReference(name);
                        blockBlob.PutBlockListAsync(new string[] { }).Wait();
                        blobs.Add(name);
                        break;

                    case BlobType.PageBlob:
                        name = "pb" + Guid.NewGuid().ToString();
                        CloudPageBlob pageBlob = container.GetPageBlobReference(name);
                        pageBlob.CreateAsync(0).Wait();
                        blobs.Add(name);
                        break;
                }
            }
            return blobs;
        }
 public AzureImageUploader()
 {
     var storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
       _blobClient = storageAccount.CreateCloudBlobClient();
       _container = FindOrCreateContainer(ContainerName);
       _filecontainer = FindOrCreateContainer(FileContainerName);
 }
Example #43
0
 public List<CloudBlockBlob> GetBlobs(CloudBlobContainer container, string filter = "")
 {
     return container.ListBlobs()
             .OfType<CloudBlockBlob>()
             .Where(b => String.IsNullOrEmpty(filter) || b.Name.Contains(filter))
             .ToList();
 }
        public static List<string> CreateLogs(CloudBlobContainer container, StorageService service, int count, DateTime start, string granularity)
        {
            string name;
            List<string> blobs = new List<string>();

            for (int i = 0; i < count; i++)
            {
                CloudBlockBlob blockBlob;

                switch (granularity)
                {
                    case "hour":
                        name = string.Concat(service.ToString().ToLowerInvariant(), "/", start.AddHours(i).ToString("yyyy/MM/dd/HH", CultureInfo.InvariantCulture), "00/000001.log"); 
                        break;

                    case "day":
                        name = string.Concat(service.ToString().ToLowerInvariant(), "/", start.AddDays(i).ToString("yyyy/MM/dd/HH", CultureInfo.InvariantCulture), "00/000001.log"); 
                        break;

                    case "month":
                        name = string.Concat(service.ToString().ToLowerInvariant(), "/", start.AddMonths(i).ToString("yyyy/MM/dd/HH", CultureInfo.InvariantCulture), "00/000001.log"); 
                        break;

                    default:
                        throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "CreateLogs granularity of '{0}' is invalid.", granularity));
                }

                blockBlob = container.GetBlockBlobReference(name);
                blockBlob.PutBlockList(new string[] { });
                blobs.Add(name);
            }

            return blobs;
        }
 public StorageBlob.CloudBlobContainer CreateContainer(string containerName, StorageBlob.BlobContainerPublicAccessType permission)
 {
     StorageBlob.CloudBlobContainer       container           = CreateContainer(containerName);
     StorageBlob.BlobContainerPermissions containerPermission = new StorageBlob.BlobContainerPermissions();
     containerPermission.PublicAccess = permission;
     container.SetPermissions(containerPermission);
     return(container);
 }
 /// <summary>
 /// Create a random container with a random blob
 /// </summary>
 public void SetupTestContainerAndBlob()
 {
     ContainerName = Utility.GenNameString("container");
     BlobName      = Utility.GenNameString("blob");
     StorageBlob.CloudBlobContainer container = CreateContainer(ContainerName);
     Blob      = CreateRandomBlob(container, BlobName);
     Container = container;
 }
Example #47
0
        //Delete a blob file
        public void DeleteBlob(string UserId, string BlobName)
        {
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = GetCloudBlobContainer(UserId);

            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob blockBlob = container.GetBlockBlobReference(BlobName);
            if (blockBlob.Exists())
            {
                blockBlob.Delete();
            }
        }
Example #48
0
        public string DownloadPublicBlob(string UserId, string BlobName, bool PublicAccess = true)
        {
            //Retrieve a reference to a container.
            //Retrieve storage account from connection string.
            Microsoft.WindowsAzure.Storage.CloudStorageAccount StorageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(Convert.ToString(ConfigurationManager.AppSettings["StorageConnectionString"]));

            // Create the blob client.
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient blobClient = StorageAccount.CreateCloudBlobClient();

            // Retrieve a reference to a container.
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = blobClient.GetContainerReference(UserId);

            // Create the container if it doesn't exist.
            container.CreateIfNotExists();


            //Set permission to public
            if (PublicAccess)
            {
                container.SetPermissions(
                    new Microsoft.WindowsAzure.Storage.Blob.BlobContainerPermissions
                {
                    PublicAccess =
                        Microsoft.WindowsAzure.Storage.Blob.BlobContainerPublicAccessType.Blob
                });
            }
            else
            {
                container.SetPermissions(
                    new Microsoft.WindowsAzure.Storage.Blob.BlobContainerPermissions
                {
                    PublicAccess =
                        Microsoft.WindowsAzure.Storage.Blob.BlobContainerPublicAccessType.Off
                });
            }



            // Retrieve reference to a blob named

            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob blockBlob = container.GetBlockBlobReference(BlobName);

            //var sasToken = blockBlob.GetSharedAccessSignature(new SharedAccessBlobPolicy()
            //{
            //    Permissions = SharedAccessBlobPermissions.Read,
            //    SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(10),//assuming the blob can be downloaded in 10 miinutes
            //}, new SharedAccessBlobHeaders()
            //{
            //    ContentDisposition = "attachment; filename=file-name"
            //});

            //return string.Format("{0}{1}", blockBlob.Uri, sasToken);

            return(blockBlob.Uri.ToString());
        }
        public void SetBlobContentWithSubDirectory()
        {
            DirectoryInfo rootDir = new DirectoryInfo(uploadDirRoot);

            DirectoryInfo[] dirs = rootDir.GetDirectories();

            foreach (DirectoryInfo dir in dirs)
            {
                string containerName = Utility.GenNameString("container");
                StorageBlob.CloudBlobContainer container = blobUtil.CreateContainer(containerName);

                try
                {
                    List <StorageBlob.IListBlobItem> blobLists = container.ListBlobs(string.Empty, true, StorageBlob.BlobListingDetails.All).ToList();
                    Test.Assert(blobLists.Count == 0, string.Format("container {0} should contain {1} blobs, and actually it contain {2} blobs", containerName, 0, blobLists.Count));

                    StorageBlob.BlobType blobType = StorageBlob.BlobType.BlockBlob;

                    if (dir.Name.StartsWith("dirpage"))
                    {
                        blobType = Microsoft.WindowsAzure.Storage.Blob.BlobType.PageBlob;
                    }

                    ((PowerShellAgent)agent).AddPipelineScript(string.Format("ls -File -Recurse -Path {0}", dir.FullName));
                    Test.Info("Upload files...");
                    Test.Assert(agent.SetAzureStorageBlobContent(string.Empty, containerName, blobType), "upload multiple files should be successsed");
                    Test.Info("Upload finished...");

                    blobLists = container.ListBlobs(string.Empty, true, StorageBlob.BlobListingDetails.All).ToList();
                    List <string> dirFiles = files.FindAll(item => item.StartsWith(dir.Name));
                    Test.Assert(blobLists.Count == dirFiles.Count(), string.Format("set-azurestorageblobcontent should upload {0} files, and actually it's {1}", dirFiles.Count(), blobLists.Count));

                    StorageBlob.ICloudBlob blob = null;
                    for (int i = 0, count = dirFiles.Count(); i < count; i++)
                    {
                        blob = blobLists[i] as StorageBlob.ICloudBlob;

                        if (blob == null)
                        {
                            Test.AssertFail("blob can't be null");
                        }

                        string convertedName = blobUtil.ConvertBlobNameToFileName(blob.Name, dir.Name);
                        Test.Assert(dirFiles[i] == convertedName, string.Format("blob name should be {0}, and actully it's {1}", dirFiles[i], convertedName));
                        string localMd5 = Helper.GetFileContentMD5(Path.Combine(uploadDirRoot, dirFiles[i]));
                        Test.Assert(blob.BlobType == blobType, "blob type should be block blob");
                        Test.Assert(localMd5 == blob.Properties.ContentMD5, string.Format("blob content md5 should be {0}, and actualy it's {1}", localMd5, blob.Properties.ContentMD5));
                    }
                }
                finally
                {
                    blobUtil.RemoveContainer(containerName);
                }
            }
        }
Example #50
0
        //Upload a Blob File
        public void UploadByteBlob(string UserId, string BlobName, string ContentType, byte[] image)
        {
            // Retrieve a reference to a container.
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = GetCloudBlobContainer(UserId);

            // Retrieve reference to a blob named
            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob blockBlob = container.GetBlockBlobReference(BlobName);

            blockBlob.Properties.ContentType = ContentType;
            blockBlob.UploadFromByteArray(image, 0, image.Length);
        }
Example #51
0
        // Upload a Blob File
        public void UploadBlob(string UserId, string BlobName, HttpPostedFileBase image, int timeout = 60)
        {
            // Retrieve a reference to a container.
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = GetCloudBlobContainer(UserId);

            // Retrieve reference to a blob named
            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob blockBlob = container.GetBlockBlobReference(BlobName);

            blockBlob.Properties.ContentType = image.ContentType;
            blockBlob.UploadFromStream(image.InputStream);
        }
 /// <summary>
 /// Create a blob with specified blob type
 /// </summary>
 /// <param name="container">CloudBlobContainer object</param>
 /// <param name="blobName">Blob name</param>
 /// <param name="type">Blob type</param>
 /// <returns>ICloudBlob object</returns>
 public StorageBlob.ICloudBlob CreateBlob(StorageBlob.CloudBlobContainer container, string blobName, StorageBlob.BlobType type)
 {
     if (type == StorageBlob.BlobType.BlockBlob)
     {
         return(CreateBlockBlob(container, blobName));
     }
     else
     {
         return(CreatePageBlob(container, blobName));
     }
 }
        public List <StorageBlob.ICloudBlob> CreateRandomBlob(StorageBlob.CloudBlobContainer container)
        {
            int           count     = random.Next(1, 5);
            List <string> blobNames = new List <string>();

            for (int i = 0; i < count; i++)
            {
                blobNames.Add(Utility.GenNameString("blob"));
            }

            return(CreateRandomBlob(container, blobNames));
        }
        private void LogError(Exception ex, string message)
        {
            //ConfigurationManager.ConnectionStrings["CineStorageConStr"].ConnectionString
            Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("CineStorageConStr"));

            Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            // Retrieve a reference to a container.
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = blobClient.GetContainerReference("data");

            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob logBlob = container.GetBlockBlobReference(String.Format(this.LogFile, DateTime.UtcNow.ToString("dd MMM yyyy")));
            try
            {
                using (StreamReader sr = new StreamReader(logBlob.OpenRead()))
                {
                    using (StreamWriter sw = new StreamWriter(logBlob.OpenWrite()))
                    {
                        sw.Write(sr.ReadToEnd());

                        if (ex != null)
                        {
                            sw.Write(System.Environment.NewLine);

                            sw.WriteLine(ex.Message);
                            sw.WriteLine(ex.StackTrace);

                            sw.Write(System.Environment.NewLine);

                            if (ex.InnerException != null)
                            {
                                sw.Write(System.Environment.NewLine);

                                sw.WriteLine(ex.InnerException.Message);
                                sw.WriteLine(ex.InnerException.StackTrace);

                                sw.Write(System.Environment.NewLine);
                            }
                        }

                        if (message != null)
                        {
                            sw.Write(System.Environment.NewLine);

                            sw.WriteLine(message);

                            sw.Write(System.Environment.NewLine);
                        }
                    }
                }
            }
            catch { }
        }
        /// <summary>
        /// clean test container and blob
        /// </summary>
        public void CleanupTestContainerAndBlob()
        {
            if (String.IsNullOrEmpty(ContainerName))
            {
                return;
            }

            RemoveContainer(ContainerName);
            ContainerName = string.Empty;
            BlobName      = string.Empty;
            Blob          = null;
            Container     = null;
        }
        /// <summary>
        /// create a list of blobs with random properties/metadata/blob type
        /// </summary>
        /// <param name="container">CloudBlobContainer object</param>
        /// <param name="blobName">a list of blob names</param>
        /// <returns>a list of cloud page blobs</returns>
        public List <StorageBlob.ICloudBlob> CreateRandomBlob(StorageBlob.CloudBlobContainer container, List <string> blobNames)
        {
            List <StorageBlob.ICloudBlob> blobs = new List <StorageBlob.ICloudBlob>();

            foreach (string blobName in blobNames)
            {
                blobs.Add(CreateRandomBlob(container, blobName));
            }

            blobs = blobs.OrderBy(blob => blob.Name).ToList();

            return(blobs);
        }
 public static StorageBlob.ICloudBlob GetBlob(StorageBlob.CloudBlobContainer container, string blobName, StorageType blobType)
 {
     StorageBlob.ICloudBlob blob = null;
     if (blobType == StorageType.BlockBlob)
     {
         blob = container.GetBlockBlobReference(blobName);
     }
     else
     {
         blob = container.GetPageBlobReference(blobName);
     }
     return(blob);
 }
        /// <summary>
        /// create a new page blob with random properties and metadata
        /// </summary>
        /// <param name="container">CloudBlobContainer object</param>
        /// <param name="blobName">blob name</param>
        /// <returns>ICloudBlob object</returns>
        public StorageBlob.ICloudBlob CreatePageBlob(StorageBlob.CloudBlobContainer container, string blobName)
        {
            StorageBlob.CloudPageBlob pageBlob = container.GetPageBlobReference(blobName);
            int size = random.Next(1, 10) * PageBlobUnitSize;

            pageBlob.Create(size);
            byte[] buffer = new byte[size];
            string md5sum = Convert.ToBase64String(Helper.GetMD5(buffer));

            pageBlob.Properties.ContentMD5 = md5sum;
            GenerateBlobPropertiesAndMetaData(pageBlob);
            Test.Info(string.Format("create page blob '{0}' in container '{1}'", blobName, container.Name));
            return(pageBlob);
        }
        internal void CopyBlobTestGB(Agent agent, StorageBlob.BlobType blobType)
        {
            string uploadFilePath    = @".\" + Utility.GenNameString("gbupload");
            string srcContainerName  = Utility.GenNameString("gbupload-", 15);
            string destContainerName = Utility.GenNameString("gbupload-", 15);
            string blobName          = Path.GetFileName(uploadFilePath);

            // create the container
            StorageBlob.CloudBlobContainer srcContainer  = blobUtil.CreateContainer(srcContainerName);
            StorageBlob.CloudBlobContainer destContainer = blobUtil.CreateContainer(destContainerName);

            // Generate a 512 bytes file which contains GB18030 characters
            File.WriteAllText(uploadFilePath, GB18030String);

            string localMd5 = Helper.GetFileContentMD5(uploadFilePath);

            try
            {
                // create source blob
                StorageBlob.ICloudBlob blob = CloudBlobUtil.GetBlob(srcContainer, blobName, blobType);

                // upload file data
                using (var fileStream = System.IO.File.OpenRead(uploadFilePath))
                {
                    blob.UploadFromStream(fileStream);
                    // need to set MD5 as for page blob, it won't set MD5 automatically
                    blob.Properties.ContentMD5 = localMd5;
                    blob.SetProperties();
                }

                //--------------Copy blob operation--------------
                Test.Assert(agent.StartAzureStorageBlobCopy(srcContainerName, blobName, destContainerName, blobName), Utility.GenComparisonData("Start copy blob using blob name", true));

                // Get destination blob
                blob = CloudBlobUtil.GetBlob(destContainer, blobName, blobType);

                // Check MD5
                blob.FetchAttributes();
                Test.Assert(localMd5 == blob.Properties.ContentMD5,
                            string.Format("blob content md5 should be {0}, and actualy it's {1}", localMd5, blob.Properties.ContentMD5));
            }
            finally
            {
                // cleanup
                srcContainer.DeleteIfExists();
                destContainer.DeleteIfExists();
                File.Delete(uploadFilePath);
            }
        }
        /// <summary>
        /// Create a list of blobs with random properties/metadata/blob type
        /// </summary>
        /// <param name="container">CloudBlobContainer object</param>
        /// <param name="blobName">Blob name</param>
        /// <returns>ICloudBlob object</returns>
        public StorageBlob.ICloudBlob CreateRandomBlob(StorageBlob.CloudBlobContainer container, string blobName)
        {
            int switchKey = 0;

            switchKey = random.Next(0, 2);

            if (switchKey == 0)
            {
                return(CreatePageBlob(container, blobName));
            }
            else
            {
                return(CreateBlockBlob(container, blobName));
            }
        }