Beispiel #1
0
        // GET api/SurfReports
        /// <summary>
        /// Retrieves a Shared Access Signature for the surf-reports blob storage for either upload or download, depending on the
        /// query param passed in
        /// </summary>
        /// <param name="SAStype">
        /// shared access signature for upload or download. Must be "upload" or "download" lolz
        /// </param>
        /// <returns></returns>
        public async Task <StorageEntitySas> Get(SASAllowedRequests request)
        {
            ServiceEventSource.Current.Message("SAS key requested from valet service of type {0}: {1}", request, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

            // Retrieve a shared access signature of the location we should upload/download this file to/from
            try
            {
                var blobName             = Guid.NewGuid();
                StorageEntitySas blobSas = new StorageEntitySas();

                //Create an array of tasks which will be executed asynchronously in order to retrieve the SAS credentials
                //I can't figure out how to make this work without task factories
                //TODO: make this work with normal tasks instead of a factory which should be unnecessary
                Task <StorageEntitySas>[] getSas = new Task <StorageEntitySas> [1];

                StorageEntitySas sas = new StorageEntitySas();
                if (request == SASAllowedRequests.Upload)
                {
                    getSas[0] = Task.Factory.StartNew(() => sas = this.GetSharedAccessReferenceForUpload(blobName.ToString()));
                }
                else if (request == SASAllowedRequests.Download)
                {
                    getSas[0] = Task.Factory.StartNew(() => sas = this.GetSharedAccessReferenceForDownload(blobName.ToString()));
                }
                else
                {
                    throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest)
                    {
                        Content      = new StringContent("You must specify upload or download in the query request"),
                        ReasonPhrase = "Invalid request format"
                    });
                }

                Trace.WriteLine(string.Format("Blob Uri: {0} - Shared Access Signature: {1}", blobSas.BlobUri, blobSas.Credentials));

                //the continue when all call here is a little silly but I'm not great at async in C#
                var retrieved = Task.Factory.ContinueWhenAll <StorageEntitySas>(getSas, completedTask => { return(sas); });

                return(await retrieved);
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.Message);
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError)
                {
                    Content      = new StringContent("An error has ocurred"),
                    ReasonPhrase = "Critical Exception"
                });
            }
        }
Beispiel #2
0
        private static async Task <StorageEntitySas> GetBlobSas(Uri blobUri)
        {
            StorageEntitySas    blobSas  = new StorageEntitySas();
            HttpClient          client   = new HttpClient();
            HttpResponseMessage response = await client.GetAsync(blobUri);

            if (response.IsSuccessStatusCode)
            {
                string stringData = response.Content.ReadAsStringAsync().Result;
                blobSas = JsonConvert.DeserializeObject <StorageEntitySas>(stringData);
            }

            return(blobSas);
        }
Beispiel #3
0
        public async Task <StorageEntitySas> Get(string blobName, string requestType)
        {
            //TODO add validator for this guy and inject the validation service into this class
            if (String.IsNullOrWhiteSpace(requestType))
            {
                throw new InvalidOperationException("you need to give a request type");
            }
            if (String.IsNullOrWhiteSpace(blobName))
            {
                throw new InvalidOperationException("you need to give a blob name");
            }
            ServiceEventSource.Current.Message("SAS key requested through API of type {0}: {1}", requestType, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            //call the service SurfReportsSASController in $/ValetAccessManager/Controllers/SurfReportController.cs and ask it to give us a gosh darned SAS key
            StorageEntitySas sasKey = await Svc.Get(Utilities.ParseEnum <SASAllowedRequests>(requestType));

            //string ben = await "sdfjadslk";

            return(sasKey);
        }
        private StorageEntitySas GetSharedAccessReferenceForUpload(string containerName, string blobName)
        {
            var client    = storageAccount.CreateCloudBlobClient();
            var container = client.GetContainerReference(containerName);
            var blob      = container.GetBlockBlobReference(blobName);

            var credentials = blob.GetSharedAccessSignature(new SharedAccessBlobPolicy
            {
                Permissions            = SharedAccessBlobPermissions.Write,
                SharedAccessStartTime  = DateTime.UtcNow.AddMinutes(-5),
                SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(30)
            });

            var EntitySAS = new StorageEntitySas {
                Credentials = credentials,
                BlobUri     = blob.Uri,
                BlobUriSAS  = blob.Uri.AbsoluteUri + credentials
            };

            return(EntitySAS);
        }