Beispiel #1
0
        void UploadToBlob(List <string> entries)
        {
            string data = "";

            foreach (string item in entries)
            {
                data += item + "\n";
            }

            try
            {
                //Setup connection to the azure blob storage
                Microsoft.Azure.Storage.CloudStorageAccount storageAccount = Microsoft.Azure.Storage.CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["TableConnectionString"].ConnectionString);
                CloudBlobClient    blobClient = storageAccount.CreateCloudBlobClient();
                CloudBlobContainer container  = blobClient.GetContainerReference(blobName);
                container.CreateIfNotExistsAsync().Wait();
                CloudBlockBlob blob = container.GetBlockBlobReference(fileName);

                //Upload the data string to storage
                blob.UploadTextAsync(data);
            }
            catch (Exception ex)
            {
                Error1.Text = "Failed to upload to blob storage. ";
            }
        }
        /// <summary>
        /// Get the service properties
        /// </summary>
        /// <param name="account">Cloud storage account</param>
        /// <param name="type">Service type</param>
        /// <param name="options">Request options</param>
        /// <param name="operationContext">Operation context</param>
        /// <returns>The service properties of the specified service type</returns>
        public XSCLProtocol.ServiceProperties GetStorageServiceProperties(StorageServiceType type, IRequestOptions options, XSCL.OperationContext operationContext)
        {
            XSCL.CloudStorageAccount account = StorageContext.StorageAccount;
            try
            {
                switch (type)
                {
                case StorageServiceType.Blob:
                    return(account.CreateCloudBlobClient().GetServicePropertiesAsync((BlobRequestOptions)options, operationContext).Result);

                case StorageServiceType.Queue:
                    return(account.CreateCloudQueueClient().GetServicePropertiesAsync((QueueRequestOptions)options, operationContext).Result);

                case StorageServiceType.File:
                    FileServiceProperties          fileServiceProperties = account.CreateCloudFileClient().GetServicePropertiesAsync((FileRequestOptions)options, operationContext).Result;
                    XSCLProtocol.ServiceProperties sp = new XSCLProtocol.ServiceProperties();
                    sp.Clean();
                    sp.Cors          = fileServiceProperties.Cors;
                    sp.HourMetrics   = fileServiceProperties.HourMetrics;
                    sp.MinuteMetrics = fileServiceProperties.MinuteMetrics;
                    return(sp);

                default:
                    throw new ArgumentException(Resources.InvalidStorageServiceType, "type");
                }
            }
            catch (AggregateException e) when(e.InnerException is XSCL.StorageException)
            {
                throw e.InnerException;
            }
        }
Beispiel #3
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton(Configuration);
            if (Env.IsDevelopment())
            {
                services.AddDataProtection()
                .SetDefaultKeyLifetime(TimeSpan.FromDays(14));
            }
            else
            {
                IConfigurationSection dpConfig      = Configuration.GetSection("DataProtection");
                var       provider                  = new AzureServiceTokenProvider();
                var       kvClient                  = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(provider.KeyVaultTokenCallback));
                string    vaultUri                  = Configuration[ConfigurationConstants.KeyVaultUriConfigurationKey];
                string    keyVaultKeyIdentifierName = dpConfig["KeyIdentifier"];
                KeyBundle key = kvClient.GetKeyAsync(vaultUri, keyVaultKeyIdentifierName).GetAwaiter().GetResult();
                AzureStorage.CloudStorageAccount cloudStorageAccount = AzureStorage.CloudStorageAccount.Parse(dpConfig["StorageAccountConnectionString"]);

                services.AddDataProtection()
                .PersistKeysToAzureBlobStorage(cloudStorageAccount, "/site/keys.xml")
                .ProtectKeysWithAzureKeyVault(kvClient, key.KeyIdentifier.ToString())
                .SetDefaultKeyLifetime(TimeSpan.FromDays(14))
                .SetApplicationName(typeof(Startup).FullName);
            }

            AddServices(services);
            ConfigureConfiguration(services);
        }
