public Uri Upload(SubscriptionCloudCredentials credentials, string storageAccountName, string packageFile, string uploadedFileName)
        {
            var cloudStorage =
                new CloudStorageAccount(new StorageCredentials(storageAccountName, GetStorageAccountPrimaryKey(credentials, storageAccountName)), true);

            var blobClient = cloudStorage.CreateCloudBlobClient();
            var container = blobClient.GetContainerReference(OctopusPackagesContainerName);
            container.CreateIfNotExists();

            var permission = container.GetPermissions();
            permission.PublicAccess = BlobContainerPublicAccessType.Off;
            container.SetPermissions(permission);

            var fileInfo = new FileInfo(packageFile);

            var packageBlob = GetUniqueBlobName(uploadedFileName, fileInfo, container);
            if (packageBlob.Exists())
            {
                Log.VerboseFormat("A blob named {0} already exists with the same length, so it will be used instead of uploading the new package.",
                    packageBlob.Name);
                return packageBlob.Uri;
            }

            UploadBlobInChunks(fileInfo, packageBlob, blobClient);

            Log.Info("Package upload complete");
            return packageBlob.Uri;
        }
Example #2
0
        public CloudStorageAccount GetCurrentStorageAccount(IServiceManagement channel)
        {
            if (String.IsNullOrEmpty(CurrentStorageAccount))
            {
                return null;
            }

            if (_currentStorageAccount != null)
            {
                return _currentStorageAccount;
            }

            CloudStorageAccount currentStorage = null;
            using (new OperationContextScope((IContextChannel)channel))
            {
                var storageService = channel.GetStorageService(SubscriptionId, CurrentStorageAccount);
                var storageServiceKeys = channel.GetStorageKeys(SubscriptionId, CurrentStorageAccount);
                if (storageService != null && storageServiceKeys != null)
                {
                    string connectionString = General.BuildConnectionString("https", storageService.ServiceName, storageServiceKeys.StorageServiceKeys.Primary, storageService.StorageServiceProperties.Endpoints[0].Replace("http://", "https://"), storageService.StorageServiceProperties.Endpoints[2].Replace("http://", "https://"), storageService.StorageServiceProperties.Endpoints[1].Replace("http://", "https://"));
                    currentStorage = CloudStorageAccount.Parse(connectionString);
                }
            }

            _currentStorageAccount = currentStorage;
            return currentStorage;
        }
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");
            }
        }
        public TProduct(string affiliate, string code, int qty, bool force_lookup = false)
        {
            ProductCode = code;
            Qty = qty;

            cloud = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("AbundaStorage"));

            client = cloud.CreateCloudTableClient();

            products = client.GetTableReference("Products");
            products.CreateIfNotExists();

            using (var context = new DataBaseDataContext())
            {
                var aff = (from ev in context.Affiliates
                           where ev.code == affiliate
                           select ev).FirstOrDefault();

                merchantID = aff.MerchantID;
                marketplaceID = aff.MarketPlaceID;
                secretKey = aff.SecretKey;
                accessKey = aff.AccessKey;
            }

            var amzResults = PerformAmazonLookup();
        }
 static NDIAzureTableController()
 {
     _storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
     _tableClient = _storageAccount.CreateCloudTableClient();
     _table = _tableClient.GetTableReference("ndiparams");
     _retrieveOperation = TableOperation.Retrieve("VidParams", "LastVideo");
 }
 /// <summary>
 /// Constructs a new instance of the TableStorageAppender with default settings.
 /// </summary>
 /// <param name="storageAccount">The storage account to use</param>
 public TableStorageAppender(CloudStorageAccount storageAccount)
 {
     StorageAccount = storageAccount;
     TableName = "WADLogsTable";
     TransferIntervalInMinutes = 5;
     LogmarkerIntervalInMinutes = 30;
 }
		private static CloudQueue MakeQueue(CloudStorageAccount account)
		{
			var client = account.CreateCloudQueueClient();
			client.RetryPolicy = new ExponentialRetry(new TimeSpan(0, 0, 0, 2), 10);

			return client.GetQueueReference(QueueName);
		}
    public static bool DeleteBlob(string blobName)
    {
        //http://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-blobs/#delete-blobs

        string accountKey = "",
               accountName = "",
               blobContainer = "";
        try
        {
            CloudStorageAccount storageAccount = new CloudStorageAccount(new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(accountName, accountKey), true);

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

            // Retrieve reference to a previously created container.
            CloudBlobContainer container = blobClient.GetContainerReference(blobContainer);

            // Retrieve reference to a blob named "myblob.txt".
            CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName);

            // Delete the blob.
            blockBlob.Delete();
        }
        catch (Exception)
        {
            return false;
        }
        return true;
    }
Example #9
0
 public TableStorageOperations()
 {
     //var cloudStorageConnectionString =
     //    CloudConfigurationManager.GetSetting(CloudConfigurationName.DefaultStorageConnectionString);
     var cloudStorageConnectionString =
         ConfigurationManager.ConnectionStrings[CloudConfigurationName.DefaultStorageConnectionString].ConnectionString;
     _cloudStorageAccount = CloudStorageAccount.Parse(cloudStorageConnectionString);
Example #10
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);
            }
        }
        public CloudStorageContainer CreateCloudStorageContainer(CloudStorageAccount account, Guid publicKey, string alias, string containerName, string description)
        {
            if (account == null)
                throw new NullReferenceException("DefaultCloudStorageAccount cannot be null.");

            Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = CreateInternalAzureCloudStorageAccount(account.AccountName, account.AccountKey);
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = blobClient.GetContainerReference(containerName);

            container.Create();

            container.SetPermissions(new Microsoft.WindowsAzure.Storage.Blob.BlobContainerPermissions {
                PublicAccess = Microsoft.WindowsAzure.Storage.Blob.BlobContainerPublicAccessType.Off
            });

            // var containers = blobClient.ListContainers();

            CloudStorageContainer csContainer = this.context.CloudStorageContainers.Add( new CloudStorageContainer {
                CloudStorageAccountId = account.CloudStorageAccountId,
                PublicKey = publicKey,
                ProviderKey = containerName,
                ContainerName = containerName,
                Alias = alias,
                Description = description
            });

            context.SaveChanges();

            return csContainer;
        }
 private CloudStorageAccount Patch(CloudStorageAccount account)
 {
     ServicePointManager.FindServicePoint(account.BlobEndpoint).UseNagleAlgorithm = false;
     ServicePointManager.FindServicePoint(account.TableEndpoint).UseNagleAlgorithm = false;
     ServicePointManager.FindServicePoint(account.QueueEndpoint).UseNagleAlgorithm = false;
     return account;
 }
 protected override void Initialize(HttpControllerContext controllerContext)
 {
     base.Initialize(controllerContext);
     storageAccount = CloudStorageAccount.Parse("UseDevelopmentStorage=true");
     var tableClient = storageAccount.CreateCloudTableClient();
     table = tableClient.GetTableReference("people");
 }
Example #14
0
 public SensorAccess()
 {
     credentials = new StorageCredentials(_accountName, _key);
     storageAccount = new CloudStorageAccount(credentials, true);
     tableClient = storageAccount.CreateCloudTableClient();
     table = tableClient.GetTableReference("AccelerometerTable");
 }
Example #15
0
		/// <summary>
		/// A helper method that purges all of blob storage.
		/// </summary>
		/// <param name="azureAccount">The Azure account to clear out.</param>
		/// <returns>A task representing the asynchronous operation.</returns>
		private static async Task PurgeAllAsync(CloudStorageAccount azureAccount) {
			Requires.NotNull(azureAccount, "azureAccount");

			var blobClient = azureAccount.CreateCloudBlobClient();
			foreach (var container in blobClient.ListContainers()) {
				if (container.Name != "wad-control-container") {
					Console.WriteLine("\nContainer: {0}", container.Name);
					if (container.Name.StartsWith("unittests")) {
						container.Delete();
					} else {
						var blobs = await container.ListBlobsSegmentedAsync(
							container.Name,
							useFlatBlobListing: true,
							pageSize: 50,
							details: BlobListingDetails.Metadata,
							options: new BlobRequestOptions(),
							operationContext: null);
						foreach (var blob in blobs.Cast<ICloudBlob>()) {
							Console.WriteLine("\tBlob: {0} {1}", blob.Uri, blob.Metadata["DeleteAfter"]);
						}

						await Task.WhenAll(blobs.Cast<ICloudBlob>().Select(b => b.DeleteAsync()));
					}
				}
			}
		}
Example #16
0
        private void InitializeCors()
        {
            var tableServiceProperties = new ServiceProperties();
            var tableClient =
                new CloudStorageAccount(
                    new StorageCredentials(
                        CloudConfigurationManager.GetSetting("storageAccountName"),
                        CloudConfigurationManager.GetSetting("storageAccountKey")),
                    true).CreateCloudTableClient();

            tableServiceProperties.HourMetrics = null;
            tableServiceProperties.MinuteMetrics = null;
            tableServiceProperties.Logging = null;

            tableServiceProperties.Cors = new CorsProperties();
            tableServiceProperties.Cors.CorsRules.Add(new CorsRule()
            {
                AllowedHeaders = new List<string>() { "*" },
                AllowedMethods =  CorsHttpMethods.Get | CorsHttpMethods.Head ,
                //AllowedOrigins = new List<string>() { "http://ercenkbike.azurewebsites.net/" },
                AllowedOrigins = new List<string>() { "*" },
                ExposedHeaders = new List<string>() { "*" },
                MaxAgeInSeconds = 1800 // 30 minutes
            });

            tableClient.SetServiceProperties(tableServiceProperties);
        }
 public MediaLinkFactory(CloudStorageAccount currentStorage, string serviceName, string roleName, string containerName)
 {
     this.currentStorage = currentStorage;
     this.serviceName = serviceName;
     this.roleName = roleName;
     this.containerName = containerName;
     dateTimeCreated = DateTime.Now;
 }
    static GuestBookDataSource()
    {
      storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("DataConnectionString"));

      CloudTableClient cloudTableClient = storageAccount.CreateCloudTableClient();
      CloudTable table = cloudTableClient.GetTableReference("GuestBookEntry");
      table.CreateIfNotExists();
    }
 /// <summary>
 /// Initializes a new instance of the <see cref="AzureServerPackageRepository"/> class.
 /// </summary>
 /// <param name="packageLocator">The package locator.</param>
 /// <param name="packageSerializer">The package serializer.</param>
 public AzureServerPackageRepository(IPackageLocator packageLocator, IAzurePackageSerializer packageSerializer)
 {
     _packageLocator = packageLocator;
     _packageSerializer = packageSerializer;
     var azureConnectionString = CloudConfigurationManager.GetSetting("StorageConnectionString");
     _storageAccount = CloudStorageAccount.Parse(azureConnectionString);
     _blobClient = _storageAccount.CreateCloudBlobClient();
 }
