コード例 #1
0
        public IHttpActionResult GetSasLink(string region, string blobName, string operations)
        {
            string url = "";

            if (!string.IsNullOrEmpty(region))
            {
                var account = AzureSpeedData.Accounts.FirstOrDefault(v => v.Region == region);
                if (account != null)
                {
                    var storageAccount = StorageUtils.CreateCloudStorageAccount(account, true);
                    var blobClient     = storageAccount.CreateCloudBlobClient();
                    var container      = blobClient.GetContainerReference(Constants.PrivateContainerName);
                    var blob           = container.GetBlockBlobReference(blobName);
                    var permissions    = SharedAccessBlobPermissions.None;
                    if (operations.ToLower().Contains("upload"))
                    {
                        permissions |= SharedAccessBlobPermissions.Write;
                    }
                    if (operations.ToLower().Contains("download"))
                    {
                        permissions |= SharedAccessBlobPermissions.Read;
                    }
                    url = StorageUtils.GetSasUrl(blob, permissions);
                }
            }
            return(Ok(url));
        }
コード例 #2
0
        public JsonResult GetSasLinks(string[] regions, string blobName, string operations)
        {
            List <SasUrl> result = new List <SasUrl>();

            foreach (var account in AzureSpeedData.Accounts)
            {
                if (regions.Length > 0 && regions.Contains(account.Region))
                {
                    var storageAccount = StorageUtils.CreateCloudStorageAccount(account);
                    var blobClient     = storageAccount.CreateCloudBlobClient();
                    var container      = blobClient.GetContainerReference("azurespeed");
                    var blob           = container.GetBlockBlobReference(blobName);
                    var permissions    = SharedAccessBlobPermissions.None;
                    if (operations.ToLower().Contains("upload"))
                    {
                        permissions |= SharedAccessBlobPermissions.Write;
                    }
                    if (operations.ToLower().Contains("download"))
                    {
                        permissions |= SharedAccessBlobPermissions.Read;
                    }
                    string url = StorageUtils.GetSasUrl(blob, permissions);
                    result.Add(new SasUrl {
                        Storage = account.Name, Url = url
                    });
                }
            }
            return(Json(result, JsonRequestBehavior.AllowGet));
        }
コード例 #3
0
 public IHttpActionResult DeleteOutDatedBlobs()
 {
     foreach (var account in AzureSpeedData.Accounts)
     {
         var storageAccount = StorageUtils.CreateCloudStorageAccount(account);
         var blobClient     = storageAccount.CreateCloudBlobClient();
         var container      = blobClient.GetContainerReference(Constants.PrivateContainerName);
         var blobs          = container.ListBlobs();
         var oneMonthAgo    = DateTimeOffset.Now.AddMonths(-1);
         foreach (IListBlobItem blob in blobs)
         {
             var cblob = blob as ICloudBlob;
             if (cblob != null && cblob.Name != "callback.js" && cblob.Name != "100MB.bin")
             {
                 if (cblob.Properties.LastModified.Value.CompareTo(oneMonthAgo) < 0)
                 {
                     cblob.DeleteAsync();
                 }
             }
         }
     }
     return(Ok());
 }
コード例 #4
0
 public HttpStatusCodeResult DeleteOutDatedBlobs()
 {
     foreach (var account in AzureSpeedData.Accounts)
     {
         var storageAccount = StorageUtils.CreateCloudStorageAccount(account);
         var blobClient     = storageAccount.CreateCloudBlobClient();
         var container      = blobClient.GetContainerReference("azurespeed");
         var blobs          = container.ListBlobs();
         var aDayAgo        = DateTimeOffset.Now.AddDays(-1);
         foreach (IListBlobItem blob in blobs)
         {
             var cblob = blob as ICloudBlob;
             if (cblob != null && cblob.Name != "callback.js" && cblob.Name != "100MB.bin")
             {
                 if (cblob.Properties.LastModified.Value.CompareTo(aDayAgo) < 0)
                 {
                     cblob.DeleteAsync();
                 }
             }
         }
     }
     return(new HttpStatusCodeResult(200, "Delete Successfully"));
 }
コード例 #5
0
        public string EnableStorageCORS(Account account)
        {
            try
            {
                var storageAccount = StorageUtils.CreateCloudStorageAccount(account);
                var blobClient     = storageAccount.CreateCloudBlobClient();

                CorsHttpMethods allowedMethods = CorsHttpMethods.None;
                allowedMethods = allowedMethods | CorsHttpMethods.Get;
                allowedMethods = allowedMethods | CorsHttpMethods.Put;
                allowedMethods = allowedMethods | CorsHttpMethods.Post;
                allowedMethods = allowedMethods | CorsHttpMethods.Delete;
                allowedMethods = allowedMethods | CorsHttpMethods.Options;

                var          delimiter      = new[] { "," };
                CorsRule     corsRule       = new CorsRule();
                const string allowedOrigins = "*";
                const string allowedHeaders = "*";
                const string exposedHeaders = "";

                string[] allAllowedOrigin = allowedOrigins.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
                string[] allExpHeaders    = exposedHeaders.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
                string[] allAllowHeaders  = allowedHeaders.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);

                List <string> corsAllowedOrigin = new List <string>();
                foreach (var item in allAllowedOrigin)
                {
                    if (!string.IsNullOrWhiteSpace(item))
                    {
                        corsAllowedOrigin.Add(item.Trim());
                    }
                }
                List <string> corsExposedHeaders = new List <string>();
                foreach (var item in allExpHeaders)
                {
                    if (!string.IsNullOrWhiteSpace(item))
                    {
                        corsExposedHeaders.Add(item.Trim());
                    }
                }
                List <string> corsAllowHeaders = new List <string>();
                foreach (var item in allAllowHeaders)
                {
                    if (!string.IsNullOrWhiteSpace(item))
                    {
                        corsAllowHeaders.Add(item.Trim());
                    }
                }
                corsRule.MaxAgeInSeconds = 200;
                corsRule.AllowedMethods  = allowedMethods;
                corsRule.AllowedHeaders  = corsAllowHeaders;
                corsRule.AllowedOrigins  = corsAllowedOrigin;
                corsRule.ExposedHeaders  = corsExposedHeaders;
                ServiceProperties properties = blobClient.GetServiceProperties();
                properties.Cors.CorsRules.Clear();
                properties.Cors.CorsRules.Add(corsRule);
                blobClient.SetServiceProperties(properties);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return("Failed due to Incorrect Account Name or Key.");
            }
            return("Enabling CORS Succeed");
        }