Beispiel #4
0
        private string UploadProfilePicture(IFormFile profilePicture)
        {
            var reader = profilePicture.OpenReadStream();
            var cloudStorageAccount = CloudStorageAccount.Parse(@"DefaultEndpointsProtocol=https;AccountName=atazure;AccountKey=KfLMRh/w+nHjvUmPdhnBQtgYamgn418nqxMqOrk0T4Kxt14PnUXBpJuH+dEgvIHWBoeXq4H+Fi6NKZK84yNUIw==;EndpointSuffix=core.windows.net");
            var blobClient          = cloudStorageAccount.CreateCloudBlobClient();
            var container           = blobClient.GetContainerReference("fotoperfil");

            container.CreateIfNotExists();
            var blob = container.GetBlockBlobReference(Guid.NewGuid().ToString());

            blob.UploadFromStream(reader);
            var destinyOfThePictureInTheCloud = blob.Uri.ToString();

            return(destinyOfThePictureInTheCloud);
        }
Beispiel #5
0
        private async Task AddEntryQueue(QueueItem orderItem)
        {
            // Retrieve storage account from connection string.
            Microsoft.Azure.Storage.CloudStorageAccount storageAccount = Microsoft.Azure.Storage.CloudStorageAccount.Parse(storageConnectionString);

            // Create the queue client.
            CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();

            // Retrieve a reference to a queue.
            CloudQueue queue = queueClient.GetQueueReference("orders");

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

            // Create a message and add it to the queue.
            CloudQueueMessage message = new CloudQueueMessage(JsonConvert.SerializeObject(orderItem));
            await queue.AddMessageAsync(message);
        }
Beispiel #6
0
        void RemoveFromBlob()
        {
            try
            {
                //Setup connection to the azure blob storage
                Microsoft.Azure.Storage.CloudStorageAccount storageAccount = Microsoft.Azure.Storage.CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["TableConnectionString"].ConnectionString);
                CloudBlobClient    blobClient = storageAccount.CreateCloudBlobClient();
                CloudBlobContainer container  = blobClient.GetContainerReference(blobName);
                CloudBlockBlob     blob       = container.GetBlockBlobReference(fileName);

                //Deletes the whole blob
                blob.DeleteAsync();

                //Update status to user
                ResultTitle.Text = "Status:";
                Results1.Text    = "";
                Results1.Text    = "Removed from Blob Storage";
            }
            catch (Exception ex)
            {
                Error1.Text = "Failed to remove from blob storage. ";
            }
        }
        /// <summary>
        /// Set service properties
        /// </summary>
        /// <param name="account">Cloud storage account</param>
        /// <param name="type">Service type</param>
        /// <param name="properties">Service properties</param>
        /// <param name="options">Request options</param>
        /// <param name="operationContext">Operation context</param>
        public void SetStorageServiceProperties(StorageServiceType type, XSCLProtocol.ServiceProperties properties, IRequestOptions options, XSCL.OperationContext operationContext)
        {
            XSCL.CloudStorageAccount account = StorageContext.StorageAccount;
            try
            {
                switch (type)
                {
                case StorageServiceType.Blob:
                    Task.Run(() => account.CreateCloudBlobClient().SetServicePropertiesAsync(properties, (BlobRequestOptions)options, operationContext)).Wait();
                    break;

                case StorageServiceType.Queue:
                    Task.Run(() => account.CreateCloudQueueClient().SetServicePropertiesAsync(properties, (QueueRequestOptions)options, operationContext)).Wait();
                    break;

                case StorageServiceType.File:
                    if (null != properties.Logging)
                    {
                        throw new InvalidOperationException(Resources.FileNotSupportLogging);
                    }

                    FileServiceProperties fileServiceProperties = new FileServiceProperties();
                    fileServiceProperties.Cors          = properties.Cors;
                    fileServiceProperties.HourMetrics   = properties.HourMetrics;
                    fileServiceProperties.MinuteMetrics = properties.MinuteMetrics;
                    Task.Run(() => account.CreateCloudFileClient().SetServicePropertiesAsync(fileServiceProperties, (FileRequestOptions)options, operationContext)).Wait();
                    break;

                default:
                    throw new ArgumentException(Resources.InvalidStorageServiceType, "type");
                }
            }
            catch (AggregateException e) when(e.InnerException is XSCL.StorageException)
            {
                throw e.InnerException;
            }
        }
