/// <summary>
        /// Checks  if a container exists and creates if if it doesnt
        /// </summary>
        /// <param name="containerName">The container name in question</param>
        /// <returns>True if the container was created, false else</returns>
        public bool CreateIfNotExist(string containerName)
        {
            var cli = _helper.CreateCloudBlobClient();
            var con = cli.GetContainerReference(containerName);

            return(con.CreateIfNotExist());
        }
        public static bool DownloadConfig(string downloadedZipPath, string AzureAccountName, string AzureAccountKey, string orgID, string studyID, string homeID, string containerName, string actualConfigFilename)
        {
            Microsoft.WindowsAzure.CloudStorageAccount storageAccount = null;
            Microsoft.WindowsAzure.StorageClient.CloudBlobClient blobClient = null;
            Microsoft.WindowsAzure.StorageClient.CloudBlobContainer container = null;
            Microsoft.WindowsAzure.StorageClient.CloudBlockBlob blockBlob = null;
            string leaseId = null;

            try
            {
                storageAccount = new Microsoft.WindowsAzure.CloudStorageAccount(new Microsoft.WindowsAzure.StorageCredentialsAccountAndKey(AzureAccountName, AzureAccountKey), true);
                blobClient = storageAccount.CreateCloudBlobClient();
                container = blobClient.GetContainerReference(containerName);

                blockBlob = container.GetBlockBlobReference(ActualConfigBlobName(orgID, studyID, homeID, actualConfigFilename));

                bool blobExists = BlockBlobExists(blockBlob);

                if (blobExists)
                    leaseId = AcquireLease(blockBlob); // Acquire Lease on Blob
                else
                    return false;

                if (blobExists && leaseId == null)
                {
                    Utils.configLog("ER", "AcquireLease on Blob: " + ActualConfigBlobName(orgID, studyID, homeID, actualConfigFilename) + " Failed");
                    return false;
                }

                string url = blockBlob.Uri.ToString();
                if (blockBlob.ServiceClient.Credentials.NeedsTransformUri)
                {
                    url = blockBlob.ServiceClient.Credentials.TransformUri(url);
                }

                var req = BlobRequest.Get(new Uri(url), AzureBlobLeaseTimeout, null, leaseId);
                blockBlob.ServiceClient.Credentials.SignRequest(req);

                using (var reader = new BinaryReader(req.GetResponse().GetResponseStream()))
                {
                    FileStream zipFile = new FileStream(downloadedZipPath, FileMode.OpenOrCreate);
                    reader.BaseStream.CopyTo(zipFile);
                    zipFile.Close();
                }
                req.GetResponse().GetResponseStream().Close();

                ReleaseLease(blockBlob, leaseId); // Release Lease on Blob
                return true;
            }
            catch (Exception e)
            {
                Utils.configLog("E", e.Message + ". DownloadConfig_Azure, downloadZipPath: " + downloadedZipPath + ". " + e);
                ReleaseLease(blockBlob, leaseId);
                return false;
            }
        }
