예제 #1
1
		private CloudBlobContainer GetContainerReference(string storageName, string storageKey, string containerName)
		{
			var credentials = new StorageCredentials(storageName, storageKey);
			var account = new CloudStorageAccount(credentials, true);
			var blobClient = account.CreateCloudBlobClient();
			return blobClient.GetContainerReference(containerName);
		}
        public CloudBlobContainer GetBlobContainer(string container)
        {
            Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(
                StorageConnectionString);

            ServicePoint tableServicePoint = ServicePointManager.FindServicePoint(storageAccount.TableEndpoint);

            tableServicePoint.UseNagleAlgorithm = false;

            Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            // Retrieve a reference to a container.
            CloudBlobContainer blobContainer = blobClient.GetContainerReference(container);

            // Create the container if it doesn't already exist.
            blobContainer.CreateIfNotExistsAsync().Wait();

            blobContainer.SetPermissionsAsync(
                new BlobContainerPermissions
            {
                PublicAccess =
                    BlobContainerPublicAccessType.Blob
            }).Wait();

            return(blobContainer);
        }
예제 #3
0
 public AzureService(string account, string azureKey, string blobUri)
 {
     _account = account;
     var storageAccount = new CloudStorageAccount(new StorageCredentials(account, azureKey), new Uri(blobUri),
                                                  null, null);
     _client = storageAccount.CreateCloudBlobClient();
 }
        internal void UploadFromString(string accountName, string accountKey, string containerName, string fileName, string sourceFileName, string fileContentType, string content)   //, string name, string fileDescription) {
        {
            Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount =
                Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(
                    string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1};BlobEndpoint=https://{0}.blob.core.windows.net/", accountName, accountKey)
                    );
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient    blobClient = storageAccount.CreateCloudBlobClient();
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container  = blobClient.GetContainerReference(containerName);
            container.CreateIfNotExists();
            string ext = System.IO.Path.GetExtension(sourceFileName);

            //string fileName = String.Format(

            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
            blockBlob.Properties.ContentType = fileContentType;
            //blockBlob.Metadata.Add("name", name);
            blockBlob.Metadata.Add("originalfilename", sourceFileName);
            //blockBlob.Metadata.Add("userid", userId.ToString());
            //blockBlob.Metadata.Add("ownerid", userId.ToString());
            DateTime created = DateTime.UtcNow;

            // https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
            // http://stackoverflow.com/questions/114983/given-a-datetime-object-how-do-i-get-a-iso-8601-date-in-string-format
            //blockBlob.Metadata.Add("username", userName);
            blockBlob.Metadata.Add("created", created.ToString("yyyy-MM-ddTHH:mm:ss"));  // "yyyy-MM-ddTHH:mm:ssZ"
            blockBlob.Metadata.Add("modified", created.ToString("yyyy-MM-ddTHH:mm:ss")); // "yyyy-MM-ddTHH:mm:ssZ"
            blockBlob.Metadata.Add("fileext", ext);

            blockBlob.UploadText(content, Encoding.UTF8); // .UploadFromStream(fileInputStream);

            blockBlob.SetMetadata();
        }
 private CloudBlobClient GetStorageClient()
 {
     var accountName = StorageAccountName.Contains(".") ? StorageAccountName.Substring(0, StorageAccountName.IndexOf('.')) : StorageAccountName;
     var storageCredentials = new StorageCredentials(accountName, StorageAccountKey);
     var storageAccount = new CloudStorageAccount(storageCredentials, true);
     return storageAccount.CreateCloudBlobClient();
 }
        private static void configureCors(CloudStorageAccount storageAccount)
        {
            var blobClient = storageAccount.CreateCloudBlobClient();

            Console.WriteLine("Storage Account: " + storageAccount.BlobEndpoint);
            var newProperties = CurrentProperties(blobClient);

            newProperties.DefaultServiceVersion = "2013-08-15";
            blobClient.SetServiceProperties(newProperties);

            var addRule = true;
            if (addRule)
            {
                var ruleWideOpenWriter = new CorsRule()
                {
                    AllowedHeaders = ALLOWED_CORS_HEADERS,
                    AllowedOrigins = ALLOWED_CORS_ORIGINS,
                    AllowedMethods = ALLOWED_CORS_METHODS,
                    MaxAgeInSeconds = (int)TimeSpan.FromDays(ALLOWED_CORS_AGE_DAYS).TotalSeconds
                };
                newProperties.Cors.CorsRules.Clear();
                newProperties.Cors.CorsRules.Add(ruleWideOpenWriter);
                blobClient.SetServiceProperties(newProperties);

                Console.WriteLine("New Properties:");
                CurrentProperties(blobClient);

                Console.ReadLine();
            }
        }
예제 #7
0
        public bool downloadSong(int song_id, string song_name, string song_path)
        {
            bool flag = false;

            //hace la cuenta
            StorageCredentials creds = new StorageCredentials(accountName, accountKey);
            CloudStorageAccount account = new CloudStorageAccount(creds, useHttps: true);

            //crea el cliente
            CloudBlobClient client = account.CreateCloudBlobClient();

            //crae el contenedor
            CloudBlobContainer sampleContainer = client.GetContainerReference("music");

            CloudBlockBlob blob = sampleContainer.GetBlockBlobReference(song_id.ToString() + ".mp3");

            try
            {
                //FileIOPermission permission = new FileIOPermission(FileIOPermissionAccess.AllAccess, "C:\\Users\\Andres\\Music");
                Console.WriteLine("Path: {0}", song_path + "\\" + song_name);
                Stream outputFile = new FileStream(song_path + "\\" + song_name, FileMode.Create);

                blob.DownloadToStream(outputFile);
                flag = true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                flag = false;
            }

            return flag;
        }
예제 #8
0
        /// <summary>
        /// Constructs a container shared access signature.
        /// </summary>
        /// <param name="storageAccountName">The Azure Storage account name.</param>
        /// <param name="storageAccountKey">The Azure Storage account key.</param>
        /// <param name="storageEndpoint">The Azure Storage endpoint.</param>
        /// <param name="containerName">The container name to construct a SAS for.</param>
        /// <returns>The container URL with the SAS.</returns>
        public static string ConstructContainerSas(
            string storageAccountName,
            string storageAccountKey,
            string storageEndpoint,
            string containerName)
        {
            //Lowercase the container name because containers must always be all lower case
            containerName = containerName.ToLower();

            StorageCredentials credentials = new StorageCredentials(storageAccountName, storageAccountKey);
            CloudStorageAccount storageAccount = new CloudStorageAccount(credentials, storageEndpoint, true);

            CloudBlobClient client = storageAccount.CreateCloudBlobClient();

            CloudBlobContainer container = client.GetContainerReference(containerName);

            DateTimeOffset sasStartTime = DateTime.UtcNow;
            TimeSpan sasDuration = TimeSpan.FromHours(2);
            DateTimeOffset sasEndTime = sasStartTime.Add(sasDuration);

            SharedAccessBlobPolicy sasPolicy = new SharedAccessBlobPolicy()
                                                   {
                                                       Permissions = SharedAccessBlobPermissions.Read,
                                                       SharedAccessExpiryTime = sasEndTime
                                                   };

            string sasString = container.GetSharedAccessSignature(sasPolicy);
            return string.Format("{0}{1}", container.Uri, sasString); ;
        }
예제 #9
0
        /// <summary>
        /// Occurs when a storage provider operation has completed.
        /// </summary>
        //public event EventHandler<StorageProviderEventArgs> StorageProviderOperationCompleted;

        #endregion

        // Initialiser method
        private void Initialise(string storageAccount, string containerName)
        {
            if (String.IsNullOrEmpty(containerName))
                throw new ArgumentException("You must provide the base Container Name", "containerName");
            
            ContainerName = containerName;

            _account = CloudStorageAccount.Parse(storageAccount);
            _blobClient = _account.CreateCloudBlobClient();
            _container = _blobClient.GetContainerReference(ContainerName);
            try
            {
                _container.FetchAttributes();
            }
            catch (StorageException)
            {
                Trace.WriteLine(string.Format("Create new container: {0}", ContainerName), "Information");
                _container.Create();

                // set new container's permissions
                // Create a permission policy to set the public access setting for the container. 
                BlobContainerPermissions containerPermissions = new BlobContainerPermissions();

                // The public access setting explicitly specifies that the container is private,
                // so that it can't be accessed anonymously.
                containerPermissions.PublicAccess = BlobContainerPublicAccessType.Off;

                //Set the permission policy on the container.
                _container.SetPermissions(containerPermissions);
            }
        }