Beispiel #8
0
        public async Task InitializeAsync()
        {
            if (!string.IsNullOrEmpty(_functionsWorkerLanguage))
            {
                Environment.SetEnvironmentVariable(RpcWorkerConstants.FunctionWorkerRuntimeSettingName, _functionsWorkerLanguage);
            }
            IConfiguration      configuration    = TestHelpers.GetTestConfiguration();
            string              connectionString = configuration.GetWebJobsConnectionString(ConnectionStringNames.Storage);
            CloudStorageAccount storageAccount   = CloudStorageAccount.Parse(connectionString);

            QueueClient = storageAccount.CreateCloudQueueClient();
            BlobClient  = storageAccount.CreateCloudBlobClient();

            TableStorageAccount tableStorageAccount = TableStorageAccount.Parse(connectionString);

            TableClient = tableStorageAccount.CreateCloudTableClient();

            await CreateTestStorageEntities();

            // ApiHubTestHelper.SetDefaultConnectionFactory();

            //ILoggerProviderFactory loggerProviderFactory = new TestLoggerProviderFactory(LoggerProvider);

            // Reset the timer logs first, since one of the tests will
            // be checking them
            TestHelpers.ClearFunctionLogs("TimerTrigger");
            TestHelpers.ClearFunctionLogs("ListenerStartupException");

            Host = new HostBuilder()
                   .ConfigureDefaultTestWebScriptHost(webjobsBuilder =>
            {
                webjobsBuilder.AddAzureStorage();

                // This needs to added manually at the ScriptHost level, as although FunctionMetadataManager is available through WebHost,
                // it needs to change the services during its lifetime.
                webjobsBuilder.Services.AddSingleton <IFunctionMetadataManager, FunctionMetadataManager>();
            },
                                                      o =>
            {
                o.ScriptPath = _rootPath;
                o.LogPath    = TestHelpers.GetHostLogFileDirectory().Parent.FullName;
            },
                                                      runStartupHostedServices: true)
                   .ConfigureServices(services =>
            {
                services.Configure <ScriptJobHostOptions>(o =>
                {
                    o.FileLoggingMode = FileLoggingMode.Always;

                    if (_functions != null)
                    {
                        o.Functions = _functions;
                    }
                });

                if (_proxyClient != null)
                {
                    services.AddSingleton <ProxyClientExecutor>(_proxyClient);
                }

                // Shared memory data transfer
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    services.AddSingleton <IMemoryMappedFileAccessor, MemoryMappedFileAccessorWindows>();
                }
                else
                {
                    services.AddSingleton <IMemoryMappedFileAccessor, MemoryMappedFileAccessorUnix>();
                }
                services.AddSingleton <ISharedMemoryManager, SharedMemoryManager>();

                ConfigureServices(services);
            })
                   .ConfigureLogging(b =>
            {
                b.AddProvider(LoggerProvider);
            })
                   .Build();

            JobHost = Host.GetScriptHost();

            if (_startHost)
            {
                JobHost.HostStarted += (s, e) => _hostStartedEvent.Set();
                await Host.StartAsync();

                _hostStartedEvent.Wait(TimeSpan.FromSeconds(30));
            }
        }