Example #20
0
        public AzureBlobManager(string connectionString, string azureContainer)
        {
            _storageAccount = CloudStorageAccount.Parse(connectionString);

            if (string.IsNullOrEmpty("azureContainer"))
                throw new ArgumentException("azureContainer");
            _azureContainer = azureContainer;
        }
        public BlobOptimisticDataStore(CloudStorageAccount account, string containerName)
        {
            var blobClient = account.CreateCloudBlobClient();
            blobContainer = blobClient.GetContainerReference(containerName.ToLower());
            blobContainer.CreateIfNotExists();

            blobReferences = new Dictionary<string, CloudBlockBlob>();
        }
        static ViewDataSource()
        {
            storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["OgdiConfigConnectionString"]) ;

            CloudTableClient.CreateTablesFromModel(
                typeof(ViewDataContext),
                ConfigurationManager.AppSettings["TableStorageEndpoint"],
                storageAccount.Credentials);
        }
Example #23
0
		/// <summary>
		/// Initializes a new instance of the <see cref="AzureBlobStorage" /> class.
		/// </summary>
		/// <param name="account">The Azure account to use.</param>
		/// <param name="containerAddress">The name of the Azure blob container to use for uploaded blobs.</param>
		public AzureBlobStorage(CloudStorageAccount account, string containerAddress) {
			Requires.NotNull(account, "account");
			Requires.NotNullOrEmpty(containerAddress, "containerAddress");
			Requires.Argument(DesktopUtilities.IsValidBlobContainerName(containerAddress), "containerAddress", "Invalid container name.");

			this.account = account;
			this.client = this.account.CreateCloudBlobClient();
			this.container = this.client.GetContainerReference(containerAddress);
		}
Example #24
0
 static AzureStorage()
 {            
     //  _storageAccount.Credentials = credentials;
     //CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) => configSetter(Settings.GetSettings(configName)));
     //_storageAccount = CloudStorageAccount.FromConfigurationSetting(STORAGE_ACCOUNT_SETTING);
     _storageAccount = CloudStorageAccount.Parse(Settings.GetSettings(STORAGE_ACCOUNT_SETTING));
     _blobClient = _storageAccount.CreateCloudBlobClient();
     Trace.WriteLine("CloudStorage client initialized.");
 }
Example #25
0
 static StorageHelper()
 {
     if (!string.IsNullOrEmpty(SimpleSettings.StorageConnectionString))
     {
         StorageAccount = CloudStorageAccount.Parse(SimpleSettings.StorageConnectionString);
         BlobClient = StorageAccount.CreateCloudBlobClient();
         QueueClient = StorageAccount.CreateCloudQueueClient();
     }
 }
 public void SetUp()
 {
     var rnd = new Random();
     int number = rnd.Next(999999999);
     _tableReference = string.Format("DatabaseForUnitTesting{0}", number);
     _storageAccount = CloudStorageAccount.Parse(
         CloudConfigurationManager.GetSetting("StorageConnectionString"));
     _database = new AzureUserDatabase(_storageAccount);
 }
        public AzureServerPackageRepository(IPackageLocator packageLocator, 
                                            IAzurePackageSerializer packageSerializer,
                                            CloudStorageAccount storageAccount)
        {
            _packageLocator = packageLocator;
            _packageSerializer = packageSerializer;

            _storageAccount = storageAccount;
            _blobClient = _storageAccount.CreateCloudBlobClient();
        }
        public TableStorageSiteUrlRepository(IStorageQueueConfiguration configuration)
        {
            _storageAccount = CloudStorageAccount.Parse(configuration.GetConnectionString());

            _tableClient = _storageAccount.CreateCloudTableClient();
            _tableClient.GetTableReference(_tableName);
            _tableClient.GetTableReference(_tableName).CreateIfNotExists();

            _tableContext = _tableClient.GetTableServiceContext();
        }
Example #29
0
        private void SetAccount(string accountSonnectionString)
        {
            if (string.IsNullOrWhiteSpace(accountSonnectionString) && RoleEnvironment.IsAvailable)
                accountSonnectionString = CloudConfigurationManager.GetSetting("StorageConnectionString");

            if (string.IsNullOrWhiteSpace(accountSonnectionString) || accountSonnectionString.Contains("UseDevelopmentStorage=true"))
                cloudStorageAccount = CloudStorageAccount.DevelopmentStorageAccount;
            else
                cloudStorageAccount = CloudStorageAccount.Parse(accountSonnectionString);
        }
 /// <summary>
 /// Initializes the crypto by reading symmetric keys from the specified storage account. 
 /// This should be called once while the app is starting up. 
 /// A WorkerRole.OnStart() method or the Global.Application_Start() function would be a good place for that. 
 /// </summary>
 /// <param name="acct"></param>
 public static void Initialize(CloudStorageAccount acct)
 {
     //This is basically allowing only one keystore. First configured store wins. That could be annoying if you wanted to have multiple keystores,
     //but it's easier if you have multiple storage accounts and want them all to use the same keystore.
     //I figure the latter case is more likely, and it's the one I want anyway.
     if (keyStore == null)
     {
         keyStore = new AzureTableCryptoKeyStore(acct);
     }
 }
Example #31
0
        private Management.Compute.Models.Role CreatePersistentVMRole(PersistentVM persistentVM, CloudStorageAccount currentStorage)
        {
            var mediaLinkFactory = new MediaLinkFactory(currentStorage, this.ServiceName, persistentVM.RoleName);

            if (persistentVM.OSVirtualHardDisk.MediaLink == null && string.IsNullOrEmpty(persistentVM.OSVirtualHardDisk.DiskName))
            {
                persistentVM.OSVirtualHardDisk.MediaLink = mediaLinkFactory.Create();
            }

            foreach (var datadisk in persistentVM.DataVirtualHardDisks.Where(d => d.MediaLink == null && string.IsNullOrEmpty(d.DiskName)))
            {
                datadisk.MediaLink = mediaLinkFactory.Create();
            }

            var result = new Management.Compute.Models.Role
            {
                AvailabilitySetName = persistentVM.AvailabilitySetName,
                OSVirtualHardDisk   = Mapper.Map(persistentVM.OSVirtualHardDisk, new Management.Compute.Models.OSVirtualHardDisk()),
                RoleName            = persistentVM.RoleName,
                RoleSize            = persistentVM.RoleSize,
                RoleType            = persistentVM.RoleType,
                Label = persistentVM.Label,
                ProvisionGuestAgent         = persistentVM.ProvisionGuestAgent,
                ResourceExtensionReferences = persistentVM.ProvisionGuestAgent != null && persistentVM.ProvisionGuestAgent.Value ? Mapper.Map <List <ResourceExtensionReference> >(persistentVM.ResourceExtensionReferences) : null
            };

            if (persistentVM.DataVirtualHardDisks != null)
            {
                persistentVM.DataVirtualHardDisks.ForEach(c =>
                {
                    var dataDisk = Mapper.Map(c, new Microsoft.WindowsAzure.Management.Compute.Models.DataVirtualHardDisk());
                    dataDisk.LogicalUnitNumber = dataDisk.LogicalUnitNumber;
                    result.DataVirtualHardDisks.Add(dataDisk);
                });
            }

            if (persistentVM.ConfigurationSets != null)
            {
                PersistentVMHelper.MapConfigurationSets(persistentVM.ConfigurationSets).ForEach(c => result.ConfigurationSets.Add(c));
            }

            return(result);
        }
Example #32
0
 public AzureTable(CloudStorageAccount account, string tableName)
 {
     this._tableName = tableName;
     this._account   = account;
 }
Example #33
0
 /// <summary>
 /// Creates a <typeparam name="TSource" /> with the specified name <paramref name="sourceName"/> if it doesn't already exist.
 /// </summary>
 /// <param name="storageAccount">The storage account to create the container is</param>
 /// <param name="sourceName">The name of the source.</param>
 /// <param name="isPublic">Whether or not this source is publicly accessible.</param>
 protected abstract TSource CreateSource(CloudStorageAccount storageAccount, string sourceName, bool isPublic = true);