예제 #10
0
        public bool uploadSong(int song_id, string song_path)
        {
            bool flag = false;

            //hace la cuenta
            StorageCredentials creds = new StorageCredentials(accountName, accountKey);
            CloudStorageAccount account = new CloudStorageAccount(creds, useHttps: true);

            //crea el cliente
            CloudBlobClient client = account.CreateCloudBlobClient();

            //crae el contenedor
            CloudBlobContainer container = client.GetContainerReference("music");
            container.CreateIfNotExists();
            //
            CloudBlockBlob blob = container.GetBlockBlobReference(song_id.ToString() + ".mp3");
            using (System.IO.Stream file = System.IO.File.OpenRead(song_path))
            {
                try
                {
                    blob.UploadFromStream(file);
                    flag = true;

                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    flag = false;
                }

            }

            return flag;
        }
예제 #11
0
        public bool Post(CommitBlobRequest commitRequest)
        {

            var result = false;

            commitRequest.FileName = commitRequest.FileName.ToLower();
            commitRequest.ContainerName = commitRequest.ContainerName.ToLower();
            var accountAndKey = new StorageCredentials(AppSettings.StorageAccountName, AppSettings.StorageAccountKey);
            var storageaccount = new CloudStorageAccount(accountAndKey, true);
            var blobClient = storageaccount.CreateCloudBlobClient();
            CloudBlobContainer container = blobClient.GetContainerReference(commitRequest.ContainerName);
            container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
            CloudBlockBlob blob = container.GetBlockBlobReference(commitRequest.FileName);

            try
            {
                blob.PutBlockList(commitRequest.blobParts);
                blob.Properties.ContentType = commitRequest.type;
                blob.Properties.ContentDisposition = "attachment";
                blob.SetProperties();

                result = true;
            }
            catch (Exception ex)
            {
                //Trace.TraceError("BuildFileSasUrl throw excaption", ex.Message);
            }
              

            return result;



        }
예제 #12
0
    public static bool DeleteBlob(string blobName)
    {
        //http://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-blobs/#delete-blobs

        string accountKey = "",
               accountName = "",
               blobContainer = "";
        try
        {
            CloudStorageAccount storageAccount = new CloudStorageAccount(new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(accountName, accountKey), true);

            // Create the blob client.
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            // Retrieve reference to a previously created container.
            CloudBlobContainer container = blobClient.GetContainerReference(blobContainer);

            // Retrieve reference to a blob named "myblob.txt".
            CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName);

            // Delete the blob.
            blockBlob.Delete();
        }
        catch (Exception)
        {
            return false;
        }
        return true;
    }
예제 #13
0
        public BlobFileProvider(IEnumerable<string> locations)
            : base()
        {
            _storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);
            _blobClient = _storageAccount.CreateCloudBlobClient();
            _container = _blobClient.GetContainerReference("data");
            _container.CreateIfNotExists();

            _strings = new List<string>();
            foreach(string location in locations) {
                foreach (IListBlobItem item in _container.ListBlobs(location,true))
                {
                    if (item.GetType() == typeof(CloudBlockBlob))
                    {
                        CloudBlockBlob blob = (CloudBlockBlob)item;
                        string text;
                        using (var memoryStream = new MemoryStream())
                        {
                            blob.DownloadToStream(memoryStream);
                            text = Encoding.UTF8.GetString(memoryStream.ToArray());
                            if (text[0] == _byteOrderMarkUtf8[0])
                            {
                               text= text.Remove(0,_byteOrderMarkUtf8.Length);
                            }
                            _strings.Add(text);
                        }
                    }
                }
            }
        }
예제 #14
0
        static void Main(string[] args)
        {
            try
            {
                //hace la cuenta
                StorageCredentials creds = new StorageCredentials(accountName, accountKey);
                CloudStorageAccount account = new CloudStorageAccount(creds, useHttps: true);
                //crea el cliente
                CloudBlobClient client = account.CreateCloudBlobClient();
                //crae el contenedor
                CloudBlobContainer sampleContainer = client.GetContainerReference("music");
                sampleContainer.CreateIfNotExists();
                //
                CloudBlockBlob blob = sampleContainer.GetBlockBlobReference("9.mp3");
                using (System.IO.Stream file = System.IO.File.OpenRead("C:\\Users\\Andres\\Downloads\\9.mp3"))
                {
                    blob.UploadFromStream(file);
                }

                /*CloudBlockBlob blob = sampleContainer.GetBlockBlobReference("APictureFile.jpg");
                using (Stream outputFile = new FileStream("Downloaded.jpg", FileMode.Create))
                {
                    blob.DownloadToStream(outputFile);
                }*/

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            Console.Read();
        }
        public static bool ValidateAccountAccessible(CloudStorageAccount account)
        {
            if (account == null)
            {
                throw new ArgumentNullException("account");
            }

            // Verify the credentials are correct.
            // Have to actually ping a storage operation.
            var client = account.CreateCloudBlobClient();

            try
            {
                // This can hang for a long time if the account name is wrong. 
                // If will fail fast if the password is incorrect.
                client.GetServiceProperties();
            }
            catch (OperationCanceledException)
            {
                throw;
            }
            catch
            {
                return false;
            }

            return true;
        }
 private static void InitStorage()
 {
     var credentials = new StorageCredentials(AppKeys.Storage_Account_Name, AppKeys.PrimaryAccessKey);
     var storageAccount = new CloudStorageAccount(credentials, true);
     var blobClient = storageAccount.CreateCloudBlobClient();
     imagesContainer = blobClient.GetContainerReference("images");
 }
 private static void InitStorage()
 {
     var credentials = new StorageCredentials(resources.AccountName, resources.AccountKey);
     var storageAccount = new CloudStorageAccount(credentials, true);
     var blobClient = storageAccount.CreateCloudBlobClient();
     imagesContainer = blobClient.GetContainerReference("images");
 }
        private static IDictionary<string, int> GetRanking(CloudStorageAccount storageAccount, string blobName)
        {
            IDictionary<string, int> ranking = new Dictionary<string, int>();

            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer container = blobClient.GetContainerReference("ranking");

            CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName);

            MemoryStream stream = new MemoryStream();

            blockBlob.DownloadToStream(stream);

            stream.Seek(0, SeekOrigin.Begin);

            using (TextReader textReader = new StreamReader(stream))
            {
                using (JsonReader jsonReader = new JsonTextReader(textReader))
                {
                    JArray array = JArray.Load(jsonReader);

                    foreach (JObject item in array)
                    {
                        ranking.Add(item["id"].ToString(), item["rank"].ToObject<int>());
                    }
                }
            }

            return ranking;
        }
예제 #19
0
        public AzureFileSystem(string containerName, string root, bool isPrivate, CloudStorageAccount storageAccount) {
            // Setup the connection to custom storage accountm, e.g. Development Storage
            _storageAccount = storageAccount;
            ContainerName = containerName;
            _root = String.IsNullOrEmpty(root) ? "": root + "/";
            _absoluteRoot = Combine(Combine(_storageAccount.BlobEndpoint.AbsoluteUri, containerName), _root);

            //using ( new HttpContextWeaver() ) 
            {

                BlobClient = _storageAccount.CreateCloudBlobClient();
                // Get and create the container if it does not exist
                // The container is named with DNS naming restrictions (i.e. all lower case)
                Container = BlobClient.GetContainerReference(ContainerName);

                Container.CreateIfNotExists();

                Container.SetPermissions(isPrivate
                                             ? new BlobContainerPermissions
                                                   {PublicAccess = BlobContainerPublicAccessType.Off}
                                             : new BlobContainerPermissions
                                                   {PublicAccess = BlobContainerPublicAccessType.Blob}); // deny listing 
            }

        }
