static void Main(string[] args)
        {
            const string connectionStringAzureStorageAccount = "YOUR-CONNECTIONSTRING-HERE";
            var          secureBlobStorageConnector          = new SecureBlobStorageConnector.SecureBlobStorageConnector(connectionStringAzureStorageAccount);

            // Upload blob:
            var localPath = Environment.CurrentDirectory + "/new.jpg";

            using (var fileStream = File.Open(localPath, FileMode.Open, FileAccess.Read))
            {
                secureBlobStorageConnector.UploadAsync("car.png", "mediafiles", fileStream).GetAwaiter().GetResult();
            }
            Console.WriteLine($"File {localPath} uploaded!");

            // Getting blob:
            var stream = secureBlobStorageConnector.GetAsync("car.png", "mediafiles").GetAwaiter().GetResult();

            Console.WriteLine("File car.png downloaded, saving it to local disk...");

            // Write the file to disk
            var destinationPath = Environment.CurrentDirectory + "/car.png";

            using (var fileStream = new FileStream(destinationPath, FileMode.Create, FileAccess.ReadWrite))
            {
                stream.Seek(0, SeekOrigin.Begin);
                stream.CopyTo(fileStream);
                stream.Close();
            }

            Console.WriteLine($"The downloaded image is now here: {destinationPath}");
            Console.ReadLine();
        }
示例#2
0
        private static async Task <IActionResult> HandlePOST(HttpRequest req, ILogger log)
        {
            var secureBlobStorageConnector = new SecureBlobStorageConnector.SecureBlobStorageConnector(_connectionString);

            try
            {
                if (req.Body.Length <= 0)
                {
                    return(new BadRequestObjectResult("Provided file is empty!"));
                }
            }
            catch (NotSupportedException)
            {
                return(new BadRequestObjectResult("No file provided, or provided file is empty!"));
            }

            var contentType = req.Headers["content-type"];

            if (string.IsNullOrEmpty(contentType))
            {
                return(new BadRequestObjectResult("No content type provided in headers."));
            }

            var mime = new Mime();

            if (mime.Extension(contentType).Count == 0)
            {
                return(new BadRequestObjectResult($"Specified content type {contentType} invalid!"));
            }

            var extension     = mime.Extension(contentType)[0];
            var fileName      = $"{Guid.NewGuid().ToString()}.{extension}";
            var containerName = req.Query["containerName"];

            await secureBlobStorageConnector.UploadAsync(fileName, req.Query["containerName"], req.Body);


            return(new CreatedResult(fileName, null));
        }
示例#3
0
        private static async Task <IActionResult> HandleGET(HttpRequest req)
        {
            var blobName      = req.Query["blobName"];
            var containerName = req.Query["containerName"];

            if (string.IsNullOrEmpty(blobName) || string.IsNullOrEmpty(containerName))
            {
                return(new BadRequestObjectResult("Blob name or containerName not specified!"));
            }

            var secureBlobStorageConnector = new SecureBlobStorageConnector.SecureBlobStorageConnector(_connectionString);

            var blob = await secureBlobStorageConnector.GetAsync(blobName, containerName);

            if (blob == null || blob.Length <= 0)
            {
                return(new BadRequestObjectResult("Blob is empty"));
            }

            return(new FileContentResult(blob.ToArray(), "application/octet-stream")
            {
                FileDownloadName = blobName
            });
        }