Example #34
0
        internal static string GetMountedPathFromBlob(
            string localCachePath,
            string containerName,
            string blobName,
            int driveSize,
            out CloudDrive elasticDrive)
        {

            DiagnosticsHelper.TraceInformation(
                "In mounting cloud drive for on {0} with {1}",
                containerName,
                blobName);

            var connectionString = RoleEnvironment.GetConfigurationSettingValue("DataConnectionString");
            connectionString = connectionString.Replace("DefaultEndpointsProtocol=https", "DefaultEndpointsProtocol=http");

            var storageAccount = CloudStorageAccount.Parse(connectionString);

            var blobClient = storageAccount.CreateCloudBlobClient();

            DiagnosticsHelper.TraceInformation("Get container");
            // this should be the name of your replset
            var driveContainer = blobClient.GetContainerReference(containerName);

            // create blob container (it has to exist before creating the cloud drive)
            try
            {
                driveContainer.CreateIfNotExist();
            }
            catch (StorageException e)
            {
                DiagnosticsHelper.TraceInformation(
                    "Container creation failed with {0} {1}",
                    e.Message,
                    e.StackTrace);
            }

            var blobUri = blobClient.GetContainerReference(containerName).GetPageBlobReference(blobName).Uri.ToString();
            DiagnosticsHelper.TraceInformation("Blob uri obtained {0}", blobUri);

            // create the cloud drive
            elasticDrive = storageAccount.CreateCloudDrive(blobUri);
            try
            {
                elasticDrive.CreateIfNotExist(driveSize);
            }
            catch (CloudDriveException e)
            {
                DiagnosticsHelper.TraceInformation(
                    "Drive creation failed with {0} {1}",
                    e.Message,
                    e.StackTrace);

            }

            DiagnosticsHelper.TraceInformation("Initialize cache");
            var localStorage = RoleEnvironment.GetLocalResource(localCachePath);

            CloudDrive.InitializeCache(localStorage.RootPath.TrimEnd('\\'),
                localStorage.MaximumSizeInMegabytes);

            // mount the drive and get the root path of the drive it's mounted as
            try
            {
                DiagnosticsHelper.TraceInformation(
                    "Trying to mount blob as azure drive");
                var driveLetter = elasticDrive.Mount(localStorage.MaximumSizeInMegabytes,
                    DriveMountOptions.None);
                DiagnosticsHelper.TraceInformation(
                    "Write lock acquired on azure drive, mounted as {0}",
                    driveLetter);
                return driveLetter;
            }
            catch (CloudDriveException e)
            {
                DiagnosticsHelper.TraceCritical(
                    "Failed to mount cloud drive with {0} {1}",
                    e.Message,
                    e.StackTrace);
                throw;
            }
        }
 public AzureService()
 {
     _account = CloudStorageAccount.Parse(Environment.GetEnvironmentVariable("AzureWebJobsStorage"));
 }
 public WriteRepository(CloudStorageAccount cloudStorageAccount)
 {
     // weird DI resolving bug in azure functions..
     _client = (cloudStorageAccount ?? CloudStorageAccount.DevelopmentStorageAccount).CreateCloudTableClient();
 }
Example #37
0
        public static async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");
            string responseMessage;

            string blobName = req.Query["blobName"];

            string  requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data        = JsonConvert.DeserializeObject(requestBody);

            blobName = blobName ?? data?.blobName;

            string sourceStorage = Environment.GetEnvironmentVariable("NewImageSourceStorage");

            CloudStorageAccount sourceStorageAccount = CloudStorageAccount.Parse(sourceStorage);

            string             imageContainerName = Environment.GetEnvironmentVariable("ImageContainerName");
            CloudBlobClient    imageBlobClient    = sourceStorageAccount.CreateCloudBlobClient();
            CloudBlobContainer imageContainer     = imageBlobClient.GetContainerReference(imageContainerName);
            CloudBlockBlob     imageBlob          = imageContainer.GetBlockBlobReference(blobName);

            ImageMetadata imageData = new ImageMetadata()
            {
                timestamp        = DateTime.Now,
                uploadedFileName = blobName,
                id = Path.GetFileNameWithoutExtension(blobName)
            };

            using (MemoryStream blobMemStream = new MemoryStream())
            {
                await imageBlob.DownloadToStreamAsync(blobMemStream);


                byte[] byteData = blobMemStream.ToArray();

                log.LogInformation("Image Byte Array:" + byteData);

                var client = new HttpClient();

                // Request headers - replace this example key with your valid Prediction-Key.

                client.DefaultRequestHeaders.Add("Prediction-Key", Environment.GetEnvironmentVariable("CustomVisionPredictionKey"));

                // Prediction URL - replace this example URL with your valid Prediction URL.

                string rootUrl   = Environment.GetEnvironmentVariable("CustomVisionRootUrl");
                string iteration = Environment.GetEnvironmentVariable("CustomVisionIteration");
                string url       = rootUrl + iteration + "/image";

                HttpResponseMessage response;

                // Request body. Try this sample with a locally stored image.

                using (var content = new ByteArrayContent(byteData))
                {
                    content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                    response = await client.PostAsync(url, content);

                    string responseBody = await response.Content.ReadAsStringAsync();

                    imageData = ProcessCustomVisionResults(responseBody, imageData);

                    Console.WriteLine(responseBody);
                }

                if (imageData.isValidatedIssue)
                {
                    log.LogInformation("Uploaded Image has been identified as an issue");

                    string             metaContainerName = Environment.GetEnvironmentVariable("ImageMetadataContainer");
                    CloudBlobClient    metaBlobClient    = sourceStorageAccount.CreateCloudBlobClient();
                    CloudBlobContainer metaContainer     = metaBlobClient.GetContainerReference(metaContainerName);
                    await metaContainer.CreateIfNotExistsAsync();

                    string         newMetaJson = System.Text.Json.JsonSerializer.Serialize <ImageMetadata>(imageData);
                    CloudBlockBlob newMetaBlob = metaContainer.GetBlockBlobReference(imageData.id + ".json");
                    await newMetaBlob.UploadTextAsync(newMetaJson);

                    responseMessage = imageData.id;
                }
                else
                {
                    log.LogInformation("Uploaded Image was not identified as an Issue. Removing image from upload container...");
                    await imageBlob.DeleteIfExistsAsync();

                    responseMessage = "-1";
                }
            }

            return(new OkObjectResult(responseMessage));
        }
 public AzureBlobProvider(IOptions <AzureBlobOptions> options)
 {
     _cloudStorageAccount = ParseConnectionString(options.Value.ConnectionString);
     _cloudBlobClient     = _cloudStorageAccount.CreateCloudBlobClient();
     _cdnUrl = options.Value.CdnUrl;
 }
Example #39
0
 public AzureBlobHelper(string connectionString )
 {
    this._storageAccount = CloudStorageAccount.Parse( connectionString );
    this._blobClient = this._storageAccount.CreateCloudBlobClient();
 }
Example #40
0
        internal void ExecuteCommandNewSM()
        {
            ServiceManagementProfile.Initialize();

            base.ExecuteCommand();

            AzureSubscription currentSubscription = CurrentContext.Subscription;

            if (CurrentDeploymentNewSM == null)
            {
                throw new ApplicationException(String.Format(Resources.CouldNotFindDeployment, ServiceName, Model.DeploymentSlotType.Production));
            }

            // Auto generate disk names based off of default storage account
            foreach (var datadisk in this.VM.DataVirtualHardDisks)
            {
                if (datadisk.MediaLink == null && string.IsNullOrEmpty(datadisk.DiskName))
                {
                    CloudStorageAccount currentStorage = currentSubscription.GetCloudStorageAccount();
                    if (currentStorage == null)
                    {
                        throw new ArgumentException(Resources.CurrentStorageAccountIsNotAccessible);
                    }

                    DateTime dateTimeCreated = DateTime.Now;
                    string   diskPartName    = VM.RoleName;

                    if (datadisk.DiskLabel != null)
                    {
                        diskPartName += "-" + datadisk.DiskLabel;
                    }

                    string vhdname      = string.Format("{0}-{1}-{2}-{3}-{4}-{5}.vhd", ServiceName, diskPartName, dateTimeCreated.Year, dateTimeCreated.Month, dateTimeCreated.Day, dateTimeCreated.Millisecond);
                    string blobEndpoint = currentStorage.BlobEndpoint.AbsoluteUri;

                    if (blobEndpoint.EndsWith("/") == false)
                    {
                        blobEndpoint += "/";
                    }

                    datadisk.MediaLink = new Uri(blobEndpoint + "vhds/" + vhdname);
                }

                if (VM.DataVirtualHardDisks.Count > 1)
                {
                    // To avoid duplicate disk names
                    System.Threading.Thread.Sleep(1);
                }
            }

            var parameters = new VirtualMachineUpdateParameters
            {
                AvailabilitySetName = VM.AvailabilitySetName,
                Label                       = VM.Label,
                OSVirtualHardDisk           = Mapper.Map <OSVirtualHardDisk>(VM.OSVirtualHardDisk),
                RoleName                    = VM.RoleName,
                RoleSize                    = VM.RoleSize,
                ProvisionGuestAgent         = VM.ProvisionGuestAgent,
                ResourceExtensionReferences = VM.ProvisionGuestAgent != null && VM.ProvisionGuestAgent.Value ? Mapper.Map <List <ResourceExtensionReference> >(VM.ResourceExtensionReferences) : null
            };

            if (parameters.OSVirtualHardDisk != null)
            {
                parameters.OSVirtualHardDisk.IOType = null;
            }

            if (VM.DataVirtualHardDisks != null)
            {
                parameters.DataVirtualHardDisks = new List <DataVirtualHardDisk>();
                VM.DataVirtualHardDisks.ForEach(c =>
                {
                    var dataDisk = Mapper.Map <DataVirtualHardDisk>(c);
                    dataDisk.LogicalUnitNumber = dataDisk.LogicalUnitNumber;
                    parameters.DataVirtualHardDisks.Add(dataDisk);
                });
                parameters.DataVirtualHardDisks.ForEach(d => d.IOType = null);
            }

            if (VM.ConfigurationSets != null)
            {
                PersistentVMHelper.MapConfigurationSets(VM.ConfigurationSets).ForEach(c => parameters.ConfigurationSets.Add(c));
            }

            if (VM.DataVirtualHardDisksToBeDeleted != null && VM.DataVirtualHardDisksToBeDeleted.Any())
            {
                var vmRole = CurrentDeploymentNewSM.Roles.First(r => r.RoleName == this.Name);
                if (vmRole != null)
                {
                    foreach (var dataDiskToBeDeleted in VM.DataVirtualHardDisksToBeDeleted)
                    {
                        int lun = dataDiskToBeDeleted.Lun;
                        try
                        {
                            this.ComputeClient.VirtualMachineDisks.DeleteDataDisk(
                                this.ServiceName,
                                CurrentDeploymentNewSM.Name,
                                vmRole.RoleName,
                                lun,
                                true);
                        }
                        catch (CloudException ex)
                        {
                            WriteWarning(string.Format(Resources.CannotDeleteVirtualMachineDataDiskForLUN, lun));

                            if (ex.Response.StatusCode != System.Net.HttpStatusCode.NotFound)
                            {
                                throw;
                            }
                        }
                    }
                }
            }

            ExecuteClientActionNewSM(
                parameters,
                CommandRuntime.ToString(),
                () => this.ComputeClient.VirtualMachines.Update(this.ServiceName, CurrentDeploymentNewSM.Name, this.Name, parameters));
        }
