Пример #1
0
        public static CloudBlobClient GetCloudBlobClient()
        {
            if (BlobClient == null)
            {
                if (IsDevUrl())
                {
                    CloudStorageAccount storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
                    BlobClient = storageAccount.CreateCloudBlobClient();
                }
                else
                {
                    var    accountName = ConfigHelper.AzureAccountName;
                    string accountKey  = ConfigHelper.AzureAccountKey;

                    var credentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(accountName, accountKey);
                    CloudStorageAccount azureStorageAccount = new CloudStorageAccount(credentials, true);
                    BlobClient = azureStorageAccount.CreateCloudBlobClient();

                    // retry policy.
                    // could do with a little work.
                    IRetryPolicy linearRetryPolicy = new LinearRetry(TimeSpan.FromSeconds(ConfigHelper.RetryAttemptDelayInSeconds), ConfigHelper.MaxRetryAttempts);
                    BlobClient.RetryPolicy = linearRetryPolicy;
                }
            }

            return(BlobClient);
        }
Пример #2
0
        public async Task <MemberValidationResult> ValidateUserAsync(string username, string password)
        {
            _logger.LogInformation($"Validating credentials...");

            //reject details that don't conform to azure storage conventions
            _logger.LogTrace($"Validating password complexity");
            if (password.Length < 80 || password.Length > 92)
            {
                _logger.LogTrace("Password is not compliant.");
                return(new MemberValidationResult(MemberValidationStatus.InvalidLogin));
            }

            //verify details by creating a storage client
            var cred = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(username, password);

            _logger.LogTrace("Logging into storage account...");
            var storageAccount = new CloudStorageAccount(cred, true);
            var blobClient     = storageAccount.CreateCloudBlobClient();
            var props          = await blobClient.GetAccountPropertiesAsync();

            _logger.LogTrace($"Login successful?  {props != null}");
            _logger.LogTrace($"Storage account kind: '{props.AccountKind}'   SKU: '{props.SkuName}'");


            var ftpUser = new AzureStorageFtpUser {
                AccountName = username, AccountKey = password
            };

            return(props != null ?
                   new MemberValidationResult(MemberValidationStatus.AuthenticatedUser, ftpUser) :
                   new MemberValidationResult(MemberValidationStatus.InvalidLogin));
        }
Пример #3
0
        public CloudBlobContainer GetCloudBlobContainer()
        {
            Microsoft.WindowsAzure.Storage.Auth.StorageCredentials storageCreds;
            if (!string.IsNullOrEmpty(SasToken))
            {
                storageCreds = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(SasToken);
            }
            else
            {
                storageCreds = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(StorageAccountName, StorageAccountKey);
            }

            Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(storageCreds, StorageAccountName, null, true);

            // Create the blob client.
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            //Get container reference
            var containerRef = blobClient.GetContainerReference(ContainerName);

            if (containerRef == null)
            {
                throw new Exception($"The container {ContainerName} cannot be found in storage account {StorageAccountName}");
            }

            return(containerRef);
        }
Пример #4
0
        public AzureStorage()
        {
            var storageAccount = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(AzureCredenciais.user, AzureCredenciais.key);
            var storageURI     = new StorageUri(new Uri(AzureCredenciais.url));

            clientAzureStorage = new CloudBlobClient(storageURI, storageAccount);
        }
Пример #5
0
        } // DeleteQueueItem

        /// <summary>
        /// Creates the storage and gets a reference (once)
        /// </summary>
        public static Microsoft.WindowsAzure.Storage.Queue.CloudQueue ConnectToQueueStorage(string queueName, string cloudAccountName, string cloudKey)
        {
            try
            {
                Microsoft.WindowsAzure.Storage.Auth.StorageCredentials storageCredentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(
                    cloudAccountName, cloudKey);

                Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(storageCredentials, true);

                Microsoft.WindowsAzure.Storage.Queue.CloudQueueClient queueStorage = storageAccount.CreateCloudQueueClient();

                queueStorage.DefaultRequestOptions.RetryPolicy = new Microsoft.WindowsAzure.Storage.RetryPolicies.LinearRetry(TimeSpan.FromSeconds(DEFAULT_SAVE_QUEUE_RETRY_WAIT_IN_MILLISECONDS), DEFAULT_SAVE_QUEUE_RETRY_ATTEMPTS);

                queueStorage.DefaultRequestOptions.ServerTimeout = new TimeSpan(0, DEFAULT_SAVE_AND_READ_QUEUE_TIMEOUT_IN_MINUTES, 0);

                Microsoft.WindowsAzure.Storage.Queue.CloudQueue cloudQueue = queueStorage.GetQueueReference(queueName);
                cloudQueue.CreateIfNotExistsAsync();

                return(cloudQueue);
            }
            catch (Exception ex)
            {
                throw new Exception("Storage services initialization failure. "
                                    + "Check your storage account configuration settings. If running locally, "
                                    + "ensure that the Development Storage service is running. \n"
                                    + ex.Message);
            }
        } // ConnectToQueueStorage
