예제 #1
0
        public async Task <string> UploadOpportunityImageAsync(IFormFile imageFile, string imageName)
        {
            CloudStorageAccount storageAccount = new CloudStorageAccount(
                new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(
                    StorageName, StorageApiKey), true);

            CloudBlobClient    blobClient         = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer cloudBlobContainer = blobClient.GetContainerReference(ImageContainer);
            CloudBlockBlob     blob = cloudBlobContainer.GetBlockBlobReference(imageName.Replace(" ", String.Empty));

            await blob.DeleteIfExistsAsync();

            using (MemoryStream memoryStream = new MemoryStream())
            {
                using (var image = Image.FromStream(imageFile.OpenReadStream(), true, true))
                {
                    var imageWidth  = ImageFixedWidth;
                    var imageHeight = ImageFixedHeight;

                    var widthOffset  = 0;
                    var heightOffset = 0;

                    if ((double)image.Width / image.Height > ImageAspectRatio)
                    {
                        imageHeight  = (int)Math.Round(image.Height * (double)ImageFixedWidth / image.Width);
                        heightOffset = (int)Math.Abs((ImageFixedHeight - imageHeight) / 2);
                    }
                    else
                    {
                        imageWidth  = (int)Math.Round(image.Width * (double)ImageFixedHeight / image.Height);
                        widthOffset = (int)Math.Abs((ImageFixedWidth - imageWidth) / 2);
                    }

                    using var newImage = new Bitmap(ImageFixedWidth, ImageFixedHeight);
                    using (var graphics = Graphics.FromImage(newImage))
                    {
                        graphics.DrawImage(
                            image,
                            widthOffset, heightOffset,
                            imageWidth, imageHeight
                            );
                    }
                    newImage.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
                }

                CloudBlobStream blobStream = blob.OpenWriteAsync().Result;

                memoryStream.Position = 0;

                await memoryStream.CopyToAsync(blobStream);

                await blobStream.CommitAsync();
            }

            return(blob.Uri.ToString());
        }
예제 #2
0
        public override Task UploadTextAsync(string content, Encoding encoding, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext, CancellationToken cancellationToken)
        {
            using (CloudBlobStream stream = _store.OpenWriteBlock(_containerName, _blobName, _metadata))
            {
                byte[] buffer = Encoding.UTF8.GetBytes(content);
                stream.Write(buffer, 0, buffer.Length);
                stream.CommitAsync().Wait();
            }

            return(Task.FromResult(0));
        }
        public static void UploadEmptyPage(this IStoragePageBlob blob)
        {
            if (blob == null)
            {
                throw new ArgumentNullException("blob");
            }

            using (CloudBlobStream stream = blob.OpenWriteAsync(512, CancellationToken.None).GetAwaiter().GetResult())
            {
                stream.CommitAsync().Wait();
            }
        }
예제 #4
0
        public static async Task UploadEmptyPageAsync(this CloudPageBlob blob)
        {
            if (blob == null)
            {
                throw new ArgumentNullException("blob");
            }

            using (CloudBlobStream stream = await blob.OpenWriteAsync(512))
            {
                await stream.CommitAsync();
            }
        }
        public async Task TimeSeriesDailyAdjustedLoad(TimeSeriesDailyAdjustedResponse timeSeriesDaily)
        {
            // get a reference to the blob
            string         blobName  = timeSeriesDaily.Ticker;
            CloudBlockBlob cloudBlob = blobContainer.GetBlockBlobReference(blobName);

            // generate the blob
            CloudBlobStream stream = await cloudBlob.OpenWriteAsync();

            // serialize the quotes
            byte[] vs = timeSeriesDaily.Serialize();

            // write the Quotes into a blob
            await stream.WriteAsync(vs, 0, vs.Length);

            // commit the changes to azure storage
            await stream.CommitAsync();
        }
