public override void ExecuteCmdlet()
        {
            if (String.IsNullOrEmpty(ShareName) || String.IsNullOrEmpty(Policy))
            {
                return;
            }

            NamingUtil.ValidateShareName(this.ShareName, false);

            if (!NameUtil.IsValidStoredAccessPolicyName(this.Policy))
            {
                throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Resources.InvalidAccessPolicyName, this.Policy));
            }

            //Get existing permissions
            CloudFileShare fileShare = this.Channel.GetShareReference(this.ShareName);

            FileSharePermissions fileSharePermissions = fileShare.GetPermissions();

            //Add new policy
            if (fileSharePermissions.SharedAccessPolicies.Keys.Contains(this.Policy))
            {
                throw new ResourceAlreadyExistException(String.Format(CultureInfo.CurrentCulture, Resources.PolicyAlreadyExists, this.Policy));
            }

            SharedAccessFilePolicy policy = new SharedAccessFilePolicy();

            AccessPolicyHelper.SetupAccessPolicy <SharedAccessFilePolicy>(policy, this.StartTime, this.ExpiryTime, this.Permission);
            fileSharePermissions.SharedAccessPolicies.Add(this.Policy, policy);

            //Set permissions back to container
            fileShare.SetPermissions(fileSharePermissions);
            WriteObject(Policy);
        }
        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();
            }
        }
        /// <summary>
        /// Validate the file share access policy
        /// </summary>
        /// <param name="policy">SharedAccessFilePolicy object</param>
        /// <param name="policyIdentifier">The policy identifier which need to be checked.</param>
        public static bool ValidateShareAccessPolicy(IStorageFileManagement channel, string shareName,
                                                     string policyIdentifier, bool shouldNoPermission, bool shouldNoStartTime, bool shouldNoExpiryTime)
        {
            if (string.IsNullOrEmpty(policyIdentifier))
            {
                return(true);
            }
            CloudFileShare       fileShare  = channel.GetShareReference(shareName);
            FileSharePermissions permission = fileShare.GetPermissions();

            SharedAccessFilePolicy sharedAccessPolicy =
                GetExistingPolicy <SharedAccessFilePolicy>(permission.SharedAccessPolicies, policyIdentifier);

            if (shouldNoPermission && sharedAccessPolicy.Permissions != SharedAccessFilePermissions.None)
            {
                throw new InvalidOperationException(Resources.SignedPermissionsMustBeOmitted);
            }

            if (shouldNoStartTime && sharedAccessPolicy.SharedAccessStartTime.HasValue)
            {
                throw new InvalidOperationException(Resources.SignedStartTimeMustBeOmitted);
            }

            if (shouldNoExpiryTime && sharedAccessPolicy.SharedAccessExpiryTime.HasValue)
            {
                throw new InvalidOperationException(Resources.SignedExpiryTimeMustBeOmitted);
            }

            return(!sharedAccessPolicy.SharedAccessExpiryTime.HasValue);
        }
Exemple #4
0
        public void Generate_a_shared_access_signature_for_a_file_or_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())
            {
                string policyName = "sampleSharePolicy" + DateTime.UtcNow.Ticks;

                // 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();

                // Add the shared access policy to the share's policies. Note that each policy must have a unique name.
                permissions.SharedAccessPolicies.Add(policyName, sharedPolicy);
                share.SetPermissions(permissions);

                // Generate a SAS for a file in the share and associate this access policy with it.
                CloudFileDirectory rootDir   = share.GetRootDirectoryReference();
                CloudFileDirectory sampleDir = rootDir.GetDirectoryReference("CustomLogs");
                CloudFile          file      = sampleDir.GetFileReference("Log1.txt");
                string             sasToken  = file.GetSharedAccessSignature(null, policyName);
                Uri fileSasUri = new Uri(file.StorageUri.PrimaryUri.ToString() + sasToken);

                // Create a new CloudFile object from the SAS, and write some text to the file.
                CloudFile fileSas = new CloudFile(fileSasUri);
                fileSas.UploadText("This write operation is authenticated via SAS.");
                Console.WriteLine(fileSas.DownloadText());
            }
        }
Exemple #5
0
        public static void SetSharePermissions()
        {
            CloudFileShare share = GetShare(SHARE);

            if (share.Exists())
            {
                string policyName = "SharePolicy";

                SharedAccessFilePolicy sharedPolicy = new SharedAccessFilePolicy()
                {
                    SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24),
                    Permissions            = SharedAccessFilePermissions.Read | SharedAccessFilePermissions.Write
                };

                FileSharePermissions permissions = share.GetPermissions();
                permissions.SharedAccessPolicies.Add(policyName, sharedPolicy);
                share.SetPermissions(permissions);
            }
        }
        public static void NewFileCreate()
        {
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connection);

            CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
            CloudFileShare  share      = fileClient.GetShareReference("doc2020");

            // Ensure that the share exists.
            if (share.Exists())
            {
                string policyName = "FileSharePolicy" + DateTime.UtcNow.Ticks;

                SharedAccessFilePolicy sharedPolicy = new SharedAccessFilePolicy()
                {
                    SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24),
                    Permissions            = SharedAccessFilePermissions.Read | SharedAccessFilePermissions.Write
                };

                FileSharePermissions permissions = share.GetPermissions();

                permissions.SharedAccessPolicies.Add(policyName, sharedPolicy);
                share.SetPermissions(permissions);

                CloudFileDirectory rootDir   = share.GetRootDirectoryReference();
                CloudFileDirectory sampleDir = rootDir.GetDirectoryReference("storage");

                CloudFile file       = sampleDir.GetFileReference("Log2.txt");
                string    sasToken   = file.GetSharedAccessSignature(null, policyName);
                Uri       fileSasUri = new Uri(file.StorageUri.PrimaryUri.ToString() + sasToken);

                // Create a new CloudFile object from the SAS, and write some text to the file.
                CloudFile fileSas = new CloudFile(fileSasUri);
                fileSas.UploadText("This file created by the Console App at Runtime");
                Console.WriteLine(fileSas.DownloadText());
            }
        }
Exemple #7
0
 public FileSharePermissions GetSharePermissions(CloudFileShare share, AccessCondition accessCondition = null,
                                                 FileRequestOptions options = null, OperationContext operationContext = null)
 {
     return(share.GetPermissions(accessCondition, options, operationContext));
 }