1
votes

I'm trying to copy files from one file directory in Azure Storage Explorer to a sub-directory of the source files directory.

I initially wanted to use AZcopy to copy the files over, but Azcopy doesnt support copying files to a sub-directory.

So now I'm going the Powershell route, using Start-AzStorageFileCopy cmdlet.

Below is my full script

$context = New-AzStorageContext -StorageAccountName "store1" -SasToken "? 
sv=2015-12-11&si=aaa-15D97F9B09D&sr=s&sig=xxx"

# Since the file name has date, you can specify which date to be deleted.
# In this sample, we try to delete the files' name contains previous 
datemonth string eg. "201901"

$pattern =[datetime]::Today.AddMonths(-2).ToString('yyyyMM')
$ShareName = "aaa"
$SourcePath = "Test/Temp_Clean_up_test_Folder"
$ArchivePath = "Test/Temp_Clean_up_test_Folder/Archive/"+        
[datetime]::Today.AddMonths(-2).ToString('yyyyMM')


Get-AzStorageFile -ShareName "aaa" -Path "Test/Temp_Clean_up_test_Folder" - 
Context $context | Get-AzStorageFile | where {($_.GetType().Name -eq 
"CloudFile") -and ($_.Name -like "*$pattern*")} | Start-AzStorageFileCopy - 
DestFilePath $ArchivePath -DestShareName "aaa" -SrcFile {$_} -DestContext 
$Context 

I expect the files to copy over, but I receive the following error.

Start-AzStorageFileCopy : The specified resource does not exist. HTTP Status >Code: 404 - HTTP Error Message: The specified resource does not exist. At line:18 char:201 + ... ttern*")} | Start-AzStorageFileCopy -DestFilePath $ArchivePath -DestS >... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : CloseError: (:) [Start-AzStorageFileCopy], >StorageException + FullyQualifiedErrorId : >StorageException,Microsoft.WindowsAzure.Commands.Storage.File.Cmdlet.StartAzur>eStorageFileCopyCommand

Start-AzStorageFileCopy : The specified resource does not exist. HTTP Status >Code: 404 - HTTP Error Message: The specified resource does not exist. At line:18 char:201 + ... ttern*")} | Start-AzStorageFileCopy -DestFilePath $ArchivePath -DestS >... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : CloseError: (:) [Start-AzStorageFileCopy], >StorageException + FullyQualifiedErrorId : >StorageException,Microsoft.WindowsAzure.Commands.Storage.File.Cmdlet.StartAzur>eStorageFileCopyCommand

NOTES:

The Destination directory already exists.

The Source files Exist (I tested the output before I use Start-azStorageFileCopy Cmdlet)

I added

-DefaultProfile $Context 

to the end of the script, hoping this would solve the issue, but it returned the same error.

1

1 Answers

1
votes

Please try the following code:

$context = New-AzStorageContext -StorageAccountName "xxx" -SasToken "xxx"

$pattern =[datetime]::Today.AddMonths(-2).ToString('yyyyMM')
$sharename = "aaa"
$sourcepath = "Test/Temp_Clean_up_test_Folder"
$archivepath = "Test/Temp_Clean_up_test_Folder/Archive/"+[datetime]::Today.AddMonths(-2).ToString('yyyyMM')

# add the following code to create the archive directory, if it does not exist.
# New-AzStorageDirectory -Context $context -ShareName $sharename -Path $archivepath

 $files = Get-AzStorageFile -ShareName $sharename -Path $sourcepath -Context $context | Get-AzStorageFile | where {($_.GetType().Name -eq "CloudFile") -and ($_.Name -like "*$pattern*")}

 foreach($s1 in $files){ 

 Start-AzStorageFileCopy -SrcShareName $sharename -SrcFilePath (join-path $sourcepath $s1.name) -DestShareName $sharename -DestFilePath (join-path $archivepath $s1.name) -Context $context
}

Please note that make sure the archivepath exists before copy to it. if it does not exists, you can use the following code to create it:

New-AzStorageDirectory -Context $context -ShareName $sharename -Path $archivepath