예제 #6
0
        public async Task <string> UploadImageAsync(IFormFile image, string imageName)
        {
            CloudStorageAccount storageAccount = new CloudStorageAccount(
                new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(
                    StorageName, StorageApiKey), true);

            CloudBlobClient    blobClient         = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer cloudBlobContainer = blobClient.GetContainerReference(ImageContainer);
            CloudBlockBlob     blob = cloudBlobContainer.GetBlockBlobReference(imageName.Replace(" ", String.Empty));

            await blob.DeleteIfExistsAsync();

            CloudBlobStream blobStream = blob.OpenWriteAsync().Result;

            await image.CopyToAsync(blobStream);

            await blobStream.CommitAsync();

            return(blob.Uri.ToString());
        }
        public static async Task Run(
            [BlobTrigger("<yourStorageBlob>/{name}", Connection = "AzureWebJobsStorage")]
            Stream encryptedFile,
            string name,
            ILogger log)
        {
            // Saving the start time for diagnostics
            DateTime startTime = DateTime.Now;

            try
            {
                // The function triggers for every uploaded file.
                // We just exclude the metadata file, to have maximum flexibility.
                // Please adjust the trigger or filter for files to ignore to have the function match your purpose.
                if (name.Contains(".json") || name.Contains("metadata"))
                {
                    log.LogInformation("Metadata file upload. Exiting function.");
                    return;
                }

                ivArray = ArrayPool <byte> .Shared.Rent(12);

                // preparing decryption
                log.LogInformation("Loading account and container info...");
                string conString = Environment.GetEnvironmentVariable("AzureWebJobsStorage");
                sourceContainerName = Environment.GetEnvironmentVariable("SourceContainer");
                targetContainerName = Environment.GetEnvironmentVariable("TargetContainer");
                privateKeyFileName  = Environment.GetEnvironmentVariable("PrivateKeyFileName");
                metadataFileName    = $"{Path.GetFileNameWithoutExtension(name)}_metadata.json";

                // Remove this check if you are not using a connection string
                if (string.IsNullOrWhiteSpace(conString))
                {
                    throw new InvalidOperationException("No connection string was specified.");
                }

                if (string.IsNullOrWhiteSpace(privateKeyFileName))
                {
                    throw new InvalidOperationException("No private key file was specified.");
                }

                if (string.IsNullOrWhiteSpace(sourceContainerName))
                {
                    throw new InvalidOperationException("No source container was specified.");
                }

                if (string.IsNullOrWhiteSpace(targetContainerName))
                {
                    throw new InvalidOperationException("No target container was specified.");
                }

                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(conString);
                CloudBlobClient     blobClient     = storageAccount.CreateCloudBlobClient();
                sourceContainer = blobClient.GetContainerReference(sourceContainerName);
                targetContainer = blobClient.GetContainerReference(targetContainerName);

                log.LogInformation($"C# Blob trigger function Processed blob\n Name: {name}\n Size: {encryptedFile.Length} Bytes");
                if (encryptedFile.Length == 0)
                {
                    for (int retriesRemaining = 3; retriesRemaining > 0; retriesRemaining--)
                    {
                        if (encryptedFile.Length > 0)
                        {
                            break;
                        }
                        else
                        {
                            log.LogInformation("No data on the stream yet. Retrying in two seconds.");
                            await Task.Delay(2000);
                        }
                    }

                    if (encryptedFile.Length == 0)
                    {
                        log.LogInformation("No data received.");
                        return;
                    }
                }

                log.LogInformation("Loading metadata...");
                log.LogInformation($"Expected metadata file name: {metadataFileName}");
                using CsvProcessor csvProcessor = new CsvProcessor(await GetMetaDataAsync());

                log.LogInformation("Decrypting session key...");
                using RSACryptoServiceProvider privateKey = new RSACryptoServiceProvider();
                privateKey.FromXmlString(await GetPrivateKeyAsync());
                // decryption AES session key
                byte[] sessionKey = privateKey.Decrypt(csvProcessor.EncryptedSessionKey, true);

                log.LogInformation("Opening target stream...");
                CloudBlockBlob plainTextBlob = targetContainer.GetBlockBlobReference(name);
                await using CloudBlobStream uploadStream = await plainTextBlob.OpenWriteAsync();

                // process and decrypt the data.
                log.LogInformation("Decrypting data...");
                using (aesGcm = new AesGcm(sessionKey))
                {
                    await csvProcessor.ProcessDataAsync(DecryptCell, encryptedFile.ReadAsync, uploadStream.WriteAsync,
                                                        CancellationToken.None);
                }

                log.LogInformation("Wrapping up upload to destination blob.");
                await uploadStream.CommitAsync();
            }
            catch (Exception e)
            {
                log.LogError(e.ToString());
            }
            finally
            {
                // cleanup some resources
                if (ivArray != null)
                {
                    ArrayPool <byte> .Shared.Return(ivArray);
                }

                log.LogInformation($"Function started at {startTime} terminated.");
            }
        }
예제 #8
0
 public override Task CommitAsync()
 {
     return(_inner.CommitAsync());
 }