Пример #6
0
        private static Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient InitializeClient()
        {
            var accountInfo = AccountInfo.Instance;
            var credentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(accountInfo.AccountName, accountInfo.SharedKey);
            var account     = new CloudStorageAccount(credentials, true);

            return(account.CreateCloudBlobClient());
        }
Пример #7
0
 public AzureBlobHelper(string accountName, string accessKey, string groupName, bool useHttps = false)
 {
     var credentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(accountName, accessKey);
     var storageAccount = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(credentials, useHttps);
     this.CloudBlobClient = storageAccount.CreateCloudBlobClient();
     this.Container = new ContainerLogic(this, groupName);
     this.Blob = new BlobLogic(this, groupName);
 }
Пример #8
0
        /// <summary>
        /// Creates the storage and gets a reference (once)
        /// </summary>
        private static void InitializeStorage(string blobContainer)
        {
            string containerName = blobContainer.ToString().ToLower(); // must be lower case!

            if (storageInitializedDictionary.ContainsKey(containerName) && storageInitializedDictionary[containerName] == true)
            {
                return;
            }

            lock (gate)
            {
                if (storageInitializedDictionary.ContainsKey(containerName) && storageInitializedDictionary[containerName] == true)
                {
                    return;
                }

                try
                {
                    Microsoft.WindowsAzure.Storage.Auth.StorageCredentials storageCredentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(
                        Sample.Azure.Common.Setting.SettingService.CloudStorageAccountName,
                        Sample.Azure.Common.Setting.SettingService.CloudStorageKey);

                    Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(storageCredentials,
                                                                                                                                               new Uri(Sample.Azure.Common.Setting.SettingService.CloudStorageBlobEndPoint),
                                                                                                                                               new Uri(Sample.Azure.Common.Setting.SettingService.CloudStorageQueueEndPoint),
                                                                                                                                               new Uri(Sample.Azure.Common.Setting.SettingService.CloudStorageTableEndPoint),
                                                                                                                                               new Uri(Sample.Azure.Common.Setting.SettingService.CloudStorageFileEndPoint));

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

                    int    blobSaveTimeoutInMinutes = DEFAULT_SAVE_AND_READ_BLOB_TIMEOUT_IN_MINUTES;
                    string timeOutOverRide          = Sample.Azure.Common.Setting.SettingService.SaveAndReadBlobTimeoutInMinutes;
                    if (timeOutOverRide != null)
                    {
                        blobSaveTimeoutInMinutes = int.Parse(timeOutOverRide);
                    }
                    blobStorage.DefaultRequestOptions.ServerTimeout = TimeSpan.FromMinutes(blobSaveTimeoutInMinutes);

                    blobStorage.DefaultRequestOptions.RetryPolicy = new Microsoft.WindowsAzure.Storage.RetryPolicies.LinearRetry(TimeSpan.FromSeconds(1), 10);
                    Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer cloudBlobContainer = blobStorage.GetContainerReference(containerName);
                    cloudBlobContainer.CreateIfNotExists();

                    Microsoft.WindowsAzure.Storage.Blob.BlobContainerPermissions permissions = new Microsoft.WindowsAzure.Storage.Blob.BlobContainerPermissions();
                    permissions.PublicAccess = Microsoft.WindowsAzure.Storage.Blob.BlobContainerPublicAccessType.Off;
                    cloudBlobContainer.SetPermissions(permissions);

                    blobStorageDictionary.Add(containerName, blobStorage);
                    storageInitializedDictionary.Add(containerName, true);
                }
                catch (Exception ex)
                {
                    throw new Exception("Storage services initialization failure. "
                                        + "Check your storage account configuration settings. If running locally, "
                                        + "ensure that the Development Storage service is running. \n"
                                        + ex.Message);
                }
            } // lock
        }     // InitializeStorage