Beispiel #9
0
        public async Task <IActionResult> Index(PowerpointFile powerpointFile)
        {
            // Create a local file in the ./data/ directory for uploading and downloading
            string localPath = "";
            //powerpointFile.FileTitle = powerpointFile.MyFile.FileName;
            string results_fileName  = "results_" + Guid.NewGuid().ToString() + ".txt";
            string localFilePath_txt = Path.Combine(localPath, results_fileName);



            Microsoft.WindowsAzure.Storage.CloudStorageAccount storageacc = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(connectionString);



            Microsoft.Azure.Storage.CloudStorageAccount queueStorageAccount = Microsoft.Azure.Storage.CloudStorageAccount.Parse(connectionString);



            // Create a BlobServiceClient object which will be used to create a container client
            CloudBlobClient blobClient = storageacc.CreateCloudBlobClient();

            CloudQueueClient queueClient = queueStorageAccount.CreateCloudQueueClient();

            // Retrieve a reference to a queue.
            CloudQueue queue = queueClient.GetQueueReference(queueName);

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


            // Create the container and return a container client object
            CloudBlobContainer container = blobClient.GetContainerReference(containerName);
            await container.CreateIfNotExistsAsync();

            CloudBlockBlob blockBlob = container.GetBlockBlobReference(results_fileName);

            blockBlob.Properties.ContentType = "text/plain";

            // Write text to the file
            await System.IO.File.WriteAllTextAsync(localFilePath_txt, "Working on ppt_xx.pptx");

            using (var filestream = System.IO.File.OpenRead(localFilePath_txt))
            {
                await blockBlob.UploadFromStreamAsync(filestream);

                //send message to Queue
                CloudQueueMessage message = new CloudQueueMessage(blockBlob.Uri.ToString());
                queue.AddMessage(message);
            }

            /*
             * https://github.com/jayesh-tanna/azure-blob/blob/master/AzureBlob/AzureBlob.API/Controllers/UserController.cs
             */

            if (Microsoft.Azure.Storage.CloudStorageAccount.TryParse(connectionString, out Microsoft.Azure.Storage.CloudStorageAccount storageAccount))
            {
                await container.CreateIfNotExistsAsync();

                //MS: Don't rely on or trust the FileName property without validation. The FileName property should only be used for display purposes.
                var picBlob = container.GetBlockBlobReference(powerpointFile.MyFile.FileName);

                await picBlob.UploadFromStreamAsync(powerpointFile.MyFile.OpenReadStream());

                //send message to Queue
                CloudQueueMessage message = new CloudQueueMessage(picBlob.Uri.ToString());
                queue.AddMessage(message);

                // return Ok(picBlob.Uri);
            }



            return(Redirect("/Home/LinkPage"));
        }