예제 #20
0
        public async Task WriteAuditRecord(string resourceType, CloudStorageAccount storageAccount)
        {
            var entry = new AuditEntry(
                this,
                await AuditActor.GetCurrentMachineActor());

            // Write the blob to the storage account
            var client = storageAccount.CreateCloudBlobClient();
            var container = client.GetContainerReference("auditing");
            await container.CreateIfNotExistsAsync();
            var blob = container.GetBlockBlobReference(
                resourceType + "/" + this.GetPath() + "/" + DateTime.UtcNow.ToString("s") + "-" + this.GetAction().ToLowerInvariant() + ".audit.v1.json");

            if (await blob.ExistsAsync())
            {
                throw new InvalidOperationException(String.Format(
                    CultureInfo.CurrentCulture,
                    Strings.Package_DeleteCommand_AuditBlobExists,
                    blob.Uri.AbsoluteUri));
            }

            byte[] data = Encoding.UTF8.GetBytes(
                JsonFormat.Serialize(entry));
            await blob.UploadFromByteArrayAsync(data, 0, data.Length);
        }
 public AzureBlobStorageProvider(CloudStorageAccount storageAccount)
 {
     _storageAccount = storageAccount;
     BlobClient = _storageAccount.CreateCloudBlobClient();
     Containers = new List<CloudBlobContainer>();
     ContainerFactory = CreateContainer;
 }
        public static void Run(string connectionString, bool disableLogging)
        {
            _connectionString = connectionString;
            _storageAccount = CloudStorageAccount.Parse(connectionString);
            _blobClient = _storageAccount.CreateCloudBlobClient();

            Console.WriteLine("Creating the test blob...");
            CreateTestBlob();

            try
            {
                TimeSpan azureSDKTime = RunAzureSDKTest();
                TimeSpan webJobsSDKTime = RunWebJobsSDKTest(disableLogging);

                // Convert to ulong because the measurment block does not support other data type
                ulong perfRatio = (ulong)((webJobsSDKTime.TotalMilliseconds / azureSDKTime.TotalMilliseconds) * 100);

                Console.WriteLine("--- Results ---");
                Console.WriteLine("Azure SDK:   {0} ms: ", azureSDKTime.TotalMilliseconds);
                Console.WriteLine("WebJobs SDK: {0} ms: ", webJobsSDKTime.TotalMilliseconds);

                Console.WriteLine("Perf ratio (x100, long): {0}", perfRatio);

                MeasurementBlock.Mark(
                    perfRatio,
                    (disableLogging ? BlobNoLoggingOverheadMetric : BlobLoggingOverheadMetric) + ";Ratio;Percent");
            }
            finally
            {
                Cleanup();
            }
        }
예제 #23
0
        public CloudBlobContainer GetCloudBlobContainer()
        {
            // Retrieve storage account from connection-string
            //    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
            //CloudConfigurationManager.GetSetting("StorageConnectionString"));
            Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(
                CloudConfigurationManager.GetSetting("StorageConnectionString"));

            // Create the blob client
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            // Retrieve a reference to a container

            CloudBlobContainer blobContainer = blobClient.GetContainerReference(ContainerName);

            blobContainer.CreateIfNotExists();

            blobContainer.SetPermissions(
                new BlobContainerPermissions
            {
                PublicAccess = BlobContainerPublicAccessType.Blob
            });

            return(blobContainer);
        }
예제 #24
0
 public BlobManager(string conStr)
 {
     //RoleEnvironment.GetConfigurationSettingValue("UploadCon")
     Storage = CloudStorageAccount.Parse(conStr);
     BlobClient = Storage.CreateCloudBlobClient();
     QueueClient = Storage.CreateCloudQueueClient();
 }
예제 #25
0
        public void Cleanup()
        {
            StorageCredentials storageCredentials = new StorageCredentials(storageAccountName, storageAccessKey);
            CloudStorageAccount account = new CloudStorageAccount(storageCredentials, true);

            CloudBlobClient blobClient = account.CreateCloudBlobClient();

            CloudBlobContainer container = blobClient.GetContainerReference(containerName);

            Console.WriteLine("Deleting blob storage files in {0}/{1} older than {2} days", storageAccountName, containerName, minDaysOld);

            DateTime referenceDate = DateTime.UtcNow;
            var blobQuery = from b in container.ListBlobs(null, recursive).OfType<ICloudBlob>()
                            where b.Properties.LastModified <= referenceDate.AddDays(-minDaysOld)
                            select b;

            var blobList = blobQuery.ToList();

            if (blobList.Count == 0)
            {
                Console.WriteLine("No files found in {0}/{1} older than {2} days", storageAccountName, containerName, minDaysOld);
                return;
            }

            foreach (ICloudBlob blob in blobList)
            {
                double blobAgeInDays = (referenceDate - blob.Properties.LastModified.Value).TotalDays;
                Console.WriteLine("Deleting blob storage file {0}/{1}, {2} days old", containerName, blob.Name, Math.Round(blobAgeInDays, 3));
                blob.DeleteIfExists();
            }

            Console.WriteLine("{0} blob storage files deleted in {1}/{2} older than {3} days", blobList.Count, storageAccountName, containerName, minDaysOld);
        }
예제 #26
0
        public void when_writting_to_a_pageBlob_all_data_is_correctly_retrieved()
        {
            var azureAccount = new CloudStorageAccount(new StorageCredentials("valeriob", "2SzgTAaG11U0M1gQ19SNus/vv1f0efwYOwZHL1w9YhTKEYsU1ul+s/ke92DOE1wIeCKYz5CuaowtDceUvZW2Rw=="), true);
            var blobClient = azureAccount.CreateCloudBlobClient();
            var container = blobClient.GetContainerReference("test");
            container.CreateIfNotExists();

            var blob = container.GetPageBlobReference(Guid.NewGuid()+"");

            var helper = new AzurePageBlob(blob);
            //helper.Create_if_does_not_exists();
            helper.OpenAsync().Wait();

            var output = "";
            var input = @"The uniqueifier is NULL for the first instance of each customer_id, and is then populated, in ascending order, for each subsequent row with the same customer_id value. The overhead for rows with a NULL uniqueifier value is, unsurprisingly, zero bytes. This is why min_record_size_in_bytes remained unchanged in the overhead table; the first insert had a uniqueifier value of NULL. This is also why it is impossible to estimate how much additional storage overhead will result from the addition of a uniqueifier, without first having a thorough understanding of the data being stored. For example, a non-unique clustered index on a datetime column may have very little overhead if data is inserted, say, once per minute. However, if that same table is receiving thousands of inserts per minute, then it is likely that many rows will share the same datetime value, and so the uniqueifier will have a much higher overhead.

            If your requirements seem to dictate the use of a non-unique clustered key, my advice would be to look to see if there are a couple of relatively narrow columns that, together, can form a unique key. You'll still see the increase in the row size for your clustering key in the index pages of both your clustered and nonclustered indexes, but you'll at least save the cost of the uniqueifier in the data pages of the leaf level of your clustered index. Also, instead of storing an arbitrary uniqueifier value to the index key, which is meaningless in the context of your data, you would be adding meaningful and potentially useful information to all of your nonclustered indexes.

            A good clustered index is also built upon static, or unchanging, columns. That is, you want to choose a clustering key that will never be updated. SQL Server must ensure that data exists in a logical order based upon the clustering key. Therefore, when the clustering key value is updated, the data may need to be moved elsewhere in the clustered index so that the clustering order is maintained. Consider a table with a clustered index on LastName, and two non-clustered indexes, where the last name of an employee must be updated.";

            helper.Append(input);

            using (var stream = helper.OpenReadonlyStream())
            {
                using (var reader = new StreamReader(stream))
                {
                    output = reader.ReadToEnd();
                }
            }

            Assert.AreEqual(input, output);
        }
        /// <summary>
        /// Get the service properties
        /// </summary>
        /// <param name="account">Cloud storage account</param>
        /// <param name="type">Service type</param>
        /// <param name="options">Request options</param>
        /// <param name="operationContext">Operation context</param>
        /// <returns>The service properties of the specified service type</returns>
        public XSCLProtocol.ServiceProperties GetStorageServiceProperties(StorageServiceType type, IRequestOptions options, XSCL.OperationContext operationContext)
        {
            XSCL.CloudStorageAccount account = StorageContext.StorageAccount;
            try
            {
                switch (type)
                {
                case StorageServiceType.Blob:
                    return(account.CreateCloudBlobClient().GetServicePropertiesAsync((BlobRequestOptions)options, operationContext).Result);

                case StorageServiceType.Queue:
                    return(account.CreateCloudQueueClient().GetServicePropertiesAsync((QueueRequestOptions)options, operationContext).Result);

                case StorageServiceType.File:
                    FileServiceProperties          fileServiceProperties = account.CreateCloudFileClient().GetServicePropertiesAsync((FileRequestOptions)options, operationContext).Result;
                    XSCLProtocol.ServiceProperties sp = new XSCLProtocol.ServiceProperties();
                    sp.Clean();
                    sp.Cors          = fileServiceProperties.Cors;
                    sp.HourMetrics   = fileServiceProperties.HourMetrics;
                    sp.MinuteMetrics = fileServiceProperties.MinuteMetrics;
                    return(sp);

                default:
                    throw new ArgumentException(Resources.InvalidStorageServiceType, "type");
                }
            }
            catch (AggregateException e) when(e.InnerException is XSCL.StorageException)
            {
                throw e.InnerException;
            }
        }