Пример #9
0
        } // PutQueueItem

        /// <summary>
        /// Creates the storage and gets a reference (once)
        /// </summary>
        private static void InitializeStorage(string queueContainer)
        {
            string containerName = queueContainer.ToString().ToLower(); // must be lower case!

            if (storageInitializedDictionary.ContainsKey(containerName) && storageInitializedDictionary[containerName] == true)
            {
                return;
            }

            lock (gate)
            {
                if (storageInitializedDictionary.ContainsKey(containerName) && storageInitializedDictionary[containerName] == true)
                {
                    return;
                }

                try
                {
                    Microsoft.WindowsAzure.Storage.Auth.StorageCredentials storageCredentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(
                        Sample.Azure.Common.Setting.SettingService.CloudStorageAccountName,
                        Sample.Azure.Common.Setting.SettingService.CloudStorageKey);

                    Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(storageCredentials,
                                                                                                                                               new Uri(Sample.Azure.Common.Setting.SettingService.CloudStorageBlobEndPoint),
                                                                                                                                               new Uri(Sample.Azure.Common.Setting.SettingService.CloudStorageQueueEndPoint),
                                                                                                                                               new Uri(Sample.Azure.Common.Setting.SettingService.CloudStorageTableEndPoint),
                                                                                                                                               new Uri(Sample.Azure.Common.Setting.SettingService.CloudStorageFileEndPoint));

                    Microsoft.WindowsAzure.Storage.Queue.CloudQueueClient queueStorage = storageAccount.CreateCloudQueueClient();

                    queueStorage.DefaultRequestOptions.RetryPolicy = new Microsoft.WindowsAzure.Storage.RetryPolicies.LinearRetry(TimeSpan.FromSeconds(DEFAULT_SAVE_QUEUE_RETRY_WAIT_IN_MILLISECONDS), DEFAULT_SAVE_QUEUE_RETRY_ATTEMPTS);

                    int    queueSaveTimeoutInMinutes = DEFAULT_SAVE_AND_READ_QUEUE_TIMEOUT_IN_MINUTES;
                    string timeOutOverRide           = Sample.Azure.Common.Setting.SettingService.SaveAndReadQueueTimeoutInMinutes;
                    if (timeOutOverRide != null)
                    {
                        queueSaveTimeoutInMinutes = int.Parse(timeOutOverRide);
                    }
                    queueStorage.DefaultRequestOptions.ServerTimeout = TimeSpan.FromMinutes(queueSaveTimeoutInMinutes);

                    queueStorage.DefaultRequestOptions.ServerTimeout = new TimeSpan(0, DEFAULT_SAVE_AND_READ_QUEUE_TIMEOUT_IN_MINUTES, 0);

                    Microsoft.WindowsAzure.Storage.Queue.CloudQueue cloudQueue = queueStorage.GetQueueReference(containerName);
                    cloudQueue.CreateIfNotExists();

                    queueStorageDictionary.Add(containerName, queueStorage);
                    storageInitializedDictionary.Add(containerName, true);
                }
                catch (Exception ex)
                {
                    throw new Exception("Storage services initialization failure. "
                                        + "Check your storage account configuration settings. If running locally, "
                                        + "ensure that the Development Storage service is running. \n"
                                        + ex.Message);
                }
            } // lock
        }     // InitializeStorage
 public string SetupConnection(string accountName, string accountKey, string containerName)
 {
     Microsoft.WindowsAzure.Storage.Auth.StorageCredentials creds = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(accountName, accountKey);
     _storageAccount  = new CloudStorageAccount(creds, false); // The boolean is use HTTPS or not
     _blobClient      = _storageAccount.CreateCloudBlobClient();
     _blobContainer   = _blobClient.GetContainerReference(containerName);
     _connectionSetup = true;
     return("\nConnected to " + accountName);
 }
Пример #11
0
        public AzureStorageFileSystem(string storageAccountName, string storageAccountKey, ILogger <AzureStorageFileSystem> logger)
        {
            _logger = logger;

            var cred = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(storageAccountName, storageAccountKey);

            _storageAccount = new CloudStorageAccount(cred, true);
            _blobClient     = _storageAccount.CreateCloudBlobClient();
        }
Пример #12
0
        public AzureBlobHelper(string accountName, string accessKey, string groupName, bool useHttps = false)
        {
            var credentials    = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(accountName, accessKey);
            var storageAccount = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(credentials, useHttps);

            this.CloudBlobClient = storageAccount.CreateCloudBlobClient();
            this.Container       = new ContainerLogic(this, groupName);
            this.Blob            = new BlobLogic(this, groupName);
        }
Пример #13
0
        private static CloudBlobContainer MakeContainer(BlobTransferInfo config)
        {
            var creds = new
                        Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(config.Account, config.Key);

            var blobStorage = new CloudBlobClient(new Uri(config.AccountUrl), creds);
            var container   = blobStorage.GetContainerReference(config.Container);

            return(container);
        }
Пример #14
0
        public static CloudStorageAccount GetCloudStorageAccount(string accountKey, string accountName)
        {
            CloudStorageAccount storageAccount;

            var credentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(accountName, accountKey);

            storageAccount = new CloudStorageAccount(credentials, true);

            return(storageAccount);
        }