Example #3
0
        public static Tuple <bool, Exception> IsValidAccount(string account, string accountKey)
        {
            bool      accountValid = true;
            Exception exception    = null;

            try
            {
                Microsoft.WindowsAzure.CloudStorageAccount              storageAccount = null;
                Microsoft.WindowsAzure.StorageClient.CloudBlobClient    blobClient     = null;
                Microsoft.WindowsAzure.StorageClient.CloudBlobContainer container      = null;

                storageAccount = new Microsoft.WindowsAzure.CloudStorageAccount(new Microsoft.WindowsAzure.StorageCredentialsAccountAndKey(account, accountKey), true);
                blobClient     = storageAccount.CreateCloudBlobClient();
                container      = blobClient.GetContainerReference(AzureConfigContainerName);
                container.CreateIfNotExist();
            }
            catch (Exception e)
            {
                accountValid = false;
                exception    = e;
            }

            return(new Tuple <bool, Exception>(accountValid, exception));
        }
        public static bool UploadConfig(string configZipPath, string AzureAccountName, string AzureAccountKey, string orgID, string studyID, string homeID, string desiredConfigFilename, NLog.Logger logger = null)
        {
            Microsoft.WindowsAzure.CloudStorageAccount storageAccount = null;
            Microsoft.WindowsAzure.StorageClient.CloudBlobClient blobClient = null;
            Microsoft.WindowsAzure.StorageClient.CloudBlobContainer container = null;
            Microsoft.WindowsAzure.StorageClient.CloudBlockBlob blockBlob = null;
            string leaseId = null;

            try
            {
                storageAccount = new Microsoft.WindowsAzure.CloudStorageAccount(new Microsoft.WindowsAzure.StorageCredentialsAccountAndKey(AzureAccountName, AzureAccountKey), true);
                blobClient = storageAccount.CreateCloudBlobClient();
                container = blobClient.GetContainerReference(AzureConfigContainerName);
                container.CreateIfNotExist();
                blockBlob = container.GetBlockBlobReference(DesiredConfigBlobName(orgID, studyID, homeID, desiredConfigFilename));

                bool blobExists = BlockBlobExists(blockBlob);

                if (blobExists)
                    leaseId = AcquireLease(blockBlob, logger); // Acquire Lease on Blob
                else
                    blockBlob.Container.CreateIfNotExist();

                if (blobExists && leaseId == null)
                {
                    if (null != logger)
                    {
                        logger.Error("AcquireLease on Blob: " + DesiredConfigBlobName(orgID, studyID, homeID, desiredConfigFilename) + " Failed");
                    }
                    return false;
                }

                string url = blockBlob.Uri.ToString();
                if (blockBlob.ServiceClient.Credentials.NeedsTransformUri)
                {
                    url = blockBlob.ServiceClient.Credentials.TransformUri(url);
                }

                var req = BlobRequest.Put(new Uri(url), AzureBlobLeaseTimeout, new Microsoft.WindowsAzure.StorageClient.BlobProperties(), Microsoft.WindowsAzure.StorageClient.BlobType.BlockBlob, leaseId, 0);

                using (var writer = new BinaryWriter(req.GetRequestStream()))
                {
                    writer.Write(File.ReadAllBytes(configZipPath));
                    writer.Close();
                }

                blockBlob.ServiceClient.Credentials.SignRequest(req);
                req.GetResponse().Close();
                ReleaseLease(blockBlob, leaseId); // Release Lease on Blob
                return true;
            }
            catch (Exception e)
            {
                if (null != logger)
                {
                    logger.ErrorException("UploadConfig_Azure, configZipPath: " + configZipPath, e);
                }
                ReleaseLease(blockBlob, leaseId);
                return false;
            }
        }
        public static Tuple<bool, Exception> IsValidAccount(string account, string accountKey)
        {
            bool accountValid = true;
            Exception exception = null;
            try
            {
                Microsoft.WindowsAzure.CloudStorageAccount storageAccount = null;
                Microsoft.WindowsAzure.StorageClient.CloudBlobClient blobClient = null;
                Microsoft.WindowsAzure.StorageClient.CloudBlobContainer container = null;

                storageAccount = new Microsoft.WindowsAzure.CloudStorageAccount(new Microsoft.WindowsAzure.StorageCredentialsAccountAndKey(account, accountKey), true);
                blobClient = storageAccount.CreateCloudBlobClient();
                container = blobClient.GetContainerReference(AzureConfigContainerName);
                container.CreateIfNotExist();
            }
            catch(Exception e)
            {
                accountValid = false;
                exception = e;
            }

            return new Tuple<bool, Exception>(accountValid, exception);
        }