예제 #28
0
        public static CloudBlobDirectory GetBlobDirectory(CloudStorageAccount account, string path)
        {
            var client = account.CreateCloudBlobClient();
            client.DefaultRequestOptions = new BlobRequestOptions()
            {
                ServerTimeout = TimeSpan.FromMinutes(5)
            };

            string[] segments = path.Split('/');
            string containerName;
            string prefix;

            if (segments.Length < 2)
            {
                // No "/" segments, so the path is a container and the catalog is at the root...
                containerName = path;
                prefix = String.Empty;
            }
            else
            {
                // Found "/" segments, but we need to get the first segment to use as the container...
                containerName = segments[0];
                prefix = String.Join("/", segments.Skip(1)) + "/";
            }

            var container = client.GetContainerReference(containerName);
            var dir = container.GetDirectoryReference(prefix);
            return dir;
        }
예제 #29
0
        private string FileToUpload(HttpPostedFileBase file, string FolderId, string uniqueBlobName)
        {
            Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = CreateStorageAccountFromConnectionString(Microsoft.Azure.CloudConfigurationManager.GetSetting(StorageProjectFiles));
            CloudBlobClient    blobClient = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer container  = blobClient.GetContainerReference(FolderId);

            try
            {
                container.CreateIfNotExists();
            }
            catch (StorageException)
            {
                throw;
            }

            /*
             * CloudBlockBlob blockBlob = container.GetBlockBlobReference(newinfo.Name);
             * await blockBlob.UploadFromFileAsync(newinfo.FullName, FileMode.Open);
             */
            CloudBlockBlob blockBlob = container.GetBlockBlobReference(uniqueBlobName);

            blockBlob.Properties.ContentType = file.ContentType;
            blockBlob.UploadFromStream(file.InputStream);
            return(blockBlob.Uri.AbsoluteUri);
        }
예제 #30
0
        public BlobManager(){
            try
            {
                storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString"));

                blobClient = storageAccount.CreateCloudBlobClient();
                container = blobClient.GetContainerReference("supstorage");
                container.CreateIfNotExists();
            }
            catch (ArgumentNullException)
            {
                Trace.TraceInformation("CloudStorageAccount Exception null ou vide");
                // Use Application Local Storage Account String
            }
            catch (NullReferenceException)
            {
                Trace.TraceInformation("CloudBlobClient Or CloudBlobContainer Exception");
                // Create Container 
            }
            catch (FormatException)
            {
                Trace.TraceInformation("CloudStorageAccount Exception Connection String Invalid");
            }
            catch (ArgumentException)
            {
                Trace.TraceInformation("CloudStorageAccount Exception connectionString ne peut pas être analysée");
            }
        }
예제 #31
0
        public Uri Upload(SubscriptionCloudCredentials credentials, string storageAccountName, string packageFile, string uploadedFileName)
        {
            var cloudStorage =
                new CloudStorageAccount(new StorageCredentials(storageAccountName, GetStorageAccountPrimaryKey(credentials, storageAccountName)), true);

            var blobClient = cloudStorage.CreateCloudBlobClient();
            var container = blobClient.GetContainerReference(OctopusPackagesContainerName);
            container.CreateIfNotExists();

            var permission = container.GetPermissions();
            permission.PublicAccess = BlobContainerPublicAccessType.Off;
            container.SetPermissions(permission);

            var fileInfo = new FileInfo(packageFile);

            var packageBlob = GetUniqueBlobName(uploadedFileName, fileInfo, container);
            if (packageBlob.Exists())
            {
                Log.VerboseFormat("A blob named {0} already exists with the same length, so it will be used instead of uploading the new package.",
                    packageBlob.Name);
                return packageBlob.Uri;
            }

            UploadBlobInChunks(fileInfo, packageBlob, blobClient);

            Log.Info("Package upload complete");
            return packageBlob.Uri;
        }
예제 #32
0
        public BlobContainer(string name)
        {
            // hämta connectionsträngen från config // RoleEnviroment bestämmer settingvalue runtime
            //var connectionString = RoleEnvironment.GetConfigurationSettingValue("PhotoAppStorage");
            //var connectionString = CloudConfigurationManager.GetSetting("CloudStorageApp");
            // hämtar kontot utfrån connectionsträngens värde
            //var account = CloudStorageAccount.Parse(connectionString);

            //var account = CloudStorageAccount.DevelopmentStorageAccount;

            var cred = new StorageCredentials("jholm",
                "/bVipQ2JxjWwYrZQfHmzhaBx1p1s8BoD/wX6VWOmg4/gpVo/aALrjsDUKqzXsFtc9utepPqe65NposrXt9YsyA==");
            var account = new CloudStorageAccount(cred, true);

            // skapar en blobclient
            _client = account.CreateCloudBlobClient();

            m_BlobContainer = _client.GetContainerReference(name);

            // Om det inte finns någon container med det namnet
            if (!m_BlobContainer.Exists())
            {
                // Skapa containern
                m_BlobContainer.Create();
                var permissions = new BlobContainerPermissions()
                {
                    PublicAccess = BlobContainerPublicAccessType.Blob
                };
                // Sätter public access till blobs
                m_BlobContainer.SetPermissions(permissions);
            }
        }
예제 #33
0
        /// <summary>
        /// Generates the blob container SAS URL.
        /// </summary>
        /// <param name="storageAccount">The storage account.</param>
        /// <param name="dto">The data transfer object.</param>
        private static void GenerateBlobContainerSasUrl(CloudStorageAccount storageAccount, DataDto dto)
        {
            // Create the blob client.
            var blobClient = storageAccount.CreateCloudBlobClient();

            // Use the client's name as container
            var container = blobClient.GetContainerReference("datacontainer");

            // Create the container if it doesn't already exist.
            // TODO: Avoid the next line of code in productive systems, since it makes a call to azure blob storage.
            // It's better to make sure the container always exists from the beginning.
            container.CreateIfNotExists();

            // Set the expiry time and permissions for the container.
            // In this case no start time is specified, so the shared access signature becomes valid immediately.
            var accessBlobPolicy = new SharedAccessBlobPolicy();
            accessBlobPolicy.SharedAccessExpiryTime = DateTime.UtcNow.AddHours(1);
            accessBlobPolicy.Permissions = SharedAccessBlobPermissions.Write;

            // Generate the SAS token. No access policy identifier is used which makes it non revocable.
            // The token is generated without issuing any calls against the Windows Azure Storage.
            string sasToken = container.GetSharedAccessSignature(accessBlobPolicy);

            dto.BlobContainerUrl = container.Uri;
            dto.BlobSasToken = sasToken;
        }