Пример #15
0
        public ArticleRepository()
        {
            var creds = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials("saihadooptest", "6CUgPRjOYdAhbK6alofJzIqzqD8IbyBJl4iCVidaopcWJxsBUHoZSeFGsvsGwxTnsyWwPbctASp9UV+srt+cOw==");

            CloudStorageAccount storageAccount = new CloudStorageAccount(creds, true);
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            container = blobClient.GetContainerReference("newsfeed");

            container.CreateIfNotExists();
        }
        public static CloudBlobClient GetCloudBlobClient(string accountName, string accountKey)
        {
            if (BlobClient == null)
            {

                var credentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(accountName, accountKey);
                CloudStorageAccount azureStorageAccount = new CloudStorageAccount(credentials, true);
                BlobClient = azureStorageAccount.CreateCloudBlobClient();
            }

            return BlobClient;
        }
Пример #17
0
        private async void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            sendingPhoto = false;
            panicLed     = DeviceFactory.Build.Led(Pin.DigitalPin2);
            infoLed      = DeviceFactory.Build.Led(Pin.DigitalPin4);
            ranger       = DeviceFactory.Build.UltraSonicSensor(Pin.DigitalPin3);
            screen       = DeviceFactory.Build.RgbLcdDisplay();
            screen.SetText("setting up...");
            screen.SetBacklightRgb(0, 0, 200);
            // init mode -> Both Led's are on
            panicLed.ChangeState(SensorStatus.On);
            infoLed.ChangeState(SensorStatus.On);


            // init camera
            camera = new UsbCamera();
            var initWorked = await camera.InitializeAsync();

            // Something went wrong
            if (!initWorked || ranger.MeasureInCentimeters() == -1)
            {
                infoLed.ChangeState(SensorStatus.Off);
                screen.SetText("Camera or Sensor not connected!");
                screen.SetBacklightRgb(200, 0, 0);
                blink(panicLed);
                return;
            }

            // init photobackend

            Microsoft.WindowsAzure.Storage.Auth.StorageCredentials credentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(accountName, accountKey);
            //credentials.UpdateSASToken("?sv=2015-04-05&ss=b&srt=sco&sp=rwlac&se=2016-11-20T04:05:54Z&st=2016-11-12T20:05:54Z&spr=https,http&sig=B0zDabRXoO7LfWy5iACsn0sHOnWzvmmrDv8fAqITPgI%3D");
            CloudStorageAccount acc    = new CloudStorageAccount(credentials, true);
            CloudBlobClient     client = acc.CreateCloudBlobClient();

            container = client.GetContainerReference("picture-storage");

            previewElement.Source = camera.MediaCaptureInstance;

            await camera.StartCameraPreview();

            // init finished - turn off panic Led
            infoLed.ChangeState(SensorStatus.Off);
            panicLed.ChangeState(SensorStatus.Off);
            screen.SetText("");
            screen.SetBacklightRgb(0, 0, 0);

            DispatcherTimer mainThread = new DispatcherTimer();

            mainThread.Interval = TimeSpan.FromSeconds(0.5);
            mainThread.Tick    += run;
            mainThread.Start();
        }
        public AzureTableStorageRepository(IOptions <AzureStorageConfig> azureTableStorageConfig)
        {
            _azureTableStorageConfig = azureTableStorageConfig.Value;
            var storageCredentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(_azureTableStorageConfig.AccountName, _azureTableStorageConfig.AccountKey);

            _storageAccount = new CloudStorageAccount(storageCredentials, _azureTableStorageConfig.EndpointSuffix, _azureTableStorageConfig.UseHttps);
            _tableClient    = _storageAccount.CreateCloudTableClient();

            // Consider: Making a mail specific repo or passing the table name in during construction
            _table = _tableClient.GetTableReference(_azureTableStorageConfig.TableName);
            _table.CreateIfNotExistsAsync().Wait();
        }