Example #6
0
        public static bool DownloadConfig(string downloadedZipPath, string AzureAccountName, string AzureAccountKey, string orgID, string studyID, string homeID, string configFilename, NLog.Logger logger = null)
        {
            Microsoft.WindowsAzure.CloudStorageAccount              storageAccount = null;
            Microsoft.WindowsAzure.StorageClient.CloudBlobClient    blobClient     = null;
            Microsoft.WindowsAzure.StorageClient.CloudBlobContainer container      = null;
            Microsoft.WindowsAzure.StorageClient.CloudBlockBlob     blockBlob      = null;
            string leaseId = null;

            try
            {
                storageAccount = new Microsoft.WindowsAzure.CloudStorageAccount(new Microsoft.WindowsAzure.StorageCredentialsAccountAndKey(AzureAccountName, AzureAccountKey), true);
                blobClient     = storageAccount.CreateCloudBlobClient();
                container      = blobClient.GetContainerReference(AzureConfigContainerName);

                if (configFilename == PackagerHelper.ConfigPackagerHelper.actualConfigFileName)
                {
                    blockBlob = container.GetBlockBlobReference(ActualConfigBlobName(orgID, studyID, homeID, configFilename));
                }
                else if (configFilename == PackagerHelper.ConfigPackagerHelper.desiredConfigFileName)
                {
                    blockBlob = container.GetBlockBlobReference(DesiredConfigBlobName(orgID, studyID, homeID, configFilename));
                }

                bool blobExists = BlockBlobExists(blockBlob);

                if (blobExists)
                {
                    leaseId = AcquireLease(blockBlob, logger); // Acquire Lease on Blob
                }
                else
                {
                    return(false);
                }

                if (blobExists && leaseId == null)
                {
                    if (null != logger)
                    {
                        logger.Error("AcquireLease on Blob: " + ActualConfigBlobName(orgID, studyID, homeID, configFilename) + " Failed");
                    }
                    return(false);
                }

                string url = blockBlob.Uri.ToString();
                if (blockBlob.ServiceClient.Credentials.NeedsTransformUri)
                {
                    url = blockBlob.ServiceClient.Credentials.TransformUri(url);
                }

                var req = BlobRequest.Get(new Uri(url), AzureBlobLeaseTimeout, null, leaseId);
                blockBlob.ServiceClient.Credentials.SignRequest(req);

                using (var reader = new BinaryReader(req.GetResponse().GetResponseStream()))
                {
                    FileStream zipFile = new FileStream(downloadedZipPath, FileMode.OpenOrCreate);
                    reader.BaseStream.CopyTo(zipFile);
                    zipFile.Close();
                }
                req.GetResponse().GetResponseStream().Close();

                ReleaseLease(blockBlob, leaseId); // Release Lease on Blob
                return(true);
            }
            catch (Exception e)
            {
                if (null != logger)
                {
                    logger.ErrorException("DownloadConfig_Azure, downloadZipPath: " + downloadedZipPath, e);
                }
                ReleaseLease(blockBlob, leaseId);
                return(false);
            }
        }
Example #7
0
        public static bool UploadConfig(string configZipPath, string AzureAccountName, string AzureAccountKey, string orgID, string studyID, string homeID, string desiredConfigFilename, NLog.Logger logger = null)
        {
            Microsoft.WindowsAzure.CloudStorageAccount              storageAccount = null;
            Microsoft.WindowsAzure.StorageClient.CloudBlobClient    blobClient     = null;
            Microsoft.WindowsAzure.StorageClient.CloudBlobContainer container      = null;
            Microsoft.WindowsAzure.StorageClient.CloudBlockBlob     blockBlob      = null;
            string leaseId = null;

            try
            {
                storageAccount = new Microsoft.WindowsAzure.CloudStorageAccount(new Microsoft.WindowsAzure.StorageCredentialsAccountAndKey(AzureAccountName, AzureAccountKey), true);
                blobClient     = storageAccount.CreateCloudBlobClient();
                container      = blobClient.GetContainerReference(AzureConfigContainerName);
                container.CreateIfNotExist();
                blockBlob = container.GetBlockBlobReference(DesiredConfigBlobName(orgID, studyID, homeID, desiredConfigFilename));

                bool blobExists = BlockBlobExists(blockBlob);

                if (blobExists)
                {
                    leaseId = AcquireLease(blockBlob, logger); // Acquire Lease on Blob
                }
                else
                {
                    blockBlob.Container.CreateIfNotExist();
                }

                if (blobExists && leaseId == null)
                {
                    if (null != logger)
                    {
                        logger.Error("AcquireLease on Blob: " + DesiredConfigBlobName(orgID, studyID, homeID, desiredConfigFilename) + " Failed");
                    }
                    return(false);
                }

                string url = blockBlob.Uri.ToString();
                if (blockBlob.ServiceClient.Credentials.NeedsTransformUri)
                {
                    url = blockBlob.ServiceClient.Credentials.TransformUri(url);
                }

                var req = BlobRequest.Put(new Uri(url), AzureBlobLeaseTimeout, new Microsoft.WindowsAzure.StorageClient.BlobProperties(), Microsoft.WindowsAzure.StorageClient.BlobType.BlockBlob, leaseId, 0);

                using (var writer = new BinaryWriter(req.GetRequestStream()))
                {
                    writer.Write(File.ReadAllBytes(configZipPath));
                    writer.Close();
                }

                blockBlob.ServiceClient.Credentials.SignRequest(req);
                req.GetResponse().Close();
                ReleaseLease(blockBlob, leaseId); // Release Lease on Blob
                return(true);
            }
            catch (Exception e)
            {
                if (null != logger)
                {
                    logger.ErrorException("UploadConfig_Azure, configZipPath: " + configZipPath, e);
                }
                ReleaseLease(blockBlob, leaseId);
                return(false);
            }
        }