예제 #34
0
        public FileManager(string ContainerName)
        {
            // Check if Container Name is null or empty
            if (string.IsNullOrEmpty(ContainerName))
            {
                throw new ArgumentNullException("ContainerName", "Container Name can't be empty");
            }
            try
            {
                // Get azure table storage connection string.
                KeyVaultManager keyVaultManager  = new KeyVaultManager();
                string          ConnectionString = keyVaultManager.GetValueFromAzureVault(Constants.SecretName);
                //string ConnectionString = "DefaultEndpointsProtocol=https;AccountName=training2blobstorage;AccountKey=d4swk28SyaP0RkzEssi2Q39Zsg6+rFQ8bv/UvzYfgwHXIqbuYjY1yrUWXXoH50YdTuipEZhnwQ+JS93TMNFRCg==;EndpointSuffix=core.windows.net";

                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConnectionString);

                CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
                blobContainer = cloudBlobClient.GetContainerReference(ContainerName);

                // Create the container and set the permission
                if (blobContainer.CreateIfNotExists())
                {
                    blobContainer.SetPermissions(
                        new BlobContainerPermissions
                    {
                        PublicAccess = BlobContainerPublicAccessType.Blob
                    }
                        );
                }
            }
            catch (Exception ExceptionObj)
            {
                throw ExceptionObj;
            }
        }
예제 #35
0
        static public void Upload(string filepath, string blobname, string accountName, string accountKey)
        {
            try
            {
                StorageCredentials creds = new StorageCredentials(accountName, accountKey);
                CloudStorageAccount account = new CloudStorageAccount(creds, useHttps: true);
                CloudBlobClient client = account.CreateCloudBlobClient();
                CloudBlobContainer sampleContainer = client.GetContainerReference("public-samples");
                sampleContainer.CreateIfNotExists();

                // for public access ////
                BlobContainerPermissions permissions = new BlobContainerPermissions();
                permissions.PublicAccess = BlobContainerPublicAccessType.Container;
                sampleContainer.SetPermissions(permissions);
                /////////////////////////

                CloudBlockBlob blob = sampleContainer.GetBlockBlobReference(blobname);
                using (Stream file = File.OpenRead(filepath))
                {
                    blob.UploadFromStream(file);
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
예제 #36
0
        public HomeController()
        {
            storageAccount = CloudStorageAccount.Parse(
            ConfigurationManager.AppSettings["StorageConnectionString"]);

            tableClient = storageAccount.CreateCloudTableClient();

            table = tableClient.GetTableReference("fouramigos");

            table.CreateIfNotExists();

            blobClient = storageAccount.CreateCloudBlobClient();

            container = blobClient.GetContainerReference("fouramigos");

            container.CreateIfNotExists();

            BlobContainerPermissions permissions = container.GetPermissions();
            permissions.PublicAccess = BlobContainerPublicAccessType.Container;
            container.SetPermissions(permissions);


            //lägga till nya
            //var tablemodels = new TableModel("Brutus", "Uggla") { Location = "T4", Description="Uggla i träd", Type="Foto" };
            //var tablemodels1 = new TableModel("brutus", "Örn") { Location = "T4", Description="Örn som flyger", Type = "Foto" };

            //var opreation = TableOperation.Insert(tablemodels);
            //var operation2 = TableOperation.Insert(tablemodels1);

            //table.Execute(opreation);
            //table.Execute(operation2);
        }
예제 #37
0
        private void DeleteBolb(string FileName)
        {
            Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = CreateStorageAccountFromConnectionString(Microsoft.Azure.CloudConfigurationManager.GetSetting(StorageProjectFiles));
            CloudBlobClient    blobClient = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer container  = blobClient.GetContainerReference(barcelona);
            CloudBlockBlob     blockBlob  = container.GetBlockBlobReference(FileName);

            blockBlob.DeleteIfExists();
        }
예제 #38
0
        public CloudBlobContainer GetBlobContainer(string tableName)
        {
            Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse("UseDevelopmentStorage=true");
            CloudBlobClient    blobClient = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer container  = blobClient.GetContainerReference(tableName);

            container.CreateIfNotExistsAsync();
            return(container);
        }
예제 #39
0
        private string GetFileUri(HttpPostedFileBase file)
        {
            Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = CreateStorageAccountFromConnectionString(Microsoft.Azure.CloudConfigurationManager.GetSetting(StorageProjectFiles));
            CloudBlobClient    blobClient = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer container  = blobClient.GetContainerReference(barcelona);
            CloudBlockBlob     blockBlob  = container.GetBlockBlobReference(file.FileName);

            return(blockBlob.Uri.AbsoluteUri);
        }
예제 #40
0
        /// <summary>
        /// This method do the housekeeping stuffs
        /// </summary>
        private static void initialize()
        {
            //Return reference to the storage account
            account = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));

            //credentials = new StorageCredentials(accountName, accountKey);
            //account = new CloudStorageAccount(credentials, useHttps: true);

            cloudBlobClient = account.CreateCloudBlobClient();
        }
예제 #41
0
        public string DownloadPublicBlob(string UserId, string BlobName, bool PublicAccess = true)
        {
            //Retrieve a reference to a container.
            //Retrieve storage account from connection string.
            Microsoft.WindowsAzure.Storage.CloudStorageAccount StorageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(Convert.ToString(ConfigurationManager.AppSettings["StorageConnectionString"]));

            // Create the blob client.
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient blobClient = StorageAccount.CreateCloudBlobClient();

            // Retrieve a reference to a container.
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = blobClient.GetContainerReference(UserId);

            // Create the container if it doesn't exist.
            container.CreateIfNotExists();


            //Set permission to public
            if (PublicAccess)
            {
                container.SetPermissions(
                    new Microsoft.WindowsAzure.Storage.Blob.BlobContainerPermissions
                {
                    PublicAccess =
                        Microsoft.WindowsAzure.Storage.Blob.BlobContainerPublicAccessType.Blob
                });
            }
            else
            {
                container.SetPermissions(
                    new Microsoft.WindowsAzure.Storage.Blob.BlobContainerPermissions
                {
                    PublicAccess =
                        Microsoft.WindowsAzure.Storage.Blob.BlobContainerPublicAccessType.Off
                });
            }



            // Retrieve reference to a blob named

            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob blockBlob = container.GetBlockBlobReference(BlobName);

            //var sasToken = blockBlob.GetSharedAccessSignature(new SharedAccessBlobPolicy()
            //{
            //    Permissions = SharedAccessBlobPermissions.Read,
            //    SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(10),//assuming the blob can be downloaded in 10 miinutes
            //}, new SharedAccessBlobHeaders()
            //{
            //    ContentDisposition = "attachment; filename=file-name"
            //});

            //return string.Format("{0}{1}", blockBlob.Uri, sasToken);

            return(blockBlob.Uri.ToString());
        }
예제 #42
0
        private void LogError(Exception ex, string message)
        {
            //ConfigurationManager.ConnectionStrings["CineStorageConStr"].ConnectionString
            Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("CineStorageConStr"));

            Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            // Retrieve a reference to a container.
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = blobClient.GetContainerReference("data");

            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob logBlob = container.GetBlockBlobReference(String.Format(this.LogFile, DateTime.UtcNow.ToString("dd MMM yyyy")));
            try
            {
                using (StreamReader sr = new StreamReader(logBlob.OpenRead()))
                {
                    using (StreamWriter sw = new StreamWriter(logBlob.OpenWrite()))
                    {
                        sw.Write(sr.ReadToEnd());

                        if (ex != null)
                        {
                            sw.Write(System.Environment.NewLine);

                            sw.WriteLine(ex.Message);
                            sw.WriteLine(ex.StackTrace);

                            sw.Write(System.Environment.NewLine);

                            if (ex.InnerException != null)
                            {
                                sw.Write(System.Environment.NewLine);

                                sw.WriteLine(ex.InnerException.Message);
                                sw.WriteLine(ex.InnerException.StackTrace);

                                sw.Write(System.Environment.NewLine);
                            }
                        }

                        if (message != null)
                        {
                            sw.Write(System.Environment.NewLine);

                            sw.WriteLine(message);

                            sw.Write(System.Environment.NewLine);
                        }
                    }
                }
            }
            catch { }
        }
        public Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient GetBlobClient()
        {
            Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(
                StorageConnectionString);

            ServicePoint tableServicePoint = ServicePointManager.FindServicePoint(storageAccount.TableEndpoint);

            tableServicePoint.UseNagleAlgorithm = false;

            Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            return(blobClient);
        }
