Skip to content

Commit abf2eb3

Browse files
yifanz7wyunchi-ms
andauthored
[Storage] Support rename file and directory (#21575)
* rename changes * Update help * update cmdlet config --------- Co-authored-by: Yunchi Wang <[email protected]>
1 parent 1a13331 commit abf2eb3

File tree

7 files changed

+926
-5
lines changed

7 files changed

+926
-5
lines changed

src/Storage/Storage.Management.Test/ScenarioTests/StorageDataPlaneTests.ps1

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,25 @@ function Test-File
110110
Assert-AreEqual $localFileProperties.Attributes.ToString() $file[0].FileProperties.SmbProperties.FileAttributes.ToString()
111111
}
112112

113-
Remove-AzStorageFile -ShareName $shareName -Path $objectName1 -Context $storageContext
113+
$fileName1 = "new" + $objectName1
114+
$file = Get-AzStorageFile -ShareName $shareName -Path $objectName1 -Context $storageContext
115+
116+
$file2 = Rename-AzStorageFile -ShareName $shareName -SourcePath $objectName1 -DestinationPath $fileName1 -Context $storageContext
117+
Assert-AreEqual $file2.Name $fileName1
118+
Assert-AreEqual $file.FileProperties.ContentType $file2.FileProperties.ContentType
119+
Assert-AreEqual $file.FileProperties.ContentLength $file2.FileProperties.ContentLength
120+
121+
$file3 = $file2 | Rename-AzStorageFile -DestinationPath $fileName1 -Context $storageContext -Force
122+
Assert-AreEqual $file3.Name $fileName1
123+
Assert-AreEqual $file2.FileProperties.ContentType $file3.FileProperties.ContentType
124+
Assert-AreEqual $file2.FileProperties.ContentLength $file3.FileProperties.ContentLength
125+
126+
Remove-AzStorageFile -ShareName $shareName -Path $fileName1 -Context $storageContext
114127
$file = Get-AzStorageFile -ShareName $shareName -Context $storageContext
115128
Assert-AreEqual $file.Count 1
116129
Assert-AreEqual $file[0].Name $objectName2
117130

118-
$dirName = "filetestdir"
131+
$dirName = "filetestdir"
119132
New-AzStorageDirectory -ShareName $shareName -Path $dirName -Context $storageContext
120133
$file = Get-AzStorageShare -Name $shareName -Context $storageContext | Get-AzStorageFile -ExcludeExtendedInfo
121134
Assert-AreEqual $file.Count 2
@@ -125,11 +138,25 @@ function Test-File
125138
Assert-AreEqual $file[1].Name $objectName2
126139
Assert-AreEqual $file[1].GetType().Name "AzureStorageFile"
127140
Assert-Null $file[1].ListFileProperties.Properties.ETag
128-
Get-AzStorageFile -ShareName $shareName -Path $dirName -Context $storageContext | Remove-AzStorageDirectory
141+
142+
$newDir = "new" + $dirName
143+
$dir = Get-AzStorageFile -ShareName $shareName -Path $dirName -Context $storageContext
144+
$dir2 = Rename-AzStorageDirectory -ShareName $shareName -SourcePath $dirName -DestinationPath $newDir -Context $storageContext
145+
Assert-AreEqual $newDir $dir2.Name
146+
Assert-AreEqual $dir.ListFileProperties.IsDirectory $dir2.ListFileProperties.IsDirectory
147+
Assert-AreEqual $dir.ListFileProperties.FileAttributes $dir2.ListFileProperties.FileAttributes
148+
149+
$newDir2 = "new2" + $dirName
150+
$dir3 = $dir2 | Rename-AzStorageDirectory -DestinationPath $newDir2 -Context $storageContext
151+
Assert-AreEqual $newDir2 $dir3.Name
152+
Assert-AreEqual $dir2.ListFileProperties.IsDirectory $dir3.ListFileProperties.IsDirectory
153+
Assert-AreEqual $dir2.ListFileProperties.FileAttributes $dir3.ListFileProperties.FileAttributes
154+
155+
$dir3 | Remove-AzStorageDirectory
129156
$file = Get-AzStorageFile -ShareName $shareName -Context $storageContext
130157
Assert-AreEqual $file.Count 1
131158
Assert-AreEqual $file[0].Name $objectName2
132-
Assert-AreEqual $file[0].GetType().Name "AzureStorageFile"
159+
Assert-AreEqual $file[0].GetType().Name "AzureStorageFile"
133160

134161
# Clean Storage Account
135162
Remove-AzStorageShare -Name $shareName -Force -Context $storageContext