Пример #19
0
        public Storage()
        {
            if (Creditals == null)
            {
                Creditals = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(StorageName, StorageKey);

                TableUri = new Uri($"https://{StorageName}.table.core.windows.net/");
                BlobUri  = new Uri($"https://{StorageName}.blob.core.windows.net/");
                QueueUri = new Uri($"https://{StorageName}.queue.core.windows.net/");

                tableClient = new CloudTableClient(TableUri, Creditals);
                blobClient  = new CloudBlobClient(BlobUri, Creditals);
                queueClient = new CloudQueueClient(QueueUri, Creditals);
            }
        }
        } // Run

        private static string GetSASToken(string containerName, string customerWhitelistIPAddressMinimum, string customerWhitelistIPAddressMaximum,
                                          int tokenExpireTimeInMinutes, string cloudAccountName, string cloudKey)
        {
            Microsoft.WindowsAzure.Storage.Auth.StorageCredentials storageCredentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(cloudAccountName, cloudKey);
            Microsoft.WindowsAzure.Storage.CloudStorageAccount     storageAccount     = null;

            if (cloudAccountName == "devstoreaccount1")
            {
                storageAccount = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(storageCredentials,
                                                                                        new Uri("http://127.0.0.1:10000/devstoreaccount1"),
                                                                                        new Uri("http://127.0.0.1:10001/devstoreaccount1"),
                                                                                        new Uri("http://127.0.0.1:10002/devstoreaccount1"),
                                                                                        new Uri("http://127.0.0.1:10003/devstoreaccount1"));
            }
            else
            {
                storageAccount = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(storageCredentials, true);
            }

            Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient blobStorage = storageAccount.CreateCloudBlobClient();
            blobStorage.DefaultRequestOptions.RetryPolicy = new Microsoft.WindowsAzure.Storage.RetryPolicies.LinearRetry(TimeSpan.FromSeconds(1), 10);
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = blobStorage.GetContainerReference(containerName);
            container.CreateIfNotExistsAsync().Wait();

            Microsoft.WindowsAzure.Storage.Blob.SharedAccessBlobPolicy policy = new Microsoft.WindowsAzure.Storage.Blob.SharedAccessBlobPolicy();
            policy.Permissions            = Microsoft.WindowsAzure.Storage.Blob.SharedAccessBlobPermissions.Write | Microsoft.WindowsAzure.Storage.Blob.SharedAccessBlobPermissions.List;
            policy.SharedAccessStartTime  = DateTimeOffset.UtcNow.AddMinutes(-5); // always do in the past to prevent errors
            policy.SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(tokenExpireTimeInMinutes);


            string sasToken = null;

            if (string.IsNullOrWhiteSpace(customerWhitelistIPAddressMinimum) ||
                string.IsNullOrWhiteSpace(customerWhitelistIPAddressMaximum) ||
                cloudAccountName == "devstoreaccount1")
            {
                sasToken = container.GetSharedAccessSignature(policy);
            }
            else
            {
                Microsoft.WindowsAzure.Storage.IPAddressOrRange iPAddressOrRange = new Microsoft.WindowsAzure.Storage.IPAddressOrRange(customerWhitelistIPAddressMinimum, customerWhitelistIPAddressMaximum);
                sasToken = container.GetSharedAccessSignature(policy, null, Microsoft.WindowsAzure.Storage.SharedAccessProtocol.HttpsOnly, iPAddressOrRange);
            }

            //  string url = "https://" + cloudAccountName + ".blob.core.windows.net" + sasToken;

            return(sasToken);
        }
 private void  CreateBackUpContainer()
 {
     //Maintain 5 backups at any point of time.  
     Microsoft.WindowsAzure.Storage.Auth.StorageCredentials scr = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(StorageName,StorageKey);
     CloudStorageAccount csa = new CloudStorageAccount(scr, false);
     CloudBlobClient blobClient = csa.CreateCloudBlobClient();
     IEnumerable<CloudBlobContainer> backupContainers = blobClient.ListContainers("v3-lucene0-automatedbackup-");
     if( backupContainers.Count() >= 5)
     {
         backupContainers.OrderBy(p => p.Properties.LastModified).ToList()[0].DeleteIfExistsAsync();
     }
     CloudBlobContainer destContainer = blobClient.GetContainerReference(destContainerName);
     //Delete and recreate.This is to make sure that rerunning creates a new container from scratch.
     destContainer.DeleteIfExists();
     destContainer.Create();            
 }
Пример #22
0
        private static async Task <CloudStorageAccount> GetCloudStorageAccountUsingMsiAsync(string accountName)
        {
            var accounts = await GetAccessibleStorageAccountsAsync();

            var account = accounts.FirstOrDefault(s => s.Name == accountName);

            if (account == null)
            {
                throw new Exception($"Azure Storage account with name: {accountName} not found in list of {accounts.Count} storage accounts.");
            }

            var key = await GetStorageAccountKeyAsync(account);

            var storageCredentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(account.Name, key);

            return(new CloudStorageAccount(storageCredentials, true));
        }