예제 #44
0
파일: Logs.cs 프로젝트: tsangtmc/BlobIM
        /// <summary>
        /// Enable logging for storage account
        /// </summary>
        /// <param name="accountname"></param>
        /// <param name="accountkey"></param>
        /// <param name="SnapshotFileName"></param>
        public static void EnableLogging(string accountname, string accountkey, string SnapshotFileName)
        {
            Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=" + accountname + ";AccountKey=" + accountkey);

            //var queueClient = storageAccount.CreateCloudQueueClient();
            var blobClient        = storageAccount.CreateCloudBlobClient();
            var serviceProperties = blobClient.GetServiceProperties();

            serviceProperties.Logging.LoggingOperations = Microsoft.WindowsAzure.Storage.Shared.Protocol.LoggingOperations.All;
            serviceProperties.Logging.RetentionDays     = 2;

            blobClient.SetServiceProperties(serviceProperties);
        }
예제 #45
0
        /// <summary>
        /// Creates a new instance for the monitor
        /// </summary>
        public ConcurrencyMonitor(string leaseConnectionString)
        {
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(leaseConnectionString);
            CloudBlobClient     blobClient     = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer  container      = blobClient.GetContainerReference("leasecontainer");

            container.CreateIfNotExists();
            blob = container.GetBlockBlobReference("leaseblob");
            if (!Exists(blob))
            {
                using (Stream str = new MemoryStream(new byte[] { 0 }))
                {
                    blob.UploadFromStream(str);
                }
            }
        }
예제 #46
0
        /*
         * listContainers():
         * - First connects to a storage account
         * - then lists the containers
         * - then higlights the 1st container,
         *   which triggers event handler that does blob and policy enumeration
         */
        private void listContainers()
        {
            if (accountText.Text.Length > 0 && keyText.Text.Length > 0)
            {
                accountName = accountText.Text;
                accountKey  = keyText.Text;
            }
            else
            {
                MessageBox.Show("Go to Settings tab and make sure Azure storage account and key are set",
                                "Missing storage settings");
                return;
            }
            try
            {
                Uri blobEndpoint = new Uri("https://" + accountName + ".blob.core.windows.net/");

                // create storage account object and blob client
                Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount =
                    new Microsoft.WindowsAzure.Storage.CloudStorageAccount(new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(accountName, accountKey), true);
                blobClient = storageAccount.CreateCloudBlobClient();

                //List all containers in this storage account.
                containerList.Items.Clear();
                foreach (var container in blobClient.ListContainers())
                {
                    containerList.Items.Add(container.Name);
                }
            }
            catch (Exception Ex)
            {
                MessageBox.Show("Invalid cloud blob client: " + Ex.Message +
                                " - check storage account credentials",
                                "Blob client error");
                return;
            }
            // highlight the first listbox item
            if (containerList.Items.Count > 0)
            {
                containerList.SetSelected(0, true);
            }
        }
        public string GetTempUrl(string accountName, string accountKey, string fullPath)
        {
            Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount =
                Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(
                    string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1};BlobEndpoint=https://{0}.blob.core.windows.net/", accountName, accountKey)
                    );

            Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            var blob = blobClient.GetBlobReferenceFromServer(new Uri(fullPath));

            var readPolicy = new Microsoft.WindowsAzure.Storage.Blob.SharedAccessBlobPolicy()
            {
                Permissions            = Microsoft.WindowsAzure.Storage.Blob.SharedAccessBlobPermissions.Read, // SharedAccessPermissions.Read,
                SharedAccessExpiryTime = DateTime.UtcNow + TimeSpan.FromMinutes(10)
            };

            string resultUrl = new Uri(blob.Uri.AbsoluteUri + blob.GetSharedAccessSignature(readPolicy)).ToString();

            return(resultUrl);
        }
예제 #48
0
        //zipArchive - blob with scene zip file
        private void extractArchive(string zipArchive)
        {
            Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(DocConstants.Get().ConnectionString);
            CloudBlobClient    blobClient     = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer driveContainer = blobClient.GetContainerReference(BlobName.SCENE_BLOB);//sceneBlob = "scenegallery"

            CloudBlockBlob blockBlob = driveContainer.GetBlockBlobReference(zipArchive);
            string         zipPath   = m_directory.FullName + blockBlob.Name;

            Utils.DownloadBlobToFile(blockBlob, zipPath); //download scene zip file to zipPath

            try
            {
                FastZip zip = new FastZip();
                zip.ExtractZip(zipPath, m_directory.FullName, null); //extract from zipPath to Directory
            }
            catch (Exception e)
            {
                string msg = "Extract archive error: " + e.Message;
                throw new Exception(msg);
            }
        }
예제 #49
0
        private DateTime GetLastModified()
        {
            Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("CineStorageConStr"));

            Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            // Retrieve a reference to a container.
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = blobClient.GetContainerReference("data");

            // Retrieve reference to a blob.
            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob cinemasUKBlob = container.GetBlockBlobReference(CinemasUKFileName);
            if (cinemasUKBlob.Exists())
            {
                DateTimeOffset?dto = cinemasUKBlob.Properties.LastModified;
                if (dto.HasValue)
                {
                    return(dto.Value.DateTime);
                }
            }

            return(DateTime.MinValue);
        }
예제 #50
0
        //Retrieve Blob Container
        public Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer GetCloudBlobContainer(string ContainerName, Boolean PublicAccess = false)
        {
            // Retrieve storage account from connection string.
            Microsoft.WindowsAzure.Storage.CloudStorageAccount StorageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(Convert.ToString(ConfigurationManager.AppSettings["StorageConnectionString"]));

            // Create the blob client.
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient blobClient = StorageAccount.CreateCloudBlobClient();

            // Retrieve a reference to a container.
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = blobClient.GetContainerReference(ContainerName);

            // Create the container if it doesn't exist.
            container.CreateIfNotExists();


            //Set permission to public
            if (PublicAccess)
            {
                container.SetPermissions(
                    new Microsoft.WindowsAzure.Storage.Blob.BlobContainerPermissions
                {
                    PublicAccess =
                        Microsoft.WindowsAzure.Storage.Blob.BlobContainerPublicAccessType.Blob
                });
            }
            else
            {
                container.SetPermissions(
                    new Microsoft.WindowsAzure.Storage.Blob.BlobContainerPermissions
                {
                    PublicAccess =
                        Microsoft.WindowsAzure.Storage.Blob.BlobContainerPublicAccessType.Off
                });
            }

            return(container);
        }
        /// <summary>
        /// Set service properties
        /// </summary>
        /// <param name="account">Cloud storage account</param>
        /// <param name="type">Service type</param>
        /// <param name="properties">Service properties</param>
        /// <param name="options">Request options</param>
        /// <param name="operationContext">Operation context</param>
        public void SetStorageServiceProperties(StorageServiceType type, XSCLProtocol.ServiceProperties properties, IRequestOptions options, XSCL.OperationContext operationContext)
        {
            XSCL.CloudStorageAccount account = StorageContext.StorageAccount;
            try
            {
                switch (type)
                {
                case StorageServiceType.Blob:
                    Task.Run(() => account.CreateCloudBlobClient().SetServicePropertiesAsync(properties, (BlobRequestOptions)options, operationContext)).Wait();
                    break;

                case StorageServiceType.Queue:
                    Task.Run(() => account.CreateCloudQueueClient().SetServicePropertiesAsync(properties, (QueueRequestOptions)options, operationContext)).Wait();
                    break;

                case StorageServiceType.File:
                    if (null != properties.Logging)
                    {
                        throw new InvalidOperationException(Resources.FileNotSupportLogging);
                    }

                    FileServiceProperties fileServiceProperties = new FileServiceProperties();
                    fileServiceProperties.Cors          = properties.Cors;
                    fileServiceProperties.HourMetrics   = properties.HourMetrics;
                    fileServiceProperties.MinuteMetrics = properties.MinuteMetrics;
                    Task.Run(() => account.CreateCloudFileClient().SetServicePropertiesAsync(fileServiceProperties, (FileRequestOptions)options, operationContext)).Wait();
                    break;

                default:
                    throw new ArgumentException(Resources.InvalidStorageServiceType, "type");
                }
            }
            catch (AggregateException e) when(e.InnerException is XSCL.StorageException)
            {
                throw e.InnerException;
            }
        }
        public string GetTempDownloadUrl(string accountName, string accountKey, string fullPath)
        {
            Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount =
                Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(
                    string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1};BlobEndpoint=https://{0}.blob.core.windows.net/", accountName, accountKey)
                    );
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            var blob = blobClient.GetBlobReferenceFromServer(new Uri(fullPath));

            var readPolicy = new Microsoft.WindowsAzure.Storage.Blob.SharedAccessBlobPolicy()
            {
                Permissions            = Microsoft.WindowsAzure.Storage.Blob.SharedAccessBlobPermissions.Read, // SharedAccessPermissions.Read,
                SharedAccessExpiryTime = DateTime.UtcNow + TimeSpan.FromMinutes(10)
            };

            string resultUrl = new Uri(blob.Uri.AbsoluteUri + blob.GetSharedAccessSignature(readPolicy,
                                                                                            new SharedAccessBlobHeaders {
                ContentDisposition = blob.Metadata.ContainsKey("originalfilename") ? "attachment; filename=" + blob.Metadata["originalfilename"] : "attachment; filename=FileUnknown",
                ContentType        = blob.Properties.ContentType
            }
                                                                                            )).ToString();

            return(resultUrl);
        }