Example #41
0
        private async void send_Click(object sender, RoutedEventArgs e)
        {
            if (Feedback.Text != "")
            {
                if (NetworkInterface.GetIsNetworkAvailable())
                {
                    show_progress();
                    invisible();
                    loading_text.Text = "Connecting to the server ...";
                    string key           = connection.get_key();
                    string tableName     = "feedback";
                    string containerName = "public";
                    string counter       = await get_counter(key, containerName);

                    temp_obj temp = new temp_obj(Feedback.Text.ToString(), "Public", counter);
                    try
                    {
                        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(key);

                        CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

                        CloudTable table = tableClient.GetTableReference(tableName);
                        loading_text.Text = "Sending your Feedback ...";
                        await table.CreateIfNotExistsAsync();

                        TableOperation insertOperation = TableOperation.InsertOrReplace(temp);

                        await table.ExecuteAsync(insertOperation);

                        if (file != null)
                        {
                            await attach_picture(key, "feedback", counter);
                        }

                        loading_text.Text = "Completed ...";
                        hide_progress();
                        visible();
                        Feedback.Text = "";
                        popup("Thank you for your suggestion \nWe'll get back to you shortly. :)", "Message:");
                    }
                    catch
                    {
                        popup("Error in entering table", "Error:");
                        hide_progress();
                        visible();
                    }
                }
                else
                {
                    popup("WHOOPS! you are not connected to the internet. \nPlease check your internet connectionand try again", "Error:");
                    hide_progress();
                    visible();
                }
            }
            else
            {
                popup("Please enter the text ...", "Error:");
                hide_progress();
                visible();
            }
        }
        public async Task <string> FileUpload(long id, IFormFile[] files)
        {
            var claimsIdentity = User.Identity as ClaimsIdentity;
            var userId         = Convert.ToInt64(claimsIdentity.Claims.FirstOrDefault(claim => claim.Type == "Id").Value);
            var user           = _multiSourcePlaylistRepository.GetUser(userId);
            var filePath       = Path.Combine(
                "uploads",
                user.FileFolder);
            var uploads = Path.Combine(
                _environment.ContentRootPath,
                filePath);

            //string url = UriHelper.GetDisplayUrl(Request);//http://localhost:8080/api/fileupload/1
            //var urlParts = url.Split(new[] { "api/fileupload" }, StringSplitOptions.None);
            //var baseUrl = urlParts[0];
            var playlist = _multiSourcePlaylistRepository.GetPlaylist(id);

            var          allTracks = _multiSourcePlaylistRepository.GetAllTracks();
            int          lastOrder = 0;
            List <Track> temp      = new List <Track>();

            if (allTracks != null)
            {
                temp = allTracks.Where(y => y.Playlist.Id == id).ToList();
            }
            if (temp != null && temp.Any())
            {
                lastOrder = temp.OrderByDescending(x => x.Order).FirstOrDefault().Order + 1;
            }
            //@string.Format("{0}://{1}{2}{3}", Context.Request.Scheme, Context.Request.Host, Context.Request.Path, Context.Request.QueryString)
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                _configuration["Production:StorageConnectionString"]);
            CloudBlobClient    blobClient = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer container  = blobClient.GetContainerReference(user.FileFolder);

            // Ensure that the share exists.
            if (await container.ExistsAsync())
            {
                int bytesToMegaBytes = 1048576;
                var totalSize        = await isThereDiscSpaceInAzure(container);

                if ((totalSize) / bytesToMegaBytes > 10000)
                {
                    return("NO_DISC_SPACE");
                }
            }
            else
            {
                return("User container doesn't exists.");
            }
            foreach (var file in files)
            {
                try {
                    var            filename = file.FileName;
                    var            fullpath = Path.Combine(uploads, filename);
                    CloudBlockBlob blob     = container.GetBlockBlobReference(filename);
                    if (!await blob.ExistsAsync())
                    {
                        if (file.Length > 0)
                        {
                            using (var fileStream = file.OpenReadStream())
                            {
                                await blob.UploadFromStreamAsync(fileStream);
                            }
                        }
                    }
                    using (var fileStream = new FileStream(fullpath, FileMode.Create))
                    {
                        await file.CopyToAsync(fileStream);

                        fileStream.Flush();
                        fileStream.Dispose();
                    }

                    Track fileTrack = new Track();
                    fileTrack.Address  = file.FileName;
                    fileTrack.Playlist = playlist;
                    fileTrack.Type     = 5;
                    fileTrack.Order    = lastOrder;
                    fileTrack.Name     = getTrackName(fullpath);//hanki bändi ja kappale mp3 tiedoston metasta
                    _multiSourcePlaylistRepository.PostTrack(fileTrack);
                    ++lastOrder;
                    System.IO.File.Delete(fullpath);
                } catch (Exception ex) {
                    return(ex.Message);
                }
            }
            return("File was Uploaded");
        }
Example #43
0
        public static AzureBlobStorageFolder CreateContainerIfNotExists(CloudStorageAccount storageAccount, string containerName, BlobContainerPublicAccessType blobContainerPublicAccessType)
        {
            var containerReference = storageAccount.CreateCloudBlobClient().GetContainerReference(containerName);

            return(CreateContainerIfNotExists(containerReference, blobContainerPublicAccessType));
        }
 public void Connect(string connectionString)
 {
     _client = CloudStorageAccount.Parse(connectionString).CreateCloudBlobClient();
 }
Example #45
0
        public static AzureBlobStorageFolder UseExistingContainer(CloudStorageAccount storageAccount, string containerName)
        {
            var containerReference = storageAccount.CreateCloudBlobClient().GetContainerReference(containerName);

            return(UseExistingContainer(containerReference));
        }
Example #46
0
        /**
         * @brief Save log task processor. \n
         */
        public static bool RunLog(CBLoggers message)
        {
            if (globalVal.CloudBreadLoggerSetting != "")
            {
                if (string.IsNullOrEmpty(message.memberID))
                {
                    message.memberID = "";      /// in case of non-member triggered job
                }

                /// critical error case, save in ATS CloudBreadErrorLog
                if (message.Level.ToUpper() == "ERROR")
                {
                    try
                    {
                        /// Save error log on Azure Table Storage
                        {
                            /// Azure Table Storage connection retry policy
                            var tableStorageRetryPolicy         = new ExponentialRetry(TimeSpan.FromSeconds(2), 10);
                            CloudStorageAccount storageAccountT = CloudStorageAccount.Parse(globalVal.StorageConnectionString);
                            CloudTableClient    tableClient     = storageAccountT.CreateCloudTableClient();
                            tableClient.DefaultRequestOptions.RetryPolicy = tableStorageRetryPolicy;
                            CloudTable         table   = tableClient.GetTableReference("CloudBreadErrorLog");
                            CBATSMessageEntity Message = new CBATSMessageEntity(message.memberID, Guid.NewGuid().ToString());
                            Message.jobID = message.jobID;
                            Message.Date  = DateTimeOffset.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ");
                            //Message.Date = DateTimeOffset.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ");
                            Message.Thread    = message.Thread;
                            Message.Level     = message.Level;
                            Message.Logger    = message.Logger;
                            Message.Message   = message.Message;
                            Message.Exception = message.Exception;
                            TableOperation insertOperation = TableOperation.Insert(Message);
                            table.Execute(insertOperation);
                        }
                    }
                    catch (Exception)
                    {
                        /// Catch fail to log on database. Most case database connection or login fail issue.
                        throw;
                    }
                }
                else
                {
                    /// Regarding to web.config logger settting, save logs on specific storage
                    try
                    {
                        switch (globalVal.CloudBreadLoggerSetting)
                        {
                        case "SQL":
                            /// Save log on SQL
                            string strQuery = string.Format("insert into dbo.CloudBreadAdminLog(memberid, jobID, [Thread], [Level], [Logger], [Message], [Exception]) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}')",
                                                            message.memberID,
                                                            message.jobID,
                                                            message.Thread,
                                                            message.Level,
                                                            message.Logger,
                                                            message.Message,
                                                            message.Exception
                                                            );

                            /// Database connection retry policy
                            RetryPolicy   retryPolicy = new RetryPolicy <SqlAzureTransientErrorDetectionStrategy>(globalVal.conRetryCount, TimeSpan.FromSeconds(globalVal.conRetryFromSeconds));
                            SqlConnection connection  = new SqlConnection(globalVal.DBConnectionString);
                            {
                                connection.OpenWithRetry(retryPolicy);
                                SqlCommand command  = new SqlCommand(strQuery, connection);
                                int        rowcount = command.ExecuteNonQueryWithRetry(retryPolicy);
                                connection.Close();
                                break;
                            }

                        case "ATS":
                            /// Save log on Azure Table Storage
                        {
                            /// Azure Table Storage connection retry policy
                            var tableStorageRetryPolicy         = new ExponentialRetry(TimeSpan.FromSeconds(2), 10);
                            CloudStorageAccount storageAccountT = CloudStorageAccount.Parse(globalVal.StorageConnectionString);
                            CloudTableClient    tableClient     = storageAccountT.CreateCloudTableClient();
                            tableClient.DefaultRequestOptions.RetryPolicy = tableStorageRetryPolicy;
                            CloudTable         table   = tableClient.GetTableReference("CloudBreadAdminLog");
                            CBATSMessageEntity Message = new CBATSMessageEntity(message.memberID, Guid.NewGuid().ToString());               //memberid를 파티션키로 쓴다.
                            Message.jobID     = message.jobID;
                            Message.Date      = DateTimeOffset.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ");
                            Message.Thread    = message.Thread;
                            Message.Level     = message.Level;
                            Message.Logger    = message.Logger;
                            Message.Message   = message.Message;
                            Message.Exception = message.Exception;
                            TableOperation insertOperation = TableOperation.Insert(Message);
                            table.Execute(insertOperation);
                            break;
                        }

                        /// AQS does not need CloudBread-Admin-Web
                        //case "AQS":
                        //    /// Save log on Azure Queue Storage
                        //    {
                        //        /// Azure Queue Storage connection retry policy
                        //        var queueStorageRetryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(2), 10);
                        //        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(globalVal.StorageConnectionString);
                        //        CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
                        //        queueClient.DefaultRequestOptions.RetryPolicy = queueStorageRetryPolicy;
                        //        CloudQueue queue = queueClient.GetQueueReference("messagestoadminlog");      /// must be lower case
                        //        CBATSMessageEntity Message = new CBATSMessageEntity(message.memberID, Guid.NewGuid().ToString());
                        //        Message.jobID = message.jobID;
                        //        Message.Date = DateTimeOffset.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ");
                        //        Message.Thread = message.Thread;
                        //        Message.Level = message.Level;
                        //        Message.Logger = message.Logger;
                        //        Message.Message = message.Message;
                        //        Message.Exception = message.Exception;
                        //        CloudQueueMessage Qmessage = new CloudQueueMessage(JsonConvert.SerializeObject(Message));
                        //        queue.AddMessage(Qmessage);
                        //        break;
                        //    }

                        /// redis does not need CloudBread-Admin-Web
                        //case "redis":
                        //    /// todolist - save log on Azure Redis Cache
                        //    /// yyyymmdd:memberid:Controller:GUID
                        //    {
                        //        string redisKey = "";
                        //        string redisVal = "";
                        //        message.Date = DateTimeOffset.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ");
                        //        redisKey = DateTime.Now.ToUniversalTime().ToString("yyyyMMddHHmm") + ":" + message.memberID + ":" + message.Logger + ":" + Guid.NewGuid().ToString();   // guid - too long key size
                        //        redisVal = JsonConvert.SerializeObject(message);
                        //        CBRedis.saveRedisLog(redisKey, redisVal, globalVal.CloudBreadGameLogExpTimeDays);
                        //    }
                        //    break;

                        //case "DocDB":
                        //    /// @todo save log data on Azure DocumentDB
                        //    break;

                        default:
                            /// case do nothing
                            break;
                        }
                    }
                    catch (Exception)
                    {
                        /// catch save log error here.
                        throw;
                    }
                }
            }
            return(true);
        }
