首页 > 解决方案 > 使用 Start-AzStorageFileCopy 时如何修复“指定的资源不存在。HTTP 状态代码:404”错误

问题描述

我正在尝试将文件从 Azure 存储资源管理器中的一个文件目录复制到源文件目录的子目录。

我最初想使用 AZcopy 将文件复制过来,但 Azcopy 不支持将文件复制到子目录。

所以现在我将使用 Start-AzStorageFileCopy cmdlet 使用 Powershell 路线。

以下是我的完整脚本

$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 

我希望文件能够复制过来,但我收到以下错误。

Start-AzStorageFileCopy:指定的资源不存在。HTTP 状态 >代码:404 - HTTP 错误消息:指定的资源不存在。在 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:指定的资源不存在。HTTP 状态 >代码:404 - HTTP 错误消息:指定的资源不存在。在 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 Cmdlet 之前测试了输出)

我添加了

-DefaultProfile $Context 

到脚本的末尾,希望这能解决问题,但它返回了同样的错误。

标签: azurepowershellazure-storageazure-powershell

解决方案


请尝试以下代码:

$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
}

请注意,在复制到它之前确保存档路径存在。如果它不存在,您可以使用以下代码创建它:

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

推荐阅读