static void CreateShareFile()
        {
            try
            {
                #region Creating the Shared Files in Azure

                _storageAccount = CloudStorageAccount.Parse(
                    CloudConfigurationManager.GetSetting("StorageConnection"));

                _share = _storageAccount.CreateCloudFileClient().GetShareReference("documentos");

                if (_share.Exists())
                {
                    //Console.Clear();
                    // Check current usage stats for the share.
                    // Note that the ShareStats object is part of the protocol layer for the File service.
                    ShareStats stats = _share.GetStats();
                    //Console.WriteLine("Current share usage: {0} GB", stats.Usage.ToString());

                    // Specify the maximum size of the share, in GB.
                    // This line sets the quota to be 10 GB greater than the current usage of the share.
                    _share.Properties.Quota = 10 + stats.Usage;
                    _share.SetProperties();

                    // Now check the quota for the share. Call FetchAttributes() to populate the share's properties.
                    _share.FetchAttributes();
                    //Console.WriteLine("Current share quota: {0} GB", _share.Properties.Quota);

                    // Create a new shared access policy and define its constraints.
                    SharedAccessFilePolicy sharedPolicy = new SharedAccessFilePolicy()
                    {
                        SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24),
                        Permissions            = SharedAccessFilePermissions.Read | SharedAccessFilePermissions.Write
                    };

                    // Get existing permissions for the share.
                    FileSharePermissions permissions = _share.GetPermissions();

                    if (!permissions.SharedAccessPolicies.ContainsKey("sampleSharePolicy"))
                    {
                        // Add the shared access policy to the share's policies. Note that each policy must have a unique name.
                        permissions.SharedAccessPolicies.Add("sampleSharePolicy", sharedPolicy);
                        _share.SetPermissions(permissions);
                    }

                    //Console.ReadKey();
                }
                else
                {
                    _share.CreateIfNotExists();
                }

                #endregion
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                Console.ReadKey();
            }
        }
Exemple #2
0
        public void Set_the_maximum_size_for_a_file_share()
        {
            // Parse the connection string for the storage account.
            StorageCredentials  Credentials    = new StorageCredentials(this.Account, this.Key);
            CloudStorageAccount storageAccount = new CloudStorageAccount(Credentials, false);

            // Create a CloudFileClient object for credentialed access to File storage.
            CloudFileClient fileClient = storageAccount.CreateCloudFileClient();

            // Get a reference to the file share we created previously.
            CloudFileShare share = fileClient.GetShareReference("logs");

            // Ensure that the share exists.
            if (share.Exists())
            {
                // Check current usage stats for the share.
                // Note that the ShareStats object is part of the protocol layer for the File service.
                Microsoft.WindowsAzure.Storage.File.Protocol.ShareStats stats = share.GetStats();
                Console.WriteLine("Current share usage: {0} GB", stats.Usage.ToString());

                // Specify the maximum size of the share, in GB.
                // This line sets the quota to be 10 GB greater than the current usage of the share.
                share.Properties.Quota = 10 + stats.Usage;
                share.SetProperties();

                // Now check the quota for the share. Call FetchAttributes() to populate the share's properties.
                share.FetchAttributes();
                Console.WriteLine("Current share quota: {0} GB", share.Properties.Quota);
            }
        }
Exemple #3
0
        private void ValidateShareQuota(CloudFileShare share, int quota)
        {
            share.FetchAttributes();

            Test.Assert(share.Properties.Quota == quota, "Share quota should be the same with expected. {0} == {1}", share.Properties.Quota, quota);

            const long TBSize = 1024 * 1024 * 1024 * 1024L;
            const long GBSize = 1024 * 1024 * 1024;

            int    sizeInTB       = quota / 1024;
            int    fileNameIndex  = 0;
            string fileNamePrefix = Utility.GenNameString("file");

            while (sizeInTB > 0)
            {
                string fileName = string.Format("{0}_{1}", fileNamePrefix, fileNameIndex);
                fileNameIndex++;

                var file = share.GetRootDirectoryReference().GetFileReference(fileName);
                file.Create(TBSize);
                sizeInTB--;
            }

            int sizeInGB = quota % 1024;

            while (sizeInGB > 0)
            {
                string fileName = string.Format("{0}_{1}", fileNamePrefix, fileNameIndex);
                fileNameIndex++;

                var file = share.GetRootDirectoryReference().GetFileReference(fileName);
                file.Create(GBSize);
                sizeInGB--;
            }

            try
            {
                string fileName = string.Format("{0}_{1}", fileNamePrefix, fileNameIndex);
                var    file     = share.GetRootDirectoryReference().GetFileReference(fileName);
                file.Create(1024);
            }
            catch (StorageException ex)
            {
                Test.Info("Got an exception when try to create files larger than quota as expected. {0}", ex.Message);
                return;
            }

            Test.Warn(string.Format("Actual quota is larger than expected. The share usage is {0}", share.GetStats().Usage));
        }