Example #47
0
        static void AzureStorageTableExamples()
        {
            // Getting configuration from the App.config file and get access to the CloudStorageAccount.
            // Azure access Key should is stored in the App.config
            String storageKeyValue = CloudConfigurationManager.GetSetting("AzureStorageAccount");

            // Just in case to check whether reading the correct value
            Console.WriteLine("Storage Account Key Used" + storageKeyValue);

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageKeyValue);

            // Create the table client.
            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

            // Retrieve a reference to the table.
            CloudTable table = tableClient.GetTableReference("tenant");

            table.CreateIfNotExists();

            // Lesson 1 Table Insert

            TenantEntity tenant1 = new TenantEntity("Neson", "De Jesus", "Canada");

            tenant1.Email       = "*****@*****.**";
            tenant1.PhoneNumber = "911-911-9111";

            TableOperation tOper = TableOperation.Insert(tenant1);

            // Lesson 2 Batch Insert
            TableBatchOperation batchInsertOperation = new TableBatchOperation();

            TenantEntity tenant2 = new TenantEntity("Vivek", "Menon", "USA");

            tenant2.Email       = "*****@*****.**";
            tenant2.PhoneNumber = "911-911-9111";

            batchInsertOperation.Insert(tenant2);

            TenantEntity tenant3 = new TenantEntity("Sanders", "John", "USA");

            tenant3.Email       = "*****@*****.**";
            tenant3.PhoneNumber = "911-911-9111";

            batchInsertOperation.Insert(tenant3);

            table.ExecuteBatch(batchInsertOperation);

            // Lession 3: Accesing the table data
            TableQuery <TenantEntity> query = new TableQuery <TenantEntity>().Where(TableQuery.GenerateFilterCondition("PartitionKey",
                                                                                                                       QueryComparisons.Equal, "USA"));

            foreach (TenantEntity tenant in table.ExecuteQuery(query))
            {
                Console.WriteLine("{0}, {1}, {2}, {3}", tenant.firstName, tenant.lastName, tenant.Email, tenant.PhoneNumber);
            }


            // Lesson 4: Multiple condition in where clause
            TableQuery <TenantEntity> queryMultipleWhereContidion = new TableQuery <TenantEntity>().Where(
                TableQuery.CombineFilters(
                    TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "USA"),
                    TableOperators.And,
                    TableQuery.GenerateFilterCondition("lastName", QueryComparisons.Equal, "Sanders")));

            foreach (TenantEntity tenant in table.ExecuteQuery(queryMultipleWhereContidion))
            {
                Console.WriteLine("{0}, {1}, {2}, {3}", tenant.firstName, tenant.lastName, tenant.Email, tenant.PhoneNumber);
            }


            // Lesson 5: Single record retrieval
            TableOperation retrieveOperation = TableOperation.Retrieve <TenantEntity>("Sanders", "John");
            TableResult    retrievedResult   = table.Execute(retrieveOperation);

            if (retrievedResult.Result != null)
            {
                Console.WriteLine(((TenantEntity)retrievedResult.Result).PhoneNumber);
            }
            else
            {
                Console.WriteLine("The phone number could not be retrieved.");
            }
            Console.ReadLine();
        }
Example #48
0
        public void NewAzureVMProcess()
        {
            SubscriptionData    currentSubscription = this.GetCurrentSubscription();
            CloudStorageAccount currentStorage      = null;

            try
            {
                currentStorage = currentSubscription.GetCurrentStorageAccount();
            }
            catch (ServiceManagementClientException) // couldn't access
            {
                throw new ArgumentException(Resources.CurrentStorageAccountIsNotAccessible);
            }
            if (currentStorage == null) // not set
            {
                throw new ArgumentException(Resources.CurrentStorageAccountIsNotSet);
            }

            bool serviceExists = DoesCloudServiceExist(this.ServiceName);

            if (!string.IsNullOrEmpty(this.Location))
            {
                if (serviceExists)
                {
                    throw new ApplicationException(Resources.ServiceExistsLocationCanNotBeSpecified);
                }
            }

            if (!string.IsNullOrEmpty(this.AffinityGroup))
            {
                if (serviceExists)
                {
                    throw new ApplicationException(Resources.ServiceExistsAffinityGroupCanNotBeSpecified);
                }
            }

            if (!serviceExists)
            {
                using (new OperationContextScope(Channel.ToContextChannel()))
                {
                    try
                    {
                        //Implicitly created hosted service2012-05-07 23:12

                        // Create the Cloud Service when
                        // Location or Affinity Group is Specified
                        // or VNET is specified and the service doesn't exist
                        var chsi = new CreateHostedServiceInput
                        {
                            AffinityGroup = this.AffinityGroup,
                            Location      = this.Location,
                            ServiceName   = this.ServiceName,
                            Description   = String.Format("Implicitly created hosted service{0}", DateTime.Now.ToUniversalTime().ToString("yyyy-MM-dd HH:mm")),
                            Label         = this.ServiceName
                        };

                        ExecuteClientAction(chsi, CommandRuntime + Resources.QuickVMCreateCloudService, s => this.Channel.CreateHostedService(s, chsi));
                    }

                    catch (ServiceManagementClientException ex)
                    {
                        this.WriteErrorDetails(ex);
                        return;
                    }
                }
            }

            if (ParameterSetName.Equals("Windows", StringComparison.OrdinalIgnoreCase))
            {
                if (WinRMCertificate != null)
                {
                    if (!CertUtils.HasExportablePrivateKey(WinRMCertificate))
                    {
                        throw new ArgumentException(Resources.WinRMCertificateDoesNotHaveExportablePrivateKey);
                    }
                    var operationDescription = string.Format(Resources.QuickVMUploadingWinRMCertificate, CommandRuntime, WinRMCertificate.Thumbprint);
                    var certificateFile      = CertUtils.Create(WinRMCertificate);
                    ExecuteClientActionInOCS(null, operationDescription, s => this.Channel.AddCertificates(s, this.ServiceName, certificateFile));
                }

                if (X509Certificates != null)
                {
                    var certificateFilesWithThumbprint = from c in X509Certificates
                                                         select new
                    {
                        c.Thumbprint,
                        CertificateFile = CertUtils.Create(c, this.NoExportPrivateKey.IsPresent)
                    };
                    foreach (var current in certificateFilesWithThumbprint.ToList())
                    {
                        var operationDescription = string.Format(Resources.QuickVMUploadingCertificate, CommandRuntime, current.Thumbprint);
                        ExecuteClientActionInOCS(null, operationDescription, s => this.Channel.AddCertificates(s, this.ServiceName, current.CertificateFile));
                    }
                }
            }

            var vm = CreatePersistenVMRole(currentStorage);

            // If the current deployment doesn't exist set it create it
            if (CurrentDeployment == null)
            {
                using (new OperationContextScope(Channel.ToContextChannel()))
                {
                    try
                    {
                        var deployment = new Deployment
                        {
                            DeploymentSlot = DeploymentSlotType.Production,
                            Name           = this.ServiceName,
                            Label          = this.ServiceName,
                            RoleList       = new RoleList {
                                vm
                            },
                            VirtualNetworkName = this.VNetName
                        };

                        if (this.DnsSettings != null)
                        {
                            deployment.Dns = new DnsSettings {
                                DnsServers = new DnsServerList()
                            };
                            foreach (DnsServer dns in this.DnsSettings)
                            {
                                deployment.Dns.DnsServers.Add(dns);
                            }
                        }

                        var operationDescription = string.Format(Resources.QuickVMCreateDeploymentWithVM, CommandRuntime, vm.RoleName);
                        ExecuteClientAction(deployment, operationDescription, s => this.Channel.CreateDeployment(s, this.ServiceName, deployment));

                        if (WaitForBoot.IsPresent)
                        {
                            WaitForRoleToBoot(vm.RoleName);
                        }
                    }

                    catch (ServiceManagementClientException ex)
                    {
                        if (ex.HttpStatus == HttpStatusCode.NotFound)
                        {
                            throw new Exception(Resources.ServiceDoesNotExistSpecifyLocationOrAffinityGroup);
                        }
                        else
                        {
                            this.WriteErrorDetails(ex);
                        }
                        return;
                    }

                    _createdDeployment = true;
                }
            }
            else
            {
                if (VNetName != null || DnsSettings != null)
                {
                    WriteWarning(Resources.VNetNameOrDnsSettingsCanOnlyBeSpecifiedOnNewDeployments);
                }
            }


            // Only create the VM when a new VM was added and it was not created during the deployment phase.
            if ((_createdDeployment == false))
            {
                using (new OperationContextScope(Channel.ToContextChannel()))
                {
                    try
                    {
                        var operationDescription = string.Format(Resources.QuickVMCreateVM, CommandRuntime, vm.RoleName);
                        ExecuteClientAction(vm, operationDescription, s => this.Channel.AddRole(s, this.ServiceName, this.ServiceName, vm));
                        if (WaitForBoot.IsPresent)
                        {
                            WaitForRoleToBoot(vm.RoleName);
                        }
                    }
                    catch (ServiceManagementClientException ex)
                    {
                        this.WriteErrorDetails(ex);
                        return;
                    }
                }
            }
        }
