示例#1
0
        public static async Task <string> SaveBinaryToAzureStorage(MobileAppSettingsDictionary settings, string blobId, string blobData)
        {
            string storageAccountName;
            string storageAccountKey;


            // Try to get the Azure storage account token from app settings.
            if (!(settings.TryGetValue("STORAGE_ACCOUNT_NAME", out storageAccountName) |
                  settings.TryGetValue("STORAGE_ACCOUNT_ACCESS_KEY", out storageAccountKey)))
            {
                return(string.Empty);
            }

            // Set the URI for the Blob Storage service.
            Uri blobEndpoint = new Uri(string.Format("https://{0}.blob.core.windows.net", storageAccountName));

            // Create the BLOB service client.
            CloudBlobClient blobClient = new CloudBlobClient(blobEndpoint,
                                                             new StorageCredentials(storageAccountName, storageAccountKey));

            string ContainerName = "beerdrinkinimages";

            // Create a container, if it doesn't already exist.
            CloudBlobContainer container = blobClient.GetContainerReference(ContainerName);
            await container.CreateIfNotExistsAsync();

            // Create a shared access permission policy.
            BlobContainerPermissions containerPermissions = new BlobContainerPermissions();

            // Enable anonymous read access to BLOBs.
            containerPermissions.PublicAccess = BlobContainerPublicAccessType.Blob;
            container.SetPermissions(containerPermissions);

            // Define a policy that gives write access to the container for 1 minute.
            SharedAccessBlobPolicy sasPolicy = new SharedAccessBlobPolicy()
            {
                SharedAccessStartTime  = DateTime.UtcNow,
                SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(1),
                Permissions            = SharedAccessBlobPermissions.Write
            };

            // Get the SAS as a string.
            var SasQueryString = container.GetSharedAccessSignature(sasPolicy);

            // Set the URL used to store the image.
            var avatarUri = string.Format("{0}{1}/{2}", blobEndpoint.ToString(),
                                          ContainerName, blobId.ToLower());

            // Upload the new image as a BLOB from the string.
            CloudBlockBlob blobFromSASCredential =
                container.GetBlockBlobReference(blobId.ToLower());

            blobFromSASCredential.UploadTextAsync(blobData);

            return(avatarUri);
        }
示例#2
0
        public static async Task<string> SaveBinaryToAzureStorage(MobileAppSettingsDictionary settings, string blobId, string blobData )
        {
            string storageAccountName;
            string storageAccountKey;


            // Try to get the Azure storage account token from app settings.  
            if (!(settings.TryGetValue("STORAGE_ACCOUNT_NAME", out storageAccountName) |
                settings.TryGetValue("STORAGE_ACCOUNT_ACCESS_KEY", out storageAccountKey)))
            {
                return string.Empty;
            }

            // Set the URI for the Blob Storage service.
            Uri blobEndpoint = new Uri(string.Format("https://{0}.blob.core.windows.net", storageAccountName));

            // Create the BLOB service client.
            CloudBlobClient blobClient = new CloudBlobClient(blobEndpoint,
                new StorageCredentials(storageAccountName, storageAccountKey));

            string ContainerName = "beerdrinkinimages";

            // Create a container, if it doesn't already exist.
            CloudBlobContainer container = blobClient.GetContainerReference(ContainerName);
            await container.CreateIfNotExistsAsync();

            // Create a shared access permission policy. 
            BlobContainerPermissions containerPermissions = new BlobContainerPermissions();

            // Enable anonymous read access to BLOBs.
            containerPermissions.PublicAccess = BlobContainerPublicAccessType.Blob;
            container.SetPermissions(containerPermissions);

            // Define a policy that gives write access to the container for 1 minute.                                   
            SharedAccessBlobPolicy sasPolicy = new SharedAccessBlobPolicy()
            {
                SharedAccessStartTime = DateTime.UtcNow,
                SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(1),
                Permissions = SharedAccessBlobPermissions.Write
            };

            // Get the SAS as a string.
            var SasQueryString = container.GetSharedAccessSignature(sasPolicy);

            // Set the URL used to store the image.
            var avatarUri = string.Format("{0}{1}/{2}", blobEndpoint.ToString(),
                ContainerName, blobId.ToLower());

            // Upload the new image as a BLOB from the string.
            CloudBlockBlob blobFromSASCredential =
                container.GetBlockBlobReference(blobId.ToLower());
            blobFromSASCredential.UploadTextAsync(blobData);

            return avatarUri;
        }