Beispiel #10
0
        public async Task InitializeAsync()
        {
            _copiedRootPath = Path.Combine(Path.GetTempPath(), "FunctionsE2E", DateTime.UtcNow.ToString("yyMMdd-HHmmss"));
            FileUtility.CopyDirectory(_rootPath, _copiedRootPath);

            var extensionsToInstall = GetExtensionsToInstall();

            if (extensionsToInstall != null && extensionsToInstall.Length > 0)
            {
                TestFunctionHost.WriteNugetPackageSources(_copiedRootPath, "http://www.myget.org/F/azure-appservice/api/v2", "https://www.myget.org/F/azure-appservice-staging/api/v2", "https://api.nuget.org/v3/index.json");
                var options = new OptionsWrapper <ScriptJobHostOptions>(new ScriptJobHostOptions
                {
                    RootScriptPath = _copiedRootPath
                });

                var manager = new ExtensionsManager(options, NullLogger <ExtensionsManager> .Instance, new TestExtensionBundleManager());
                await manager.AddExtensions(extensionsToInstall);
            }

            string logPath = Path.Combine(Path.GetTempPath(), @"Functions");

            if (!string.IsNullOrEmpty(_functionsWorkerRuntime))
            {
                Environment.SetEnvironmentVariable(RpcWorkerConstants.FunctionWorkerRuntimeSettingName, _functionsWorkerRuntime);
                Environment.SetEnvironmentVariable(RpcWorkerConstants.FunctionsWorkerProcessCountSettingName, _workerProcessCount.ToString());
            }
            if (!string.IsNullOrEmpty(_functionsWorkerRuntimeVersion))
            {
                Environment.SetEnvironmentVariable(RpcWorkerConstants.FunctionWorkerRuntimeVersionSettingName, _functionsWorkerRuntimeVersion);
            }

            FunctionsSyncManagerMock = new Mock <IFunctionsSyncManager>(MockBehavior.Strict);
            FunctionsSyncManagerMock.Setup(p => p.TrySyncTriggersAsync(It.IsAny <bool>())).ReturnsAsync(new SyncTriggersResult {
                Success = true
            });

            Host = new TestFunctionHost(_copiedRootPath, logPath,
                                        configureScriptHostWebJobsBuilder: webJobsBuilder =>
            {
                ConfigureScriptHost(webJobsBuilder);
            },
                                        configureScriptHostServices: s =>
            {
                s.AddSingleton <IFunctionsSyncManager>(_ => FunctionsSyncManagerMock.Object);
                s.AddSingleton <IMetricsLogger>(_ => MetricsLogger);
            },
                                        configureWebHostServices: s =>
            {
                s.AddSingleton <IEventGenerator>(_ => EventGenerator);
            });

            string connectionString            = Host.JobHostServices.GetService <IConfiguration>().GetWebJobsConnectionString(ConnectionStringNames.Storage);
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);

            QueueClient = storageAccount.CreateCloudQueueClient();
            BlobClient  = storageAccount.CreateCloudBlobClient();

            TableStorageAccount tableStorageAccount = TableStorageAccount.Parse(connectionString);

            TableClient = tableStorageAccount.CreateCloudTableClient();

            await CreateTestStorageEntities();

            MasterKey = await Host.GetMasterKeyAsync();
        }
Beispiel #11
0
        public async Task InitializeAsync()
        {
            if (!string.IsNullOrEmpty(_functionsWorkerLanguage))
            {
                Environment.SetEnvironmentVariable(RpcWorkerConstants.FunctionWorkerRuntimeSettingName, _functionsWorkerLanguage);
            }
            IConfiguration      configuration    = TestHelpers.GetTestConfiguration();
            string              connectionString = configuration.GetWebJobsConnectionString(ConnectionStringNames.Storage);
            CloudStorageAccount storageAccount   = CloudStorageAccount.Parse(connectionString);

            QueueClient = storageAccount.CreateCloudQueueClient();
            BlobClient  = storageAccount.CreateCloudBlobClient();

            TableStorageAccount tableStorageAccount = TableStorageAccount.Parse(connectionString);

            TableClient = tableStorageAccount.CreateCloudTableClient();

            await CreateTestStorageEntities();

            // ApiHubTestHelper.SetDefaultConnectionFactory();

            //ILoggerProviderFactory loggerProviderFactory = new TestLoggerProviderFactory(LoggerProvider);

            // Reset the timer logs first, since one of the tests will
            // be checking them
            TestHelpers.ClearFunctionLogs("TimerTrigger");
            TestHelpers.ClearFunctionLogs("ListenerStartupException");

            Host = new HostBuilder()
                   .ConfigureDefaultTestWebScriptHost(webjobsBuilder =>
            {
                webjobsBuilder.AddAzureStorage();
            },
                                                      o =>
            {
                o.ScriptPath = _rootPath;
                o.LogPath    = TestHelpers.GetHostLogFileDirectory().Parent.FullName;
            },
                                                      runStartupHostedServices: true)
                   .ConfigureServices(services =>
            {
                services.Configure <ScriptJobHostOptions>(o =>
                {
                    o.FileLoggingMode = FileLoggingMode.Always;

                    if (_functions != null)
                    {
                        o.Functions = _functions;
                    }
                });

                if (_proxyClient != null)
                {
                    services.AddSingleton <ProxyClientExecutor>(_proxyClient);
                }

                ConfigureServices(services);
            })
                   .ConfigureLogging(b =>
            {
                b.AddProvider(LoggerProvider);
            })
                   .Build();

            JobHost = Host.GetScriptHost();

            if (_startHost)
            {
                JobHost.HostStarted += (s, e) => _hostStartedEvent.Set();
                await Host.StartAsync();

                _hostStartedEvent.Wait(TimeSpan.FromSeconds(30));
            }
        }
