Пример #1
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]
            HttpRequest req,
            ILogger log,
            ExecutionContext context)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            #region Process parameters
            string fileName = req.Query["fileName"];
            if (String.IsNullOrEmpty(fileName))
            {
                log.LogInformation("Mandatory parameter omitted.");
                return(new BadRequestResult());
            }
            log.LogInformation($"Uploading file {fileName}");
            #endregion

            #region Load configuration
            var configBuilder = new ConfigurationBuilder()
                                .SetBasePath(context.FunctionAppDirectory)
                                .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                                .AddEnvironmentVariables()
                                .Build();
            var config = new ConnectorConfig(configBuilder);
            #endregion

            #region Download file
            string       requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            byte[]       byteArray   = Encoding.ASCII.GetBytes(requestBody);
            MemoryStream stream      = new MemoryStream(byteArray);

            await _azurestorage.UploadAsync(config, fileName, stream);

            var responseMessage = string.IsNullOrEmpty(fileName)
                ? "This HTTP triggered function executed successfully. Pass a name in the query string and the file in the request body."
                : $"Hello, This HTTP triggered function executed successfully {fileName}.";

            return(new OkObjectResult(responseMessage));

            #endregion
        }