예제 #53
0
        public CloudBlobDirectory GetCloudBlobDir(string path)
        {
            // Retrieve storage account from connection-string
            Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(
                CloudConfigurationManager.GetSetting("StorageConnectionString"));

            // Create the blob client

            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            CloudBlobContainer container = blobClient.GetContainerReference(ContainerName);

            CloudBlobDirectory blobDir = container.GetDirectoryReference(path);

            // Retrieve a reference to a CloudBlobDirectory

            //  CloudBlobDirectory blobDir = blobClient.GetBlobDirectoryReference(path);
            //  CloudBlobDirectory blobDir = bl;
            //modify



            return(blobDir);
        }
예제 #54
0
        public string DownloadSharedBlob(string UserId, string BlobName)
        {
            //Retrieve storage account from connection string.
            Microsoft.WindowsAzure.Storage.CloudStorageAccount StorageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(Convert.ToString(ConfigurationManager.AppSettings["StorageConnectionString"]));

            // Create the blob client.
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient blobClient = StorageAccount.CreateCloudBlobClient();

            // Retrieve a reference to a container.
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = blobClient.GetContainerReference(UserId);

            var blob     = container.GetBlockBlobReference(BlobName);
            var sasToken = blob.GetSharedAccessSignature(new SharedAccessBlobPolicy()
            {
                Permissions            = SharedAccessBlobPermissions.Read,
                SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(15),
            }, new SharedAccessBlobHeaders()
            {
                ContentDisposition = "attachment; filename=\"somefile.pdf\"",
            });
            var downloadUrl = string.Format("{0}{1}", blob.Uri.AbsoluteUri, sasToken);

            return(downloadUrl);
        }
예제 #55
0
        /// <summary>
        /// Basic operations to work with page blobs
        /// </summary>
        /// <returns>Task</returns>
        private static async Task BasicStoragePageBlobOperationsAsync()
        {
            const string PageBlobName = "samplepageblob";

            // Retrieve storage account information from connection string
            // How to create a storage connection string - http://msdn.microsoft.com/en-us/library/azure/ee758697.aspx
            Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = CreateStorageAccountFromConnectionString(Microsoft.Azure.CloudConfigurationManager.GetSetting("StorageConnectionString"));

            // Create a blob client for interacting with the blob service.
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            // Create a container for organizing blobs within the storage account.
            Console.WriteLine("1. Creating Container");
            CloudBlobContainer container = blobClient.GetContainerReference("democontainerpageblob");
            await container.CreateIfNotExistsAsync();

            // Create a page blob in the newly created container.
            Console.WriteLine("2. Creating Page Blob");
            CloudPageBlob pageBlob = container.GetPageBlobReference(PageBlobName);
            await pageBlob.CreateAsync(512 * 2 /*size*/); // size needs to be multiple of 512 bytes

            // Write to a page blob
            Console.WriteLine("2. Write to a Page Blob");
            byte[] samplePagedata = new byte[512];
            Random random         = new Random();

            random.NextBytes(samplePagedata);
            await pageBlob.UploadFromByteArrayAsync(samplePagedata, 0, samplePagedata.Length);

            // List all blobs in this container. Because a container can contain a large number of blobs the results
            // are returned in segments (pages) with a maximum of 5000 blobs per segment. You can define a smaller size
            // using the maxResults parameter on ListBlobsSegmentedAsync.
            Console.WriteLine("3. List Blobs in Container");
            BlobContinuationToken token = null;

            do
            {
                BlobResultSegment resultSegment = await container.ListBlobsSegmentedAsync(token);

                token = resultSegment.ContinuationToken;
                foreach (IListBlobItem blob in resultSegment.Results)
                {
                    // Blob type will be CloudBlockBlob, CloudPageBlob or CloudBlobDirectory
                    Console.WriteLine("{0} (type: {1}", blob.Uri, blob.GetType());
                }
            } while (token != null);

            // Read from a page blob
            //Console.WriteLine("4. Read from a Page Blob");
            int bytesRead = await pageBlob.DownloadRangeToByteArrayAsync(samplePagedata, 0, 0, samplePagedata.Count());

            // Clean up after the demo
            //Console.WriteLine("5. Delete page Blob"); // Remark by Bryan Wu 20150516
            //await pageBlob.DeleteAsync(); // Remark by Bryan Wu 20150516

            // When you delete a container it could take several seconds before you can recreate a container with the same
            // name - hence to enable you to run the demo in quick succession the container is not deleted. If you want
            // to delete the container uncomment the line of code below.
            //Console.WriteLine("6. Delete Container");
            //await container.DeleteAsync();
        }
예제 #56
0
        /// <summary>
        /// Basic operations to work with block blobs
        /// </summary>
        /// <returns>Task<returns>
        private static async Task BasicStorageBlockBlobOperationsAsync()
        {
            //const string ImageToUpload = "HelloWorld.png";
            const string ImageToUpload = @"C:\Users\IEC891652.IEC\Downloads\IMG_1912.MOV";
            FileInfo     newinfo       = new FileInfo(ImageToUpload);

            // Retrieve storage account information from connection string
            // How to create a storage connection string - http://msdn.microsoft.com/en-us/library/azure/ee758697.aspx
            Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = CreateStorageAccountFromConnectionString(Microsoft.Azure.CloudConfigurationManager.GetSetting("StorageProjectFiles"));

            // Create a blob client for interacting with the blob service.
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            // Create a container for organizing blobs within the storage account.
            Console.WriteLine("1. Creating Container");
            CloudBlobContainer container = blobClient.GetContainerReference("barcelona");

            try
            {
                await container.CreateIfNotExistsAsync();
            }
            catch (StorageException)
            {
                Console.WriteLine("If you are running with the default configuration please make sure you have started the storage emulator. Press the Windows key and type Azure Storage to select and run it from the list of applications - then restart the sample.");
                Console.ReadLine();
                throw;
            }

            // To view the uploaded blob in a browser, you have two options. The first option is to use a Shared Access Signature (SAS) token to delegate
            // access to the resource. See the documentation links at the top for more information on SAS. The second approach is to set permissions
            // to allow public access to blobs in this container. Uncomment the line below to use this approach. Then you can view the image
            // using: https://[InsertYourStorageAccountNameHere].blob.core.windows.net/democontainer/HelloWorld.png
            // await container.SetPermissionsAsync(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });

            // Upload a BlockBlob to the newly created container
            Console.WriteLine("2. Uploading BlockBlob");
            CloudBlockBlob blockBlob = container.GetBlockBlobReference(newinfo.Name);
            await blockBlob.UploadFromFileAsync(newinfo.FullName, FileMode.Open);

            // List all the blobs in the container
            Console.WriteLine("3. List Blobs in Container");
            foreach (IListBlobItem blob in container.ListBlobs())
            {
                // Blob type will be CloudBlockBlob, CloudPageBlob or CloudBlobDirectory
                // Use blob.GetType() and cast to appropriate type to gain access to properties specific to each type
                Console.WriteLine("- {0} (type: {1})", blob.Uri, blob.GetType());
            }

            // Download a blob to your file system
            Console.WriteLine("4. Download Blob from {0}", blockBlob.Uri.AbsoluteUri);
            await blockBlob.DownloadToFileAsync(string.Format("./CopyOf{0}", newinfo.Name), FileMode.Create);

            // Clean up after the demo
            //Console.WriteLine("5. Delete block Blob");    // Remark by Bryan Wu 20150516
            //await blockBlob.DeleteAsync();                // Remark by Bryan Wu 20150516

            // When you delete a container it could take several seconds before you can recreate a container with the same
            // name - hence to enable you to run the demo in quick succession the container is not deleted. If you want
            // to delete the container uncomment the line of code below.
            //Console.WriteLine("6. Delete Container");
            //await container.DeleteAsync();
        }
