Skip to content

Commit 8539935

Browse files
author
rajatagrawal-dev
authored
fixed single file blob upload (#7222) (#7286)
* fixed single file blob upload (#7222) * fixed single file blob upload * fixed single file blob upload * fixed L0 * clean target issue + pr comments * pr feedback * updated patch * pr feedback
1 parent 09f6712 commit 8539935

File tree

6 files changed

+65
-22
lines changed

6 files changed

+65
-22
lines changed

Tasks/AzureFileCopyV2/AzureFileCopyRemoteJob.ps1

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@ $AzureFileCopyRemoteJob = {
2323
}
2424
}
2525

26+
function Write-LogsAndCleanup
27+
{
28+
[CmdletBinding()]
29+
param(
30+
[Nullable[bool]]$isLogsPresent,
31+
[AllowEmptyString()][string]$logFilePath,
32+
[AllowEmptyString()][string]$azCopyLocation
33+
)
34+
35+
# Print AzCopy.exe verbose logs
36+
Get-AzCopyVerboseLogs -isLogsPresent $isLogsPresent -logFilePath $logFilePath -ErrorAction SilentlyContinue
37+
38+
# Delete AzCopy tool folder
39+
Remove-AzCopyFolder -azCopyLocation $azCopyLocation -ErrorAction SilentlyContinue
40+
}
41+
2642
function Get-AzCopyVerboseLogs
2743
{
2844
[CmdletBinding()]
@@ -51,6 +67,8 @@ $AzureFileCopyRemoteJob = {
5167

5268
try
5369
{
70+
$useDefaultArguments = ($additionalArguments -eq "")
71+
5472
$azCopyToolFileNames = $azCopyToolFileNamesString.Split(";")
5573
$azCopyToolFileContents = $azCopyToolFileContentsString.Split(";")
5674

@@ -72,15 +90,21 @@ $AzureFileCopyRemoteJob = {
7290

7391
if($CleanTargetBeforeCopy)
7492
{
75-
Get-ChildItem -Path $targetPath -Recurse -Force | Remove-Item -Force -Recurse
76-
Write-DetailLogs "Destination location cleaned"
93+
if (Test-Path $targetPath -PathType Container)
94+
{
95+
Get-ChildItem -Path $targetPath -Recurse -Force | Remove-Item -Force -Recurse
96+
Write-DetailLogs "Destination location cleaned"
97+
}
98+
else
99+
{
100+
Write-DetailLogs "Folder at path $targtPath not found for cleanup."
101+
}
77102
}
78103

79104
$azCopyExeLocation = Join-Path -Path $azCopyDestinationPath -ChildPath "AzCopy.exe"
80105

81106
$logFileName = "AzCopyVerbose_" + [guid]::NewGuid() + ".log"
82107
$logFilePath = Join-Path -Path $azCopyDestinationPath -ChildPath $logFileName
83-
$useDefaultArguments = ($additionalArguments -eq "")
84108

85109
if($useDefaultArguments)
86110
{
@@ -94,9 +118,9 @@ $AzureFileCopyRemoteJob = {
94118
$additionalArguments = "/Z:`"$azCopyDestinationPath`" /V:`"$logFilePath`" /S /Y"
95119
}
96120

97-
Write-DetailLogs "Executing command: & `"$azCopyExeLocation`" /Source:$containerURL /Dest:`"$targetPath`" /SourceSAS:`"*****`" $additionalArguments"
121+
Write-DetailLogs "##[command] & `"$azCopyExeLocation`" /Source:`"$containerURL`" /Dest:`"$targetPath`" /SourceSAS:`"*****`" $additionalArguments"
98122

99-
$azCopyCommand = "& `"$azCopyExeLocation`" /Source:$containerURL /Dest:`"$targetPath`" /SourceSAS:`"$containerSasToken`" $additionalArguments"
123+
$azCopyCommand = "& `"$azCopyExeLocation`" /Source:`"$containerURL`" /Dest:`"$targetPath`" /SourceSAS:`"$containerSasToken`" $additionalArguments"
100124
Invoke-Expression $azCopyCommand
101125
}
102126
catch
@@ -106,10 +130,6 @@ $AzureFileCopyRemoteJob = {
106130
}
107131
finally
108132
{
109-
# Print AzCopy.exe verbose logs
110-
Get-AzCopyVerboseLogs -isLogsPresent $useDefaultArguments -logFilePath $logFilePath -ErrorAction SilentlyContinue
111-
112-
# Delete AzCopy tool folder
113-
Remove-AzCopyFolder -azCopyLocation $azCopyDestinationPath -ErrorAction SilentlyContinue
133+
Write-LogsAndCleanup -isLogsPresent $useDefaultArguments -logFilePath "$logFilePath" -azCopyLocation "$azCopyDestinationPath" -ErrorAction SilentlyContinue
114134
}
115135
}

Tasks/AzureFileCopyV2/Strings/resources.resjson/en-US/resources.resjson

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"loc.input.label.TargetPath": "Destination Folder",
3838
"loc.input.help.TargetPath": "Local path on the target machines for copying the files from the source. Environment variable can be used like $env:windir\\BudgetIT\\Web.",
3939
"loc.input.label.AdditionalArgumentsForBlobCopy": "Optional Arguments (for uploading files to blob)",
40-
"loc.input.help.AdditionalArgumentsForBlobCopy": "Optional AzCopy.exe arguments that will be applied when uploading to blob like, /NC:10. If no optional arguments are specified here, the following optional arguments will be added by default.<br> /Y, /SetContentType, /Z, /V,<br> /S (only if container name is not $root),<br> /BlobType:page (only if specified storage account is a premium account)",
40+
"loc.input.help.AdditionalArgumentsForBlobCopy": "Optional AzCopy.exe arguments that will be applied when uploading to blob like, /NC:10. If no optional arguments are specified here, the following optional arguments will be added by default.<br> /Y, /SetContentType, /Z, /V,<br> /S (only if container name is not $root),<br> /BlobType:page (only if specified storage account is a premium account).<br> If source path is a file, /Pattern will always be added irrespective of whether or not specified in optional arguments.",
4141
"loc.input.label.AdditionalArgumentsForVMCopy": "Optional Arguments (for downloading files to VM)",
4242
"loc.input.help.AdditionalArgumentsForVMCopy": "Optional AzCopy.exe arguments that will be applied when downloading to VM like, /NC:10. If no optional arguments are specified here, the following optional arguments will be added by default.<br> /Y, /S, /Z, /V",
4343
"loc.input.label.enableCopyPrerequisites": "Enable Copy Prerequisites",
@@ -93,5 +93,6 @@
9393
"loc.messages.AFC_BlobStorageNotFound": "Storage account: {0} not found. Please specify existing storage account",
9494
"loc.messages.AFC_RootContainerAndDirectory": "'/S' option is not valid for $root containers.",
9595
"loc.messages.AFC_RedirectResponseInvalidStatusCode": "The HTTP response code: '{0}' is not a valid redirect status code",
96-
"loc.messages.AFC_RedirectResponseLocationHeaderIsNull": "Redirect response location header is null."
96+
"loc.messages.AFC_RedirectResponseLocationHeaderIsNull": "Redirect response location header is null.",
97+
"loc.messages.AFC_AzCopyBlobUploadNonZeroExitCode": "AzCopy.exe exited with non-zero exit code while uploading files to blob storage."
9798
}

Tasks/AzureFileCopyV2/Tests/L0UploadFilesToAzureContainer.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ $invalidInputStorageAccount = "invalidInputStorageAccount"
99
$exceptionMessage = "Exception thrown"
1010

1111
Register-Mock Write-Telemetry { }
12+
Register-Mock Test-Path { return $false }
1213

1314
# Test 1 "Should throw if Invoke-Expression fails"
1415
Register-Mock Invoke-Expression { throw $exceptionMessage }
@@ -31,6 +32,7 @@ Assert-WasCalled Remove-AzureContainer -Times 1
3132
# Test 3 "Success in Upload blob destination"
3233
Unregister-Mock Invoke-Expression
3334
Register-Mock Invoke-Expression { return $succeededCopyResponse }
35+
$LASTEXITCODE = 0
3436

3537
Upload-FilesToAzureContainer -sourcePath $validInputSourcePath -storageAccountName $validInputStorageAccount -containerName $validInputContainerName `
3638
-blobPrefix $validInputBlobPrefix -storageKey $validStorageKey -azCopyLocation $validAzCopyLocation -destinationType $validInputAzureBlobDestinationType

Tasks/AzureFileCopyV2/Utility.ps1

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,16 +247,34 @@ function Upload-FilesToAzureContainer
247247
{
248248
Write-Output (Get-VstsLocString -Key "AFC_UploadFilesStorageAccount" -ArgumentList $sourcePath, $storageAccountName, $containerName, $blobPrefix)
249249

250+
$resolvedSourcePath = $sourcePath
251+
252+
# Check if source path is a file. If yes, then add /Pattern additional argument.
253+
if(Test-Path -Path $sourcePath -PathType Leaf)
254+
{
255+
$fileInfo = Get-Item $sourcePath
256+
$resolvedSourcePath = $fileInfo.Directory.FullName
257+
$additionalArguments += " /Pattern:`"$($fileInfo.Name)`""
258+
}
259+
250260
$blobPrefix = $blobPrefix.Trim()
251261
$containerURL = [string]::Format("{0}/{1}/{2}", $blobStorageEndpoint.Trim("/"), $containerName, $blobPrefix).Trim("/")
252262
$azCopyExeLocation = Join-Path -Path $azCopyLocation -ChildPath "AzCopy.exe"
253263

254-
Write-Verbose "Executing command: & `"$azCopyExeLocation`" /Source:$sourcePath /Dest:$containerURL /DestKey:`"*****`" $additionalArguments"
264+
Write-Output "##[command] & `"$azCopyExeLocation`" /Source:`"$resolvedSourcePath`" /Dest:`"$containerURL`" /DestKey:`"*****`" $additionalArguments"
255265

256-
$uploadToBlobCommand = "& `"$azCopyExeLocation`" /Source:$sourcePath /Dest:$containerURL /DestKey:`"$storageKey`" $additionalArguments"
266+
$uploadToBlobCommand = "& `"$azCopyExeLocation`" /Source:`"$resolvedSourcePath`" /Dest:`"$containerURL`" /DestKey:`"$storageKey`" $additionalArguments"
257267

258268
Invoke-Expression $uploadToBlobCommand
259-
Write-Output (Get-VstsLocString -Key "AFC_UploadFileSuccessful" -ArgumentList $sourcePath, $storageAccountName, $containerName, $blobPrefix)
269+
270+
if($LASTEXITCODE -eq 0)
271+
{
272+
Write-Output (Get-VstsLocString -Key "AFC_UploadFileSuccessful" -ArgumentList $sourcePath, $storageAccountName, $containerName, $blobPrefix)
273+
}
274+
else
275+
{
276+
throw (Get-VstsLocString -Key "AFC_AzCopyBlobUploadNonZeroExitCode")
277+
}
260278
}
261279
catch
262280
{
@@ -283,8 +301,8 @@ function Handle-AzCopyLogs
283301
{
284302
[CmdletBinding()]
285303
param(
286-
[bool]$isLogsPresent,
287-
[string]$logsFilePath
304+
[Nullable[bool]]$isLogsPresent,
305+
[AllowEmptyString()][string]$logsFilePath
288306
)
289307

290308
if($isLogsPresent)

Tasks/AzureFileCopyV2/task.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"version": {
1414
"Major": 2,
1515
"Minor": 0,
16-
"Patch": 0
16+
"Patch": 1
1717
},
1818
"preview": true,
1919
"demands": [
@@ -207,7 +207,7 @@
207207
"label": "Optional Arguments (for uploading files to blob)",
208208
"required": false,
209209
"defaultValue": "",
210-
"helpMarkDown": "Optional AzCopy.exe arguments that will be applied when uploading to blob like, /NC:10. If no optional arguments are specified here, the following optional arguments will be added by default.<br> /Y, /SetContentType, /Z, /V,<br> /S (only if container name is not $root),<br> /BlobType:page (only if specified storage account is a premium account)"
210+
"helpMarkDown": "Optional AzCopy.exe arguments that will be applied when uploading to blob like, /NC:10. If no optional arguments are specified here, the following optional arguments will be added by default.<br> /Y, /SetContentType, /Z, /V,<br> /S (only if container name is not $root),<br> /BlobType:page (only if specified storage account is a premium account).<br> If source path is a file, /Pattern will always be added irrespective of whether or not specified in optional arguments."
211211
},
212212
{
213213
"name": "AdditionalArgumentsForVMCopy",
@@ -344,6 +344,7 @@
344344
"AFC_BlobStorageNotFound": "Storage account: {0} not found. Please specify existing storage account",
345345
"AFC_RootContainerAndDirectory": "'/S' option is not valid for $root containers.",
346346
"AFC_RedirectResponseInvalidStatusCode": "The HTTP response code: '{0}' is not a valid redirect status code",
347-
"AFC_RedirectResponseLocationHeaderIsNull": "Redirect response location header is null."
347+
"AFC_RedirectResponseLocationHeaderIsNull": "Redirect response location header is null.",
348+
"AFC_AzCopyBlobUploadNonZeroExitCode": "AzCopy.exe exited with non-zero exit code while uploading files to blob storage."
348349
}
349350
}

Tasks/AzureFileCopyV2/task.loc.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"version": {
1414
"Major": 2,
1515
"Minor": 0,
16-
"Patch": 0
16+
"Patch": 1
1717
},
1818
"preview": true,
1919
"demands": [
@@ -358,6 +358,7 @@
358358
"AFC_BlobStorageNotFound": "ms-resource:loc.messages.AFC_BlobStorageNotFound",
359359
"AFC_RootContainerAndDirectory": "ms-resource:loc.messages.AFC_RootContainerAndDirectory",
360360
"AFC_RedirectResponseInvalidStatusCode": "ms-resource:loc.messages.AFC_RedirectResponseInvalidStatusCode",
361-
"AFC_RedirectResponseLocationHeaderIsNull": "ms-resource:loc.messages.AFC_RedirectResponseLocationHeaderIsNull"
361+
"AFC_RedirectResponseLocationHeaderIsNull": "ms-resource:loc.messages.AFC_RedirectResponseLocationHeaderIsNull",
362+
"AFC_AzCopyBlobUploadNonZeroExitCode": "ms-resource:loc.messages.AFC_AzCopyBlobUploadNonZeroExitCode"
362363
}
363364
}

0 commit comments

Comments
 (0)