Пример #23
0
        /// <summary>
        /// Sets metadata for a particular container in Azure's cloud storage.
        /// </summary>
        /// <param name="containerName">Name of the container</param>
        /// <param name="key">Metadata key/name</param>
        /// <param name="val">Data to write</param>
        private void SetAzureMetaData(string containerName, string key, string val)
        {
            try
            {
                var credentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(
                    (string)AzureSettings.Default["StorageAccount"],
                    (string)AzureSettings.Default["StorageKey"]);

                var storageAccount = new CloudStorageAccount(credentials, true);
                var blobClient     = storageAccount.CreateCloudBlobClient();
                var containerRef   = blobClient.GetContainerReference(containerName);
                containerRef.CreateIfNotExists();
                containerRef.Metadata.Add(key, val);
                containerRef.SetMetadata();
            }
            catch (Exception err)
            {
                ShowError(err);
            }
        }
Пример #24
0
        private CloudStorageAccount GetStorageAccount(string storage, string location)
        {
            var storageAccountName = GetStorageAccountName(storage, location);
            var accessKey          = GetAccessKey(storage, location);

            if (String.IsNullOrEmpty(storageAccountName) ||
                String.IsNullOrEmpty(accessKey)
                )
            {
                return(null);
            }
            // Console.WriteLine($"Storage account = {storageAccountName}, key = {accessKey}");
            var authCredentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(
                storageAccountName, accessKey);
            var blobEndpoint   = GetBlobEndpoint(location);
            var endpointSuffix = getEndpointSuffix(blobEndpoint);

            CloudStorageAccount storageAccount = new CloudStorageAccount(authCredentials, endpointSuffix, true);

            return(storageAccount);
        }
Пример #25
0
        private static async Task <(IList <CloudStorageAccount>, CloudStorageAccount)> GetCloudStorageAccountsUsingMsiAsync(string accountName)
        {
            CloudStorageAccount defaultAccount = default;
            var accounts = await GetAccessibleStorageAccountsAsync();

            return(await Task.WhenAll(accounts.Select(GetCloudAccountFromStorageAccountInfo)), defaultAccount ?? throw new Exception($"Azure Storage account with name: {accountName} not found in list of {accounts.Count} storage accounts."));

            async Task <CloudStorageAccount> GetCloudAccountFromStorageAccountInfo(StorageAccountInfo account)
            {
                var key = await GetStorageAccountKeyAsync(account);

                var storageCredentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(account.Name, key);
                var storageAccount     = new CloudStorageAccount(storageCredentials, true);

                if (account.Name == accountName)
                {
                    defaultAccount = storageAccount;
                }
                return(storageAccount);
            }
        }
Пример #26
0
        /// <summary>
        /// Sets the connection to Azure
        /// Creates the table if it does not exist
        /// </summary>
        public BaseDataServiceContext()
        {
            storageCredentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(
                Sample.Azure.Common.Setting.SettingService.CloudStorageAccountName,
                Sample.Azure.Common.Setting.SettingService.CloudStorageKey);

            storageAccount = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(storageCredentials,
                                                                                    new Uri(Sample.Azure.Common.Setting.SettingService.CloudStorageBlobEndPoint),
                                                                                    new Uri(Sample.Azure.Common.Setting.SettingService.CloudStorageQueueEndPoint),
                                                                                    new Uri(Sample.Azure.Common.Setting.SettingService.CloudStorageTableEndPoint),
                                                                                    new Uri(Sample.Azure.Common.Setting.SettingService.CloudStorageFileEndPoint));

            tableClient = storageAccount.CreateCloudTableClient();

            cloudTable = tableClient.GetTableReference(this.TableName);

            if (!tableCreatedList.Contains(this.TableName))
            {
                cloudTable.CreateIfNotExists();
                tableCreatedList.Add(this.TableName);
            }
        }