src/Storage/Storage.Management/Az.Storage.psd1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,8 @@ CmdletsToExport = 'Get-AzStorageAccount', 'Get-AzStorageAccountKey',
206206
'New-AzStorageLocalUserSshPublicKey',
207207
'New-AzStorageLocalUserPermissionScope',
208208
'Get-AzDataLakeGen2DeletedItem',
209-
'Restore-AzDataLakeGen2DeletedItem'
209+
'Restore-AzDataLakeGen2DeletedItem',
210+
'Rename-AzStorageFile', 'Rename-AzStorageDirectory'
210211

211212
# Variables to export from this module
212213
# VariablesToExport = @()

src/Storage/Storage.Management/ChangeLog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21+
* Supported rename file and directory
22+
- `Rename-AzStorageFile`
23+
- `Rename-AzStorageDirectory`
2124
* Added a warning message for an upcoming breaking change when getting a single blob
2225
- `Get-AzStorageBlob`
2326
* Fixed the issue of listing blobs with leading slashes
Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
---
2+
external help file: Microsoft.Azure.PowerShell.Cmdlets.Storage.dll-Help.xml
3+
Module Name: Az.Storage
4+
online version: https://learn.microsoft.com/powershell/module/az.storage/rename-azstoragedirectory
5+
schema: 2.0.0
6+
---
7+
8+
# Rename-AzStorageDirectory
9+
10+
## SYNOPSIS
11+
Renames a directory.
12+
13+
## SYNTAX
14+
15+
### ShareName
16+
```
17+
Rename-AzStorageDirectory [-ShareName] <String> [-SourcePath] <String> [[-DestinationPath] <String>]
18+
[-Permission <String>] [-Force] [-AsJob] [-Context <IStorageContext>]
19+
[-DefaultProfile <IAzureContextContainer>] [-IgnoreReadonly] [<CommonParameters>]
20+
```
21+
22+
### ShareObject
23+
```
24+
Rename-AzStorageDirectory [-ShareClient] <ShareClient> [-SourcePath] <String> [[-DestinationPath] <String>]
25+
[-Permission <String>] [-Force] [-AsJob] [-Context <IStorageContext>]
26+
[-DefaultProfile <IAzureContextContainer>] [-IgnoreReadonly] [<CommonParameters>]
27+
```
28+
29+
### DirecotryObject
30+
```
31+
Rename-AzStorageDirectory [-ShareDirectoryClient] <ShareDirectoryClient> [[-DestinationPath] <String>]
32+
[-Permission <String>] [-Force] [-AsJob] [-Context <IStorageContext>]
33+
[-DefaultProfile <IAzureContextContainer>] [-IgnoreReadonly] [<CommonParameters>]
34+
```
35+
36+
## DESCRIPTION
37+
The **Rename-AzStorageDirectory** cmdlet renames a directory from a file share.
38+
39+
## EXAMPLES
40+
41+
### Example 1 : Rename a directory
42+
```powershell
43+
Rename-AzStorageDirectory -ShareName myshare -SourcePath testdir1 -DestinationPath testdir2
44+
```
45+
46+
```output
47+
Directory: https://myaccount.file.core.windows.net/myshare
48+
49+
Type Length Name
50+
---- ------ ----
51+
Directory 1 testdir2
52+
```
53+
54+
This command renames a directory from testdir1 to testdir2.
55+
56+
### Example 2 : Rename a directory using pipeline
57+
```powershell
58+
Get-AzStorageFile -ShareName myshare -Path testdir1 | Rename-AzStorageDirectory -DestinationPath testdir2
59+
```
60+
61+
```output
62+
Directory: https://myaccount.file.core.windows.net/myshare
63+
64+
Type Length Name
65+
---- ------ ----
66+
Directory 1 testdir2
67+
```
68+
69+
This command gets a directory from a file share first, and then rename the directory from testdir1 to testdir2 using pipeline.
70+
71+
## PARAMETERS
72+
73+
### -AsJob
74+
Run cmdlet in the background
75+
76+
```yaml
77+
Type: System.Management.Automation.SwitchParameter
78+
Parameter Sets: (All)
79+
Aliases:
80+
81+
Required: False
82+
Position: Named
83+
Default value: None
84+
Accept pipeline input: False
85+
Accept wildcard characters: False
86+
```
87+
88+
### -Context
89+
Azure Storage Context Object
90+
91+
```yaml
92+
Type: Microsoft.Azure.Commands.Common.Authentication.Abstractions.IStorageContext
93+
Parameter Sets: (All)
94+
Aliases:
95+
96+
Required: False
97+
Position: Named
98+
Default value: None
99+
Accept pipeline input: True (ByPropertyName, ByValue)
100+
Accept wildcard characters: False
101+
```
102+
103+
### -DefaultProfile
104+
The credentials, account, tenant, and subscription used for communication with Azure.
105+
106+
```yaml
107+
Type: Microsoft.Azure.Commands.Common.Authentication.Abstractions.Core.IAzureContextContainer
108+
Parameter Sets: (All)
109+
Aliases: AzureRmContext, AzureCredential
110+
111+
Required: False
112+
Position: Named
113+
Default value: None
114+
Accept pipeline input: False
115+
Accept wildcard characters: False
116+
```
117+
118+
### -DestinationPath
119+
The destination path to rename the directory to.
120+
121+
```yaml
122+
Type: System.String
123+
Parameter Sets: (All)
124+
Aliases:
125+
126+
Required: False
127+
Position: 2
128+
Default value: None
129+
Accept pipeline input: False
130+
Accept wildcard characters: False
131+
```
132+
133+
### -Force
134+
Force to overwrite the existing file.
135+
136+
```yaml
137+
Type: System.Management.Automation.SwitchParameter
138+
Parameter Sets: (All)
139+
Aliases:
140+
141+
Required: False
142+
Position: Named
143+
Default value: None
144+
Accept pipeline input: False
145+
Accept wildcard characters: False
146+
```
147+
148+
### -IgnoreReadonly
149+
Optional.
150+
Specifies whether the ReadOnly attribute on a preexisting destination file should be respected.
151+
If true, the rename will succeed, otherwise, a previous file at the destination with the ReadOnly attribute set will cause the rename to fail.
152+
153+
```yaml
154+
Type: System.Management.Automation.SwitchParameter
155+
Parameter Sets: (All)
156+
Aliases:
157+
158+
Required: False
159+
Position: Named
160+
Default value: None
161+
Accept pipeline input: False
162+
Accept wildcard characters: False
163+
```
164+
165+
### -Permission
166+
If specified the permission (security descriptor) shall be set for the directory/file.
167+
Default value: Inherit.
168+
If SDDL is specified as input, it must have owner, group and dacl.
169+
170+
```yaml
171+
Type: System.String
172+
Parameter Sets: (All)
173+
Aliases:
174+
175+
Required: False
176+
Position: Named
177+
Default value: None
178+
Accept pipeline input: False
179+
Accept wildcard characters: False
180+
```
181+
182+
### -ShareClient
183+
ShareClienr indicated the share where the directory would be listed.
184+
185+
```yaml
186+
Type: Azure.Storage.Files.Shares.ShareClient
187+
Parameter Sets: ShareObject
188+
Aliases:
189+
190+
Required: True
191+
Position: 0
192+
Default value: None
193+
Accept pipeline input: True (ByPropertyName, ByValue)
194+
Accept wildcard characters: False
195+
```
196+
197+
### -ShareDirectoryClient
198+
Source directory instance
199+
200+
```yaml
201+
Type: Azure.Storage.Files.Shares.ShareDirectoryClient
202+
Parameter Sets: DirecotryObject
203+
Aliases:
204+
205+
Required: True
206+
Position: 0
207+
Default value: None
208+
Accept pipeline input: True (ByPropertyName, ByValue)
209+
Accept wildcard characters: False
210+
```
211+
212+
### -ShareName
213+
Name of the file share where the directory would be listed.
214+
215+
```yaml
216+
Type: System.String
217+
Parameter Sets: ShareName
218+
Aliases:
219+
220+
Required: True
221+
Position: 0
222+
Default value: None
223+
Accept pipeline input: False
224+
Accept wildcard characters: False
225+
```
226+
227+
### -SourcePath
228+
Path to an existing directory.
229+
230+
```yaml
231+
Type: System.String
232+
Parameter Sets: ShareName, ShareObject
233+
Aliases:
234+
235+
Required: True
236+
Position: 1
237+
Default value: None
238+
Accept pipeline input: False
239+
Accept wildcard characters: False
240+
```
241+
242+
### CommonParameters
243+
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).
244+
245+
## INPUTS
246+
247+
### Azure.Storage.Files.Shares.ShareClient
248+
249+
### Azure.Storage.Files.Shares.ShareDirectoryClient
250+
251+
### Microsoft.Azure.Commands.Common.Authentication.Abstractions.IStorageContext
252+
253+
## OUTPUTS
254+
255+
### Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageFileDirectory
256+
257+
## NOTES
258+
259+
## RELATED LINKS

0 commit comments

Comments
 (0)