コード例 #1
0
ファイル: DataContext.cs プロジェクト: Vespion/AquaShine
        /// <summary>
        /// Generates the URIs that a client can use to upload images to the storage system
        /// </summary>
        /// <param name="entrantId">The entrant that is being uploaded</param>
        /// <param name="generateDisplayUri">Controls the generation of the displayUploadUri</param>
        /// <param name="verifyContainerName">The name of the blob container for verification images. Default: 'photos-verification'</param>
        /// <param name="displayContainerName">The name of the blob container for verification images. Default: 'photos-display'</param>
        /// <returns></returns>
        public async Task <(Uri verifyUploadUri, Uri?displayUploadUri)> GenerateImageUploadUris(string entrantId,
                                                                                                bool generateDisplayUri, string verifyContainerName = "photos-verification",
                                                                                                string displayContainerName = "photos-display")
        {
            if (string.IsNullOrWhiteSpace(entrantId))
            {
                throw new ArgumentNullException(nameof(entrantId));
            }

            //Generate containers
            var blobClient = new BlobContainerClient(_storageAccount.ToString(true), displayContainerName);
            await blobClient.CreateIfNotExistsAsync(PublicAccessType.Blob).ConfigureAwait(false);

            blobClient = new BlobContainerClient(_storageAccount.ToString(true), verifyContainerName);
            await blobClient.CreateIfNotExistsAsync(PublicAccessType.None).ConfigureAwait(false);

            var sasBuilder = new BlobSasBuilder
            {
                BlobContainerName = verifyContainerName,
                BlobName          = entrantId,
                Resource          = @"b",
                ExpiresOn         = DateTimeOffset.UtcNow.AddMinutes(5)
            };

            //Sets the permissions for the SAS token
            sasBuilder.SetPermissions(BlobSasPermissions.Create | BlobSasPermissions.Write | BlobSasPermissions.Delete);

            var credentials = new StorageSharedKeyCredential(_storageAccount.Credentials.AccountName,
                                                             _storageAccount.Credentials.ExportBase64EncodedKey());

            //The constructed sas token
            var verifyUploadSas = sasBuilder.ToSasQueryParameters(credentials);

            //Uri builder for verify blob
            var builder = new BlobUriBuilder(_storageAccount.BlobEndpoint)
            {
                BlobName          = entrantId,
                BlobContainerName = verifyContainerName,
                AccountName       = _storageAccount.Credentials.AccountName,
                Sas = verifyUploadSas
            };
            var verifyUploadUri = builder.ToUri();

            sasBuilder.BlobContainerName = displayContainerName;
            builder.BlobContainerName    = displayContainerName;

            if (generateDisplayUri)
            {
                builder.Sas = sasBuilder.ToSasQueryParameters(credentials);
                var displayUploadUri = builder.ToUri();
                return(verifyUploadUri, displayUploadUri);
            }
            return(verifyUploadUri, null);
        }
コード例 #2
0
        public async Task <IActionResult> Create([Bind("Id,FirstName,LastName,Image")] InstructorVM instructor)
        {
            CloudStorageAccount account       = CloudStorageAccount.Parse(storageConnection.ToString());
            CloudBlobClient     serviceClient = account.CreateCloudBlobClient();

            // Create container. Name must be lower case.
            Console.WriteLine("Creating container...");
            var container = serviceClient.GetContainerReference("instructor-images");

            container.CreateIfNotExistsAsync().Wait();

            // write a blob to the container
            CloudBlockBlob blob = container.GetBlockBlobReference("helloworld.txt");

            using (var memoryStream = new MemoryStream())
            {
                await instructor.Image.CopyToAsync(memoryStream);

                await blob.UploadFromStreamAsync(memoryStream);



                if (ModelState.IsValid)
                {
                    await _instructorsService.AddInstructorAsync(new InstructorRequest
                    {
                        FirstName = instructor.FirstName,
                        LastName  = instructor.LastName
                    });

                    return(RedirectToAction(nameof(Index)));
                }
            }
            return(View(instructor));
        }