Пример #27
0
        /// <summary>
        /// Upload a file to Azure's cloud storage if it does not already exist.
        /// </summary>
        /// <param name="containerName">Name of the container to upload the file to</param>
        /// <param name="filePath">Path to the file on disk</param>
        private void UploadFileIfNeeded(string containerName, string filePath)
        {
            var credentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(
                (string)AzureSettings.Default["StorageAccount"],
                (string)AzureSettings.Default["StorageKey"]);

            var storageAccount = new CloudStorageAccount(credentials, true);
            var blobClient     = storageAccount.CreateCloudBlobClient();
            var containerRef   = blobClient.GetContainerReference(containerName);

            containerRef.CreateIfNotExists();
            var blobRef = containerRef.GetBlockBlobReference(Path.GetFileName(filePath));

            var md5 = GetFileMd5(filePath);

            // if blob exists and md5 matches, there is no need to upload the file
            if (blobRef.Exists() && string.Equals(md5, blobRef.Properties.ContentMD5, StringComparison.InvariantCultureIgnoreCase))
            {
                return;
            }
            blobRef.Properties.ContentMD5 = md5;
            blobRef.UploadFromFile(filePath, FileMode.Open);
        }
        public static async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req, ILogger log, ExecutionContext context)
        {
            string Path = req.Query["Path"].ToString();
            var    sas  = "https://adsgofasttransientstg.blob.core.windows.net/" + Path + "?";

            foreach (var i in req.Query)
            {
                if (i.Key != "Path" && i.Key != "TargetSystemUidInPHI")
                {
                    sas += "&" + i.Key + "=" + i.Value;
                }
            }

            var cloudBlockBlob = new Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob(new Uri(sas));
            await cloudBlockBlob.UploadTextAsync("Hello World");

            //Write to Filelist table so that downstream tasks can be triggered efficiently
            var _storageCredentials  = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials("?sv=2018-03-28&tn=Filelist&sig=MFbvVgbNLs3UjqAPfU%2BYwQqxcTYwCPnNKCwCUp4XRmo%3D&se=2021-09-06T23%3A15%3A57Z&sp=au");
            var SourceStorageAccount = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(storageCredentials: _storageCredentials, accountName: "adsgofasttransientstg", endpointSuffix: "core.windows.net", useHttps: true);
            var client = SourceStorageAccount.CreateCloudTableClient();

            Microsoft.WindowsAzure.Storage.Table.CloudTable table = client.GetTableReference("Filelist");
            var _dict = new Dictionary <string, EntityProperty>();

            _dict.Add("FilePath", new EntityProperty(Path));
            var _tableEntity    = new DynamicTableEntity(DateTime.UtcNow.ToString("yyyy-MM-dd hh:mm"), Guid.NewGuid().ToString(), null, _dict);
            var _tableOperation = TableOperation.Insert(_tableEntity);

            try
            {
                await table.ExecuteAsync(_tableOperation);
            }
            catch (Microsoft.WindowsAzure.Storage.StorageException ex)
            {
            }
            return(new OkObjectResult(new { }));
        }
Пример #29
0
        public static CloudStorageAccount GetCloudStorageAccount(string accountKey, string accountName)
        {
            CloudStorageAccount storageAccount;

            var credentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(accountName, accountKey);
            storageAccount = new CloudStorageAccount(credentials, true);

            return storageAccount;
        }
Пример #30
0
        public static CloudBlobClient GetCloudBlobClient()
        {
            if (BlobClient == null)
            {
                if (IsDevUrl())
                {
                    CloudStorageAccount storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
                    BlobClient = storageAccount.CreateCloudBlobClient();
                }
                else
                {
                    var accountName = ConfigHelper.AzureAccountName;
                    string accountKey = ConfigHelper.AzureAccountKey;

                    var credentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(accountName, accountKey);
                    CloudStorageAccount azureStorageAccount = new CloudStorageAccount(credentials, true);
                    BlobClient = azureStorageAccount.CreateCloudBlobClient();

                    // retry policy.
                    // could do with a little work.
                    IRetryPolicy linearRetryPolicy = new LinearRetry(TimeSpan.FromSeconds(ConfigHelper.RetryAttemptDelayInSeconds), ConfigHelper.MaxRetryAttempts);
                    BlobClient.RetryPolicy = linearRetryPolicy;
                }
            }

            return BlobClient;
        }