Example #49
0
        static void AzureStorageBlobExamples()
        {
            // Getting configuration from the App.config file and get access to the CloudStorageAccount.
            // Azure access Key should is stored in the App.config
            String storageKeyValue = CloudConfigurationManager.GetSetting("AzureStorageAccount");

            // Just in case to check whether reading the correct value
            Console.WriteLine("Storage Account Key Used" + storageKeyValue);

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageKeyValue);

            // Getting the Azure Client
            CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();

            // Getting reference for the container
            CloudBlobContainer cloudContainer = cloudBlobClient.GetContainerReference("images");

            // Create the container (folder) if does not exists
            cloudContainer.CreateIfNotExists();

            // Get the reference for the Blob in Azure
            CloudBlockBlob blobCloud = cloudContainer.GetBlockBlobReference("photo.png");


            // Learning 1: Copy a file from local path in to Azure Blob storage

            //Open a local file and upload to the Azure
            using (var fileStream = System.IO.File.OpenRead(@"C:\Users\Administrator\Pictures\photo.jpg"))
            {
                blobCloud.UploadFromStream(fileStream);
                Console.WriteLine("File is uploaded to the cloud");
            }

            // Learning 2: Download a blob from Azure to the local host

            // Just copy a blob from cloud to a local file. Assume the same file which has been uploaded in the previous step
            blobCloud.DownloadToFile(@"C:\Users\Administrator\Pictures\photocopy.jpg", FileMode.CreateNew);

            // Learning 3: List all blob objects URI from a container

            //List all blobs in a container
            var blobs = cloudContainer.ListBlobs();

            foreach (var blob in blobs)
            {
                Console.Write(blob.Uri);
            }

            // Learning 4: List properties of a continer (Folder) and listing metadata of a container

            cloudContainer.FetchAttributes();
            Console.WriteLine(cloudContainer.Properties.LastModified);
            Console.WriteLine(cloudContainer.Properties.ETag);
            var metaData = cloudContainer.Metadata;

            foreach (var metaDataItem in metaData)
            {
                Console.Write("Key " + metaDataItem.Key + " & ");
                Console.WriteLine("Value" + metaDataItem.Value);
            }

            // Learning 5: Setting metaData for a container

            // Method 1
            cloudContainer.Metadata.Add("SampleKey", "SampleValue");
            // Method 2
            cloudContainer.Metadata["SecondSample"] = "Second Value";

            // Dont ask me why FetchAttributes() and SetMetadata() ! why not SetAttributes() or GetMetaData? Microsoft Way!
            cloudContainer.SetMetadata();


            // Learning 6: Setting permission for a container
            BlobContainerPermissions permissions = cloudContainer.GetPermissions();

            Console.WriteLine("Container permission " + permissions.PublicAccess.ToString());
            foreach (var sharedAccessPolicy in permissions.SharedAccessPolicies)
            {
                Console.WriteLine(sharedAccessPolicy.Key.ToString() + " = " + sharedAccessPolicy.Value.ToString());
            }
            // In order to as per the parent container
            permissions.PublicAccess = BlobContainerPublicAccessType.Container;
            // In order to remove the public access
            permissions.PublicAccess = BlobContainerPublicAccessType.Off;
            //Finally set the permission
            cloudContainer.SetPermissions(permissions);



            // Learning 7: Azure copy from one blob to another

            // Create a new Block Blog Reference
            CloudBlockBlob copyBlob = cloudContainer.GetBlockBlobReference("photo-copy.jpg");

            // Copy the original - blobCloud to the copyBlob
            copyBlob.StartCopy(new Uri(blobCloud.Uri.AbsoluteUri));


            // Learning 8: Copy all blobs from one container to another
            CloudBlobContainer sourceContainer = cloudContainer;
            CloudBlobContainer targetContainer = cloudBlobClient.GetContainerReference("newimages");

            targetContainer.CreateIfNotExists();
            foreach (var blob in sourceContainer.ListBlobs())
            {
                var sourceBlob = blob as CloudBlob;
                Console.WriteLine("Source Blob " + sourceBlob.Name);
                CloudBlockBlob newBlob = targetContainer.GetBlockBlobReference(sourceBlob.Name);
                newBlob.StartCopy(new Uri(blob.Uri.AbsoluteUri));
            }


            // Learning 9: Rename the blob
            CloudBlockBlob sourceBlockBlob = cloudContainer.GetBlockBlobReference("photo-copy.jpg");
            CloudBlockBlob targetBlockBlob = cloudContainer.GetBlockBlobReference("copy-photo.jpg");

            targetBlockBlob.StartCopy(new Uri(sourceBlockBlob.Uri.AbsoluteUri));
            while (targetBlockBlob.CopyState.Status == CopyStatus.Pending)
            {
                // Sleep for 3 seconds
                System.Threading.Thread.Sleep(2000);
            }
            sourceBlockBlob.Delete();


            // Learning 10: Appending to a blob
            DateTime           date         = DateTime.Today;
            CloudBlobContainer logContainer = cloudBlobClient.GetContainerReference("logs");
            CloudAppendBlob    logBlog      = logContainer.GetAppendBlobReference(string.Format("{0}{1}", date.ToString("yyyyMMdd"), ".log"));

            logContainer.CreateIfNotExists();

            // If the append blog does not exists, create one
            if (!logBlog.Exists())
            {
                logBlog.CreateOrReplace();
            }

            // AppendText
            logBlog.AppendText(string.Format("{0} : Azure is rocking in the cloud space at ", date.ToString("HH:MM:ss")));
            // Similar to the AppendText, there are
            // logBlog.AppendBlock
            // logBlog.AppendFromByteArray
            // logBlog.AppendFromFile
            // logBlog.AppendFromStream

            // Finally display the content of the log file.
            Console.WriteLine(logBlog.DownloadText());

            // Learning 11: Multiple Chunk of file upload to Azure
            AzureStorageReference.Program.uploadLargeFiles(cloudBlobClient, @"C:\Users\Administrator\Pictures");


            //Learning 12: Upload using Async

            AsyncCallback  callBack  = new AsyncCallback(x => Console.WriteLine("Copy Async Completed"));
            CloudBlockBlob copyAsync = cloudContainer.GetBlockBlobReference("newphoto.png");

            copyAsync.BeginStartCopy(blobCloud, callBack, null);

            Console.WriteLine("Press any key to continue");
            Console.ReadKey();
        }
 public TableStorage()
 {
     _storageAccount = CloudStorageAccount.Parse(Environment.GetEnvironmentVariable("akka-iot-telemetry-storage"));
     _retryPolicy    = new ExponentialRetry(TimeSpan.FromSeconds(5), 3);
 }
