private static void SetAclAndGetFileProperties(DataLakeFileSystemClient client) { DataLakeFileClient fileClient = client.GetFileClient("sample.txt"); fileClient.Create(); // Set Access Control List IList <PathAccessControlItem> accessControlList = PathAccessControlExtensions.ParseAccessControlList("user::rwx,group::r--,mask::rwx,other::---"); fileClient.SetAccessControlList(accessControlList); PathAccessControl accessControlResponse = fileClient.GetAccessControl(); Console.WriteLine($"User: {accessControlResponse.Owner}"); Console.WriteLine($"Group: {accessControlResponse.Group}"); Console.WriteLine($"Permissions: {accessControlResponse.Permissions}"); }
public void SetGetAcls() { // Make StorageSharedKeyCredential to pass to the serviceClient string storageAccountName = NamespaceStorageAccountName; string storageAccountKey = NamespaceStorageAccountKey; Uri serviceUri = NamespaceBlobUri; StorageSharedKeyCredential sharedKeyCredential = new StorageSharedKeyCredential(storageAccountName, storageAccountKey); // Create DataLakeServiceClient using StorageSharedKeyCredentials DataLakeServiceClient serviceClient = new DataLakeServiceClient(serviceUri, sharedKeyCredential); // Get a reference to a filesystem named "sample-filesystem-acl" and then create it DataLakeFileSystemClient filesystem = serviceClient.GetFileSystemClient(Randomize("sample-filesystem-acl")); filesystem.Create(); try { #region Snippet:SampleSnippetDataLakeFileClient_SetAcls // Create a DataLake file so we can set the Access Controls on the files DataLakeFileClient fileClient = filesystem.GetFileClient(Randomize("sample-file")); fileClient.Create(); // Set Access Control List IList <PathAccessControlItem> accessControlList = PathAccessControlExtensions.ParseAccessControlList("user::rwx,group::r--,mask::rwx,other::---"); fileClient.SetAccessControlList(accessControlList); #endregion Snippet:SampleSnippetDataLakeFileClient_SetAcls #region Snippet:SampleSnippetDataLakeFileClient_GetAcls // Get Access Control List PathAccessControl accessControlResponse = fileClient.GetAccessControl(); #endregion Snippet:SampleSnippetDataLakeFileClient_GetAcls // Check Access Control permissions Assert.AreEqual( PathAccessControlExtensions.ToAccessControlListString(accessControlList), PathAccessControlExtensions.ToAccessControlListString(accessControlResponse.AccessControlList.ToList())); } finally { // Clean up after the test when we're finished filesystem.Delete(); } }
// </Snippet_ACLDirectory> #endregion #region Get and set file ACLs // --------------------------------------------------------- // Get and set ACLs on files //---------------------------------------------------------- // <Snippet_FileACL> public async Task ManageFileACLs(DataLakeFileSystemClient fileSystemClient) { DataLakeDirectoryClient directoryClient = fileSystemClient.GetDirectoryClient("my-directory"); DataLakeFileClient fileClient = directoryClient.GetFileClient("hello.txt"); PathAccessControl FileAccessControl = await fileClient.GetAccessControlAsync(); foreach (var item in FileAccessControl.AccessControlList) { Console.WriteLine(item.ToString()); } IList <PathAccessControlItem> accessControlList = PathAccessControlExtensions.ParseAccessControlList ("user::rwx,group::r-x,other::rw-"); fileClient.SetAccessControlList(accessControlList); }
/// <summary> /// execute command /// </summary> public override void ExecuteCmdlet() { IStorageBlobManagement localChannel = Channel; bool foundAFolder = false; DataLakeFileClient fileClient = null; DataLakeDirectoryClient dirClient = null; if (ParameterSetName == ManualParameterSet) { DataLakeFileSystemClient fileSystem = GetFileSystemClientByName(localChannel, this.FileSystem); foundAFolder = GetExistDataLakeGen2Item(fileSystem, this.Path, out fileClient, out dirClient); } else //BlobParameterSet { if (!InputObject.IsDirectory) { fileClient = InputObject.File; } else { dirClient = InputObject.Directory; foundAFolder = true; } } if (foundAFolder) { if (ShouldProcess(GetDataLakeItemUriWithoutSas(dirClient), "Update Directory: ")) { //Set Permission if (this.Permission != null || this.Owner != null || this.Group != null) { //PathAccessControl originPathAccessControl = dirClient.GetAccessControl().Value; dirClient.SetPermissions( this.Permission != null ? PathPermissions.ParseSymbolicPermissions(this.Permission) : null, this.Owner, this.Group); } //Set ACL if (this.Acl != null) { dirClient.SetAccessControlList(PSPathAccessControlEntry.ParseAccessControls(this.Acl)); } // Set Properties SetDatalakegen2ItemProperties(dirClient, this.BlobProperties, setToServer: true); //Set MetaData SetDatalakegen2ItemMetaData(dirClient, this.BlobMetadata, setToServer: true); WriteDataLakeGen2Item(localChannel, dirClient); } } else { if (ShouldProcess(GetDataLakeItemUriWithoutSas(fileClient), "Update File: ")) { //Set Permission if (this.Permission != null || this.Owner != null || this.Group != null) { fileClient.SetPermissions( this.Permission != null ? PathPermissions.ParseSymbolicPermissions(this.Permission) : null, this.Owner, this.Group); } //Set ACL if (this.Acl != null) { fileClient.SetAccessControlList(PSPathAccessControlEntry.ParseAccessControls(this.Acl)); } // Set Properties SetDatalakegen2ItemProperties(fileClient, this.BlobProperties, setToServer: true); //Set MetaData SetDatalakegen2ItemMetaData(fileClient, this.BlobMetadata, setToServer: true); WriteDataLakeGen2Item(localChannel, fileClient); } } }