Beispiel #12
0
        public static async Task ProcessQueueMessage([QueueTrigger("queuebrandingpolice")] string message, ILogger logger)
        {
            logger.LogInformation(message);

            try
            {
                Microsoft.WindowsAzure.Storage.CloudStorageAccount storageacc = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(connectionString);
                // Create a BlobServiceClient object which will be used to create a container client
                CloudBlobClient blobCliente = storageacc.CreateCloudBlobClient();
                // Create the container and return a container client object
                CloudBlobContainer container = blobCliente.GetContainerReference(containerName);
                await container.CreateIfNotExistsAsync();

                // Retrieve storage account from connection string.
                Microsoft.Azure.Storage.CloudStorageAccount queueStorageAccount = Microsoft.Azure.Storage.CloudStorageAccount.Parse(connectionString);
                CloudQueueClient queueClient = queueStorageAccount.CreateCloudQueueClient();
                // Retrieve a reference to a queue.
                CloudQueue queue = queueClient.GetQueueReference(queueName);

                // Create a BlobServiceClient object which will be used to create a container client
                BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
                // Create the container and return a container client object
                BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);

                // Get the message from the queue and update the message contents.
                CloudQueueMessage messagequeue = queue.GetMessage();
                // Get the next message
                //CloudQueueMessage retrievedMessage = queue.GetMessage();


                string localPath     = "";
                string filename_pptx = null;
                string GetExtension  = null;

                Uri uri = new Uri(message);

                GetExtension = System.IO.Path.GetExtension(uri.LocalPath);

                if (GetExtension == ".txt")
                {
                    filename_txt      = System.IO.Path.GetFileName(uri.LocalPath);
                    localFilePath_txt = Path.Combine(localPath, filename_txt);
                }
                if (GetExtension != ".txt")
                {
                    filename_pptx = System.IO.Path.GetFileName(uri.LocalPath);
                    CloudBlockBlob blockBlob_txt = container.GetBlockBlobReference(filename_txt);
                    blockBlob_txt.Properties.ContentType = "text/plain";

                    // Get a reference to a blob
                    BlobClient blobClient_pptx = containerClient.GetBlobClient(filename_pptx);

                    BlobDownloadInfo download = blobClient_pptx.Download();
                    using (FileStream file1 = File.OpenWrite(filename_pptx))
                    {
                        download.Content.CopyTo(file1);
                        file1.Close();
                        int    numberOfSlides = CountSlides(file1.Name);
                        string result = "", search = "Windows Azure";
                        System.Console.WriteLine("Number of slides = {0}", numberOfSlides);
                        string slideText;
                        for (int i = 0; i < numberOfSlides; i++)
                        {
                            GetSlideIdAndText(out slideText, file1.Name, i);
                            System.Console.WriteLine("Slide #{0} contains: {1}", i + 1, slideText);
                            // append text to the file
                            //await System.IO.File.AppendAllTextAsync(localFilePath_txt, slideText);
                            if (slideText.Contains(search))
                            {
                                System.Console.WriteLine("\nRETURN 0\n", i + 1, slideText);
                            }
                            result += result + String.Format("In der {0}.Slide benutzen Sie den Begriff Windows Azure statt Microsoft Azure\n", i + 1);
                        }
                        await System.IO.File.AppendAllTextAsync(localFilePath_txt, result);

                        using (var filestream = System.IO.File.OpenRead(localFilePath_txt))
                        {
                            await blockBlob_txt.UploadFromStreamAsync(filestream);
                        }
                    }

                    //Delete .pptx blob
                    await blobClient_pptx.DeleteIfExistsAsync();


                    filename_txt      = null;
                    localFilePath_txt = null;
                }
            }
            catch (System.Exception)
            {
                throw;
            }
        }