示例#3
0
        private async Task SetTodoItemForImageUpload(AzureStorageParams asparams)
        {
            string storageAccountName;
            string storageAccountKey;

            MobileAppSettingsDictionary settings = this.Configuration.GetMobileAppSettingsProvider().GetMobileAppSettings();

            // Try to get the Azure storage account token from app settings.
            if (!(settings.TryGetValue("STORAGE_ACCOUNT_NAME", out storageAccountName) |
                  settings.TryGetValue("STORAGE_ACCOUNT_ACCESS_KEY", out storageAccountKey)))
            {
                ITraceWriter traceWriter = this.Configuration.Services.GetTraceWriter();
                traceWriter.Error("Could not retrieve storage account settings.");
            }

            // Set the URI for the Blob Storage service.
            Uri blobEndpoint = new Uri(string.Format("https://{0}.blob.core.windows.net", storageAccountName));

            // Create the BLOB service client.
            CloudBlobClient blobClient = new CloudBlobClient(blobEndpoint,
                                                             new StorageCredentials(storageAccountName, storageAccountKey));

            if (asparams.ContainerName != null)
            {
                // Set the BLOB store container name on the item, which must be lowercase.
                asparams.ContainerName = asparams.ContainerName.ToLower();

                // Create a container, if it doesn't already exist.
                CloudBlobContainer container = blobClient.GetContainerReference(asparams.ContainerName);
                await container.CreateIfNotExistsAsync();

                // Create a shared access permission policy.
                BlobContainerPermissions containerPermissions = new BlobContainerPermissions();

                // Enable anonymous read access to BLOBs.
                containerPermissions.PublicAccess = BlobContainerPublicAccessType.Blob;
                container.SetPermissions(containerPermissions);

                // Define a policy that gives write access to the container for 5 minutes.
                SharedAccessBlobPolicy sasPolicy = new SharedAccessBlobPolicy()
                {
                    SharedAccessStartTime  = DateTime.UtcNow,
                    SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(5),
                    Permissions            = SharedAccessBlobPermissions.Write
                };

                // Get the SAS as a string.
                asparams.SasQueryString = container.GetSharedAccessSignature(sasPolicy);

                // Set the URL used to store the image.
                asparams.AzureImageUri = string.Format("{0}{1}/{2}", blobEndpoint.ToString(),
                                                       asparams.ContainerName, asparams.ResourceName);
            }
        }
        // GET api/PopularBeers
        public async Task <List <Beer> > Get(double longitude, double latitude)
        {
            //Find the current country of the user
            string bingKey;

            if (!(settings.TryGetValue("BING_API_KEY", out bingKey)))
            {
                tracer.Error("Could not retrieve Bing API key.");
                bingKey = "AlPB42X199-b_n7tnHPSNM15E4cvLv18hfj4upv3irWgSFHx5GplSaOS3wpggCox";
            }
            tracer.Info($"Bing API Key{bingKey}");

            return(null);
        }
示例#5
0
        public static bool InsureBreweryDbIsInitialized(MobileAppSettingsDictionary appSettings, ITraceWriter logger)
        {
            settings = appSettings;
            tracer = logger;

            if (string.IsNullOrEmpty(BreweryDB.BreweryDBClient.ApplicationKey))
            {
                string apiKey;
                // Try to get the BreweryDB API key  app settings.  
                if (!(settings.TryGetValue("BREWERYDB_API_KEY", out apiKey)))
                {
                    tracer.Error("Could not retrieve BreweryDB API key.");
                    return false;
                }
                tracer.Info($"BreweryDB API Key {apiKey}");
                BreweryDB.BreweryDBClient.Initialize(apiKey);
            }
            return true;
        }
示例#6
0
        public static bool InsureBreweryDbIsInitialized(MobileAppSettingsDictionary appSettings, ITraceWriter logger)
        {
            settings = appSettings;
            tracer   = logger;

            if (string.IsNullOrEmpty(BreweryDB.BreweryDBClient.ApplicationKey))
            {
                string apiKey;
                // Try to get the BreweryDB API key  app settings.
                if (!(settings.TryGetValue("BREWERYDB_API_KEY", out apiKey)))
                {
                    tracer.Error("Could not retrieve BreweryDB API key.");
                    return(false);
                }
                tracer.Info($"BreweryDB API Key {apiKey}");
                BreweryDB.BreweryDBClient.Initialize(apiKey);
            }
            return(true);
        }
示例#7
0
        // GET api/BeerInfo
        public async Task <BeerInfo> Get(string userId, string beerId)
        {
            if (string.IsNullOrEmpty(BreweryDB.BreweryDBClient.ApplicationKey))
            {
                string apiKey;
                // Try to get the BreweryDB API key  app settings.
                if (!(settings.TryGetValue("BREWERYDB_API_KEY", out apiKey)))
                {
                    tracer.Error("Could not retrieve BreweryDB API key.");
                    return(null);
                }
                tracer.Info(string.Format("BreweryDB API Key {0}", apiKey));
                BreweryDB.BreweryDBClient.Initialize(apiKey);
            }

            try
            {
                var beer = await new BreweryDB.BreweryDBClient().QueryBeerById(beerId);
                if (beer != null)
                {
                    var beerInfo = new BeerInfo();
                    beerInfo.Name        = beer.Name;
                    beerInfo.BreweryDBId = beer.Id;
                    var beerCheckins = Context.CheckInItems.Where(f => f.BeerId == beer.Id);
                    beerInfo.CheckIns      = beerCheckins.Where(f => f.CheckedInBy == userId);
                    beerInfo.AverageRating = Math.Round(beerCheckins.Select(f => f.Rating).Average(), 1);
                    beerInfo.Reviews       = Context.ReviewItems.Where(f => f.BeerId == beer.Id);
                    beerInfo.ImagesURLs    = Context.BinaryItems.Where(f => f.ObjectId == beer.Id).Select(f => f.BinaryUrl);

                    return(beerInfo);
                }
            }
            catch (Exception ex)
            {
                tracer.Error(ex.Message);
            }

            return(null);
        }