Example #51
0
        private PersistentVMRole CreatePersistenVMRole(CloudStorageAccount currentStorage)
        {
            var vm = new PersistentVMRole
            {
                AvailabilitySetName  = AvailabilitySetName,
                ConfigurationSets    = new Collection <ConfigurationSet>(),
                DataVirtualHardDisks = new Collection <DataVirtualHardDisk>(),
                RoleName             = String.IsNullOrEmpty(Name) ? ServiceName : Name, // default like the portal
                RoleSize             = String.IsNullOrEmpty(InstanceSize) ? null : InstanceSize,
                RoleType             = "PersistentVMRole",
                Label             = ServiceName,
                OSVirtualHardDisk = new OSVirtualHardDisk
                {
                    DiskName        = null,
                    SourceImageName = ImageName,
                    MediaLink       = string.IsNullOrEmpty(MediaLocation) ? null : new Uri(MediaLocation),
                    HostCaching     = HostCaching
                }
            };

            if (vm.OSVirtualHardDisk.MediaLink == null && String.IsNullOrEmpty(vm.OSVirtualHardDisk.DiskName))
            {
                var mediaLinkFactory = new MediaLinkFactory(currentStorage, this.ServiceName, vm.RoleName);
                vm.OSVirtualHardDisk.MediaLink = mediaLinkFactory.Create();
            }


            var netConfig = CreateNetworkConfigurationSet();

            if (ParameterSetName.Equals("Windows", StringComparison.OrdinalIgnoreCase))
            {
                var windowsConfig = new WindowsProvisioningConfigurationSet
                {
                    AdminUsername = this.AdminUsername,
                    AdminPassword = Password,
                    ComputerName  =
                        string.IsNullOrEmpty(Name) ? ServiceName : Name,
                    EnableAutomaticUpdates    = true,
                    ResetPasswordOnFirstLogon = false,
                    StoredCertificateSettings = CertUtils.GetCertificateSettings(this.Certificates, this.X509Certificates),
                    WinRM = GetWinRmConfiguration()
                };

                netConfig.InputEndpoints.Add(new InputEndpoint {
                    LocalPort = 3389, Protocol = "tcp", Name = "RemoteDesktop"
                });
                if (!this.NoWinRMEndpoint.IsPresent && !this.DisableWinRMHttps.IsPresent)
                {
                    netConfig.InputEndpoints.Add(new InputEndpoint {
                        LocalPort = WinRMConstants.HttpsListenerPort, Protocol = "tcp", Name = WinRMConstants.EndpointName
                    });
                }
                vm.ConfigurationSets.Add(windowsConfig);
                vm.ConfigurationSets.Add(netConfig);
            }
            else
            {
                var linuxConfig = new LinuxProvisioningConfigurationSet
                {
                    HostName     = string.IsNullOrEmpty(this.Name) ? this.ServiceName : this.Name,
                    UserName     = this.LinuxUser,
                    UserPassword = this.Password,
                    DisableSshPasswordAuthentication = false
                };

                if (this.SSHKeyPairs != null && this.SSHKeyPairs.Count > 0 ||
                    this.SSHPublicKeys != null && this.SSHPublicKeys.Count > 0)
                {
                    linuxConfig.SSH = new LinuxProvisioningConfigurationSet.SSHSettings
                    {
                        PublicKeys = this.SSHPublicKeys,
                        KeyPairs   = this.SSHKeyPairs
                    };
                }

                var rdpEndpoint = new InputEndpoint {
                    LocalPort = 22, Protocol = "tcp", Name = "SSH"
                };
                netConfig.InputEndpoints.Add(rdpEndpoint);
                vm.ConfigurationSets.Add(linuxConfig);
                vm.ConfigurationSets.Add(netConfig);
            }

            return(vm);
        }
Example #52
0
        internal bool CheckTableAndContent(string StorageAccountName, string TableName, string FilterString, string WaitChar, bool UseNewTableNames, int TimeoutinMinutes = 15)
        {
            var            tableExists = false;
            StorageAccount account     = null;

            if (!String.IsNullOrEmpty(StorageAccountName))
            {
                account = this.GetStorageAccountFromCache(StorageAccountName);
            }
            if (account != null)
            {
                var        endpoint            = this.GetCoreEndpoint(StorageAccountName);
                var        key                 = this.GetAzureStorageKeyFromCache(StorageAccountName);
                var        credentials         = new StorageCredentials(StorageAccountName, key);
                var        cloudStorageAccount = new CloudStorageAccount(credentials, endpoint, true);
                var        tableClient         = cloudStorageAccount.CreateCloudTableClient();
                var        checkStart          = DateTime.Now;
                var        wait                = true;
                CloudTable table               = null;
                if (UseNewTableNames)
                {
                    try
                    {
                        table = tableClient.ListTablesSegmentedAsync(currentToken: null)
                                .ConfigureAwait(false).GetAwaiter().GetResult()
                                .FirstOrDefault(tab => tab.Name.StartsWith("WADMetricsPT1M"));
                    }
                    catch { } //#table name should be sorted
                }
                else
                {
                    try
                    {
                        table = tableClient.GetTableReference(TableName);
                    }
                    catch { }
                }

                while (wait)
                {
                    if (table != null && table.ExistsAsync().ConfigureAwait(false).GetAwaiter().GetResult())
                    {
                        TableQuery query = new TableQuery();
                        query.FilterString = FilterString;
                        var results = table.ExecuteQuerySegmentedAsync(query, token: null)
                                      .ConfigureAwait(false).GetAwaiter().GetResult();
                        if (results.Count() > 0)
                        {
                            tableExists = true;
                            break;
                        }
                    }

                    WriteHost(WaitChar, newLine: false);
                    TestMockSupport.Delay(5000);
                    if (UseNewTableNames)
                    {
                        try
                        {
                            table = tableClient.ListTablesSegmentedAsync(currentToken: null)
                                    .ConfigureAwait(false).GetAwaiter().GetResult().FirstOrDefault(tab => tab.Name.StartsWith("WADMetricsPT1M"));
                        }
                        catch { } //#table name should be sorted
                    }
                    else
                    {
                        try
                        {
                            table = tableClient.GetTableReference(TableName);
                        }
                        catch { }
                    }

                    wait = ((DateTime.Now) - checkStart).TotalMinutes < TimeoutinMinutes;
                }
            }
            return(tableExists);
        }
Example #53
0
 public BlobService(string dataConnectionString)
 {
     _account = CloudStorageAccount.Parse(dataConnectionString);
 }
Example #54
0
 public YouTubeTableClient(CloudStorageAccount storageAccount, string tableName)
 {
     this.storageAccount = storageAccount;
     this.tableName      = tableName;
     table = GetCloudTableAsync().Result;
 }
        public async override Task <IContainerGroup> CreateResourceAsync(CancellationToken cancellationToken)
        {
            this.containerGroupMsiHandler.ProcessCreatedExternalIdentities();
            this.containerGroupMsiHandler.HandleExternalIdentities();
            ContainerGroupImpl self = this;

            if (IsInCreateMode)
            {
                if (this.creatableStorageAccountKey != null && this.newFileShares != null && this.newFileShares.Count > 0)
                {
                    // Creates the new Azure file shares
                    var storageAccount = this.CreatedResource(this.creatableStorageAccountKey) as IStorageAccount;
                    if (this.Inner.Volumes == null)
                    {
                        this.Inner.Volumes = new List <Volume>();
                    }
                    var storageAccountKey = (await storageAccount.GetKeysAsync())[0].Value;
                    var sas             = $"DefaultEndpointsProtocol=https;AccountName={storageAccount.Name};AccountKey={storageAccountKey};EndpointSuffix=core.Windows.Net";
                    var cloudFileClient = CloudStorageAccount.Parse(sas).CreateCloudFileClient();
                    foreach (var fileShare in this.newFileShares)
                    {
                        CloudFileShare cloudFileShare = cloudFileClient.GetShareReference(fileShare.Value);
                        await cloudFileShare.CreateIfNotExistsAsync();

                        this.Inner.Volumes.Add(new Volume
                        {
                            Name      = fileShare.Key,
                            AzureFile = new AzureFileVolume
                            {
                                ShareName          = fileShare.Value,
                                ReadOnlyProperty   = false,
                                StorageAccountName = storageAccount.Name,
                                StorageAccountKey  = storageAccountKey
                            }
                        });
                    }
                }

                var inner = await this.Manager.Inner.ContainerGroups.CreateOrUpdateAsync(this.ResourceGroupName, this.Name, this.Inner, cancellationToken);

                SetInner(inner);
                this.InitializeChildrenFromInner();

                return(this);
            }
            else
            {
                var resourceInner = new ResourceInner();
                resourceInner.Location = this.RegionName;
                resourceInner.Tags     = this.Inner.Tags;
                var updatedInner = await this.Manager.Inner.ContainerGroups.UpdateAsync(this.ResourceGroupName, this.Name, resourceInner, cancellationToken : cancellationToken);

                // TODO: this will go away after service fixes the update bug
                updatedInner = await this.GetInnerAsync(cancellationToken);

                SetInner(updatedInner);
                this.InitializeChildrenFromInner();

                return(this);
            }
        }
Example #56
0
 /// <summary>
 /// Gets the account.
 /// </summary>
 /// <returns>CloudStorageAccount.</returns>
 private CloudStorageAccount GetAccount() => CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("Storage"));
Example #57
0
        public void RemoveFiles(string directoryUri, string storageConnectionString, CancellationToken cancelToken, Action <string> fileDeletedAction, Action <string, Exception> fileFailedAction, Action completedAction)
        {
            storageAccount = CloudStorageAccount.Parse(storageConnectionString);
            blobClient     = storageAccount.CreateCloudBlobClient();

            var directoryPath = directoryUri;
            var searchPattern = string.Empty;
            var lastSegment   = directoryUri.Substring(directoryUri.LastIndexOf("/") + 1);

            if (lastSegment.Contains('*') ||
                lastSegment.Contains('?'))
            {
                directoryPath = directoryUri.Remove(directoryUri.Length - lastSegment.Length);
                searchPattern = lastSegment;

                var blobs = listing.ListBlobs(directoryPath, searchPattern, storageConnectionString, true);
                this.DeleteBlobs(blobs, cancelToken, fileDeletedAction, fileFailedAction);
            }
            else
            {
                try
                {
                    var container = blobClient.GetContainerReference(directoryUri);
                    container.Delete();

                    if (fileDeletedAction != null)
                    {
                        fileDeletedAction(directoryUri);
                    }
                }
                catch (StorageClientException)
                {
                    try
                    {
                        var blob = blobClient.GetBlobReference(directoryUri);
                        blob.Delete();

                        if (fileDeletedAction != null)
                        {
                            fileDeletedAction(directoryUri);
                        }
                    }
                    catch (StorageClientException)
                    {
                        try
                        {
                            var directory = blobClient.GetBlobDirectoryReference(directoryUri);
                            var blobs     = directory.ListBlobs().Select(i => i.Uri.ToString()).ToArray();

                            if (blobs.Length > 0)
                            {
                                this.DeleteBlobs(blobs, cancelToken, fileDeletedAction, fileFailedAction);
                            }
                        }
                        catch (StorageClientException ex)
                        {
                            if (fileFailedAction != null)
                            {
                                fileFailedAction(directoryUri, ex);
                            }
                        }
                    }
                }
            }

            if (completedAction != null)
            {
                completedAction();
            }
        }
 public HomeController()
 {
     this.account       = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("Storage"));
     this.blobContainer = "valetkeysample";
 }