Пример #31
0
        static int Process(string[] args)
        {
            Console.WriteLine(System.Reflection.Assembly.GetExecutingAssembly().GetName().Name + ".exe v" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version);
            log4net.Config.XmlConfigurator.Configure(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("BackupToUrlWithRotation.log4net.xml"));

            log.Debug("Parsing command line");
            try
            {
                config = Configuration.ParseFromCommandLine(args);
            }
            catch (Exception e)
            {
                log.ErrorFormat("Syntax error: {0:S}", e.Message);
                Configuration.Usage();

                return -1;
            }
            log.Debug("Parsing command line completed");

            log.DebugFormat("config == {0:S}", config.ToString());

            System.Data.SqlClient.SqlConnectionStringBuilder sb = new System.Data.SqlClient.SqlConnectionStringBuilder();
            sb.DataSource = config.DataSource;
            if (config.UserIntegrated)
                sb.IntegratedSecurity = true;
            else
            {
                sb.IntegratedSecurity = false;
                sb.UserID = config.Username;
                sb.Password = config.Password;
            }

            #region Azure connection setup
            Microsoft.WindowsAzure.Storage.Auth.StorageCredentials sc = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(config.StorageAccount, config.Secret);
            CloudStorageAccount csa = new CloudStorageAccount(sc, true);
            var client = csa.CreateCloudBlobClient();
            var container = client.GetContainerReference(config.Container);
            log.DebugFormat("Creating container {0:S} if not existent", config.Container);
            container.CreateIfNotExists();
            #endregion

            dbUtil = new DBUtil(sb.ConnectionString, SQLInfoHandler);
            string tempCred = Guid.NewGuid().ToString();

            log.InfoFormat("Creating temp credential: {0:S}", tempCred);
            dbUtil.CreateCredential(tempCred, config.StorageAccount, config.Secret);

            try
            {
                #region List databases to backup
                // now that we have the container, start with the backups
                log.DebugFormat("Getting database to backup list");
                var lDBs = dbUtil.ListDatabases(config.BackupType == BackupType.Log);
                log.InfoFormat("{0:N0} databases to backup", lDBs.Count);
                #endregion

                #region Backup to URL
                foreach (var db in lDBs)
                {
                    log.InfoFormat("Performing backup of {0:S}", db);

                    DateTime dt = DateTime.Now;

                    string strBackupName = string.Format("{0:S}_{1:S}_{2:S}.{3:S}",
                        dt.ToString(DATE_FORMAT),
                        dt.ToString(TIME_FORMAT),
                        db,
                        config.BackupType.ToString());

                    log.InfoFormat("Backup will be called {0:S}", strBackupName);

                    string backupUrl = string.Format("{0:S}{1:S}/{2:S}",
                        client.StorageUri.PrimaryUri.ToString(),
                        config.Container,
                        strBackupName);
                    log.DebugFormat("Full backup URI {0:S}", backupUrl);

                    //dbUtil.BackupDatabaseToUrl(db, backupUrl, tempCred);
                }
                #endregion
            }
            finally
            {
                // try to drop credential if possibile
                log.InfoFormat("Dropping temp credential: {0:S}", tempCred);
                dbUtil.DropCredential(tempCred);
            }

            #region Old backups deletion
            if (config.RetentionDays != -1)
            {
                foreach (var blob_uri in container.ListBlobs())
                {
                    CloudBlob cblob = new CloudBlob(blob_uri.Uri);
                    var blob = container.GetBlobReference(cblob.Name);

                    // try to parse to our format
                    log.DebugFormat("Parsing {0:S}", blob.Name);

                    DateTime d;
                    if (!DateTime.TryParseExact(blob.Name.Substring(0, 8), DATE_FORMAT, CultureInfo.InvariantCulture, DateTimeStyles.None, out d))
                    {
                        log.InfoFormat("Ingoring blob {0:S} because doesn't seem to have a valid date", blob.Name);
                        continue;
                    }

                    DateTime t;

                    if (!DateTime.TryParseExact(blob.Name.Substring(9, 6), TIME_FORMAT, CultureInfo.InvariantCulture, DateTimeStyles.None, out t))
                    {
                        log.InfoFormat("Ingoring blob {0:S} because doesn't seem to have a valid time", blob.Name);
                        continue;
                    }

                    log.DebugFormat("{0:S} and {1:S}", d.ToString(), t.ToString());

                    DateTime dfinal = d.AddHours(t.Hour).AddMinutes(t.Minute).AddSeconds(t.Second);
                    log.InfoFormat("Backup {0:S} was taken {1:S}", blob.Name, dfinal.ToString());

                    var delta = DateTime.Now - dfinal;

                    if (true) //(delta.TotalDays > config.RetentionDays)
                    {
                        log.WarnFormat("Backup {0:S} was taken {1:S} days ago. It will be deleted", blob.Name, delta.TotalDays.ToString());
                        try
                        {
                            blob.Delete();
                        }
                        catch (Exception e)
                        {
                            log.ErrorFormat("Failed to delete blob {0:S}: {1:S}", blob.Name, e.Message);
                        }
                        log.DebugFormat("Backup {0:S} deleted", blob.Name);
                    }
                }
            }
            else
            {
                log.WarnFormat("Backup deletion disabled because retention is set to {0:N0}.", config.RetentionDays.ToString());
            }
            #endregion


            log.InfoFormat("Program completed, exiting with 0");

            return 0;
        }
        public static CloudBlobClient GetCloudBlobClient(string accName, string accKey, bool isDev=false)
        {
            accountName = accName;
            accountKey = accKey;

            if (BlobClient == null)
            {
                if (isDev)
                {

                    CloudStorageAccount storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
                    BlobClient = storageAccount.CreateCloudBlobClient();

                }
                else
                {

                    var credentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(accountName, accountKey);
                    CloudStorageAccount azureStorageAccount = new CloudStorageAccount(credentials, true);
                    BlobClient = azureStorageAccount.CreateCloudBlobClient();

                    // retry policy.
                    // could do with a little work.
                    IRetryPolicy linearRetryPolicy = new LinearRetry( TimeSpan.FromSeconds( 5), 10);
                    BlobClient.RetryPolicy = linearRetryPolicy;

                }

            }

            return BlobClient;
        }