azure - 使用 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
到脚本的末尾,希望这能解决问题,但它返回了同样的错误。
解决方案
请尝试以下代码:
$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
推荐阅读
- mongodb - 如何将数据从一个集合传输到另一个集合
- javascript - 使用确切路径反应路由器嵌套路由
- python - 更新 HTML 表 Python 的 SQL 查询
- c++ - c++中整数的第一个和最后一个数字的总和
- python - 如何在 bash 中执行 python 脚本?
- android-studio - 如何将列表项存储在 string.xml 文件而不是 Java 中?
- c# - unity 跳墙问题
- javascript - reactjs中如何判断props值是否存在
- rust - 如何使用 Rust js_sys 获取浏览器的本地时区?
- python-3.x - 在 setup.py 中包含本地 tar.gz 文件