Example #59
0
        public async Task <List <ImageLists> > UploadImages(List <Stream> strmLists, List <string> lstContntTypes)
        {
            string myContainerName = "Test007";
            string assetID         = CreateBLOBContainer(myContainerName);

            assetID = assetID.Replace("nb:cid:UUID:", "asset-");
            List <ImageLists>   retCollection  = new List <ImageLists>();
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(myAzureStorageConSetting);
            CloudBlobClient     blobClient     = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer  container      = blobClient.GetContainerReference(assetID);

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

            if (strmLists != null)
            {
                for (int i = 0; i < strmLists.Count; i++)
                {
                    string strExtension = string.Empty;
                    if (lstContntTypes[i] == "image/gif")
                    {
                        strExtension = ".gif";
                    }
                    else if (lstContntTypes[i] == "image/jpeg")
                    {
                        strExtension = ".jpeg";
                    }
                    else if (lstContntTypes[i] == "image/jpg")
                    {
                        strExtension = ".jpg";
                    }
                    else if (lstContntTypes[i] == "image/png")
                    {
                        strExtension = ".png";
                    }
                    ImageLists     img       = new ImageLists();
                    string         imgGUID   = Guid.NewGuid().ToString();
                    CloudBlockBlob blockBlob = container.GetBlockBlobReference(string.Concat(imgGUID, strExtension));
                    await blockBlob.UploadFromStreamAsync(strmLists[i]);

                    img.ImageID   = new Guid(imgGUID);
                    img.Title     = string.Concat(imgGUID, strExtension);
                    img.ImageSize = strmLists[i].Length;
                    img.AssetID   = assetID;
                    retCollection.Add(img);

                    CloudBlockBlob blockblobthumb = container.GetBlockBlobReference(string.Concat(imgGUID, "_thumb", strExtension));
                    Stream         strmThumb      = ResizeImage(strmLists[i]);
                    using (strmThumb)
                    {
                        await blockblobthumb.UploadFromStreamAsync(strmThumb);

                        img           = new ImageLists();
                        img.ImageID   = new Guid(imgGUID);
                        img.Title     = string.Concat(imgGUID, "_thumb", strExtension);
                        img.ImageSize = strmThumb.Length;
                        img.AssetID   = assetID;
                        retCollection.Add(img);
                    }
                }
            }
            return(retCollection);
        }
Example #60
0
        public void NewAzureVMProcess()
        {
            WindowsAzureSubscription currentSubscription = CurrentSubscription;
            CloudStorageAccount      currentStorage      = null;

            try
            {
                currentStorage = currentSubscription.GetCloudStorageAccount();
            }
            catch (Exception ex) // couldn't access
            {
                throw new ArgumentException(Resources.CurrentStorageAccountIsNotAccessible, ex);
            }
            if (currentStorage == null) // not set
            {
                throw new ArgumentException(Resources.CurrentStorageAccountIsNotSet);
            }

            try
            {
                if (this.ParameterSetName.Equals("CreateService", StringComparison.OrdinalIgnoreCase))
                {
                    var parameter = new HostedServiceCreateParameters
                    {
                        AffinityGroup = this.AffinityGroup,
                        Location      = this.Location,
                        ServiceName   = this.ServiceName,
                        Description   = this.ServiceDescription ??
                                        String.Format("Implicitly created hosted service{0}", DateTime.Now.ToUniversalTime().ToString("yyyy-MM-dd HH:mm")),
                        Label = this.ServiceLabel ?? this.ServiceName
                    };
                    ExecuteClientActionNewSM(
                        parameter,
                        CommandRuntime + " - Create Cloud Service",
                        () => this.ComputeClient.HostedServices.Create(parameter));
                }
            }
            catch (CloudException ex)
            {
                this.WriteExceptionDetails(ex);
                return;
            }

            foreach (var vm in from v in VMs let configuration = v.ConfigurationSets.OfType <Model.PersistentVMModel.WindowsProvisioningConfigurationSet>().FirstOrDefault() where configuration != null select v)
            {
                if (vm.WinRMCertificate != null)
                {
                    if (!CertUtilsNewSM.HasExportablePrivateKey(vm.WinRMCertificate))
                    {
                        throw new ArgumentException(Resources.WinRMCertificateDoesNotHaveExportablePrivateKey);
                    }
                    var operationDescription = string.Format(Resources.AzureVMUploadingWinRMCertificate, CommandRuntime, vm.WinRMCertificate.Thumbprint);
                    var parameters           = CertUtilsNewSM.Create(vm.WinRMCertificate);
                    ExecuteClientActionNewSM(
                        null,
                        operationDescription,
                        () => this.ComputeClient.ServiceCertificates.Create(this.ServiceName, parameters),
                        (s, r) => ContextFactory <ComputeOperationStatusResponse, ManagementOperationContext>(r, s));
                }
                var certificateFilesWithThumbprint = from c in vm.X509Certificates
                                                     select new
                {
                    c.Thumbprint,
                    CertificateFile = CertUtilsNewSM.Create(c, vm.NoExportPrivateKey)
                };
                foreach (var current in certificateFilesWithThumbprint.ToList())
                {
                    var operationDescription = string.Format(Resources.AzureVMCommandUploadingCertificate, CommandRuntime, current.Thumbprint);
                    ExecuteClientActionNewSM(
                        null,
                        operationDescription,
                        () => this.ComputeClient.ServiceCertificates.Create(this.ServiceName, current.CertificateFile),
                        (s, r) => ContextFactory <ComputeOperationStatusResponse, ManagementOperationContext>(r, s));
                }
            }

            var persistentVMs = this.VMs.Select(vm => CreatePersistentVMRole(vm, currentStorage)).ToList();

            // If the current deployment doesn't exist set it create it
            if (CurrentDeploymentNewSM == null)
            {
                try
                {
                    var parameters = new VirtualMachineCreateDeploymentParameters
                    {
                        DeploymentSlot     = DeploymentSlot.Production,
                        Name               = this.DeploymentName ?? this.ServiceName,
                        Label              = this.DeploymentLabel ?? this.ServiceName,
                        VirtualNetworkName = this.VNetName,
                        Roles              = { persistentVMs[0] },
                        ReservedIPName     = ReservedIPName
                    };

                    if (this.DnsSettings != null)
                    {
                        parameters.DnsSettings = new Management.Compute.Models.DnsSettings();

                        foreach (var dns in this.DnsSettings)
                        {
                            parameters.DnsSettings.DnsServers.Add(new Microsoft.WindowsAzure.Management.Compute.Models.DnsServer()
                            {
                                Name = dns.Name, Address = dns.Address
                            });
                        }
                    }

                    var operationDescription = string.Format(Resources.AzureVMCommandCreateDeploymentWithVM, CommandRuntime, persistentVMs[0].RoleName);
                    ExecuteClientActionNewSM(
                        parameters,
                        operationDescription,
                        () => this.ComputeClient.VirtualMachines.CreateDeployment(this.ServiceName, parameters));

                    if (this.WaitForBoot.IsPresent)
                    {
                        WaitForRoleToBoot(persistentVMs[0].RoleName);
                    }
                }
                catch (CloudException ex)
                {
                    if (ex.Response.StatusCode == HttpStatusCode.NotFound)
                    {
                        throw new Exception(Resources.ServiceDoesNotExistSpecifyLocationOrAffinityGroup);
                    }
                    else
                    {
                        this.WriteExceptionDetails(ex);
                    }
                    return;
                }

                this.createdDeployment = true;
            }
            else
            {
                if (this.VNetName != null || this.DnsSettings != null || !string.IsNullOrEmpty(this.DeploymentLabel) || !string.IsNullOrEmpty(this.DeploymentName))
                {
                    WriteWarning(Resources.VNetNameDnsSettingsDeploymentLabelDeploymentNameCanBeSpecifiedOnNewDeployments);
                }
            }

            if (this.createdDeployment == false && CurrentDeploymentNewSM != null)
            {
                this.DeploymentName = CurrentDeploymentNewSM.Name;
            }

            int startingVM = this.createdDeployment ? 1 : 0;

            for (int i = startingVM; i < persistentVMs.Count; i++)
            {
                var operationDescription = string.Format(Resources.AzureVMCommandCreateVM, CommandRuntime, persistentVMs[i].RoleName);

                var parameter = new VirtualMachineCreateParameters
                {
                    AvailabilitySetName         = persistentVMs[i].AvailabilitySetName,
                    OSVirtualHardDisk           = persistentVMs[i].OSVirtualHardDisk,
                    RoleName                    = persistentVMs[i].RoleName,
                    RoleSize                    = persistentVMs[i].RoleSize,
                    ProvisionGuestAgent         = persistentVMs[i].ProvisionGuestAgent,
                    ResourceExtensionReferences = persistentVMs[i].ProvisionGuestAgent != null && persistentVMs[i].ProvisionGuestAgent.Value ? persistentVMs[i].ResourceExtensionReferences : null
                };

                persistentVMs[i].DataVirtualHardDisks.ForEach(c => parameter.DataVirtualHardDisks.Add(c));
                persistentVMs[i].ConfigurationSets.ForEach(c => parameter.ConfigurationSets.Add(c));

                ExecuteClientActionNewSM(
                    persistentVMs[i],
                    operationDescription,
                    () => this.ComputeClient.VirtualMachines.Create(this.ServiceName, this.DeploymentName ?? this.ServiceName, parameter));
            }

            if (this.WaitForBoot.IsPresent)
            {
                for (int i = startingVM; i < persistentVMs.Count; i++)
                {
                    WaitForRoleToBoot(persistentVMs[i].RoleName);
                }
            }
        }