예제 #57
0
        public async Task ProcessCinemaPerformances(RegionDef region, int cinemaID, List <FilmInfo> films)
        {
            CineworldService  cws            = new CineworldService();
            List <FilmHeader> filmsForCinema = new List <FilmHeader>();

            foreach (var film in films)
            {
                Task <Dates> td = cws.GetDates(region, cinemaID, film.EDI);
                td.Wait();

                HashSet <DateTime> perfDates = new HashSet <DateTime>();
                if (!td.IsFaulted && td.Result != null && td.Result.dates != null)
                {
                    FilmHeader fh = new FilmHeader()
                    {
                        EDI = film.EDI
                    };
                    filmsForCinema.Add(fh);

                    fh.Performances = new List <PerformanceInfo>();

                    foreach (var date in td.Result.dates)
                    {
                        DateTime performanceDate = DateTime.ParseExact(date, "yyyyMMdd", CultureInfo.InvariantCulture);

                        Task <Performances> tp = cws.GetPerformances(region, cinemaID, film.EDI, date);
                        tp.Wait();

                        if (!tp.IsFaulted && tp.Result != null && tp.Result.performances != null)
                        {
                            foreach (var p in tp.Result.performances)
                            {
                                PerformanceInfo pi = new PerformanceInfo();
                                pi.PerformanceTS = performanceDate.Add(DateTime.ParseExact(p.time, "HH:mm", CultureInfo.InvariantCulture).TimeOfDay);
                                pi.Available     = p.available;
                                pi.Type          = p.Type;
                                pi.BookUrl       = new Uri(p.booking_url);
                                fh.Performances.Add(pi);
                            }
                        }
                    }
                }
            }

            //return;

            Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("CineStorageConStr"));

            Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            // Retrieve a reference to a container.
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = blobClient.GetContainerReference("data");

            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob cinemaFilmsBlob = container.GetBlockBlobReference(String.Format(FilmsPerCinemaFileName, cinemaID));
            using (Stream s = cinemaFilmsBlob.OpenWrite())
            {
                using (var gzipStream = new GZipStream(s, CompressionMode.Compress))
                {
                    using (StreamWriter sw = new StreamWriter(gzipStream))
                    {
                        await sw.WriteAsync(JsonConvert.SerializeObject(filmsForCinema));
                    }
                }
            }
        }
예제 #58
0
        private void ProcessData()
        {
            //return;

            Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("CineStorageConStr"));

            Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            // Retrieve a reference to a container.
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = blobClient.GetContainerReference("data");

            // Retrieve reference to a blob.
            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob cinemasUKBlob = container.GetBlockBlobReference(CinemasUKFileName);
            using (Stream s = cinemasUKBlob.OpenWrite())
            {
                using (var gzipStream = new GZipStream(s, CompressionMode.Compress))
                {
                    using (StreamWriter sw = new StreamWriter(gzipStream))
                    {
                        sw.Write(JsonConvert.SerializeObject(CinemasUK));
                    }
                }
            }

            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob filmsUKBlob = container.GetBlockBlobReference(FilmsUKFileName);
            using (Stream s = filmsUKBlob.OpenWrite())
            {
                using (var gzipStream = new GZipStream(s, CompressionMode.Compress))
                {
                    using (StreamWriter sw = new StreamWriter(gzipStream))
                    {
                        sw.Write(JsonConvert.SerializeObject(FilmsUK));
                    }
                }
            }

            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob filmCinemasUKBlob = container.GetBlockBlobReference(FilmCinemasUKFileName);
            using (Stream s = filmCinemasUKBlob.OpenWrite())
            {
                using (var gzipStream = new GZipStream(s, CompressionMode.Compress))
                {
                    using (StreamWriter sw = new StreamWriter(gzipStream))
                    {
                        sw.Write(JsonConvert.SerializeObject(filmCinemasUK));
                    }
                }
            }

            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob cinemaFilmsUKBlob = container.GetBlockBlobReference(CinemaFilmsUKFileName);
            using (Stream s = cinemaFilmsUKBlob.OpenWrite())
            {
                using (var gzipStream = new GZipStream(s, CompressionMode.Compress))
                {
                    using (StreamWriter sw = new StreamWriter(gzipStream))
                    {
                        sw.Write(JsonConvert.SerializeObject(cinemaFilmsUK));
                    }
                }
            }

            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob cinemasIEBlob = container.GetBlockBlobReference(CinemasIEFileName);
            using (Stream s = cinemasIEBlob.OpenWrite())
            {
                using (var gzipStream = new GZipStream(s, CompressionMode.Compress))
                {
                    using (StreamWriter sw = new StreamWriter(gzipStream))
                    {
                        sw.Write(JsonConvert.SerializeObject(CinemasIE));
                    }
                }
            }

            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob filmsIEBlob = container.GetBlockBlobReference(FilmsIEFileName);
            using (Stream s = filmsIEBlob.OpenWrite())
            {
                using (var gzipStream = new GZipStream(s, CompressionMode.Compress))
                {
                    using (StreamWriter sw = new StreamWriter(gzipStream))
                    {
                        sw.Write(JsonConvert.SerializeObject(FilmsIE));
                    }
                }
            }

            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob filmCinemasIEBlob = container.GetBlockBlobReference(FilmCinemasIEFileName);
            using (Stream s = filmCinemasIEBlob.OpenWrite())
            {
                using (var gzipStream = new GZipStream(s, CompressionMode.Compress))
                {
                    using (StreamWriter sw = new StreamWriter(gzipStream))
                    {
                        sw.Write(JsonConvert.SerializeObject(filmCinemasIE));
                    }
                }
            }

            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob cinemaFilmsIEBlob = container.GetBlockBlobReference(CinemaFilmsIEFileName);
            using (Stream s = cinemaFilmsIEBlob.OpenWrite())
            {
                using (var gzipStream = new GZipStream(s, CompressionMode.Compress))
                {
                    using (StreamWriter sw = new StreamWriter(gzipStream))
                    {
                        sw.Write(JsonConvert.SerializeObject(cinemaFilmsIE));
                    }
                }
            }

            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob filmsPostersBlob = container.GetBlockBlobReference(MovieFilmPostersFileName);
            using (Stream s = filmsPostersBlob.OpenWrite())
            {
                using (var gzipStream = new GZipStream(s, CompressionMode.Compress))
                {
                    using (StreamWriter sw = new StreamWriter(gzipStream))
                    {
                        sw.Write(JsonConvert.SerializeObject(MoviePosters));
                    }
                }
            }

            //Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob mediumfilmsPostersBlob = container.GetBlockBlobReference(MediumFilmPostersFileName);
            //using (Stream s = mediumfilmsPostersBlob.OpenWrite())
            //{
            //    using (var gzipStream = new GZipStream(s, CompressionMode.Compress))
            //    {
            //        using (StreamWriter sw = new StreamWriter(gzipStream))
            //        {
            //            sw.Write(JsonConvert.SerializeObject(mediumposters));
            //        }
            //    }
            //}
        }