Azure Blob Storage: 파일 복사에 대한 PowerShell 가이드

배우세요. PowerShell Set-AzureStorageBlobContent cmdlet을 사용하여 Azure Blob Storage에 파일을 복사하는 방법에 대해 이 편리한 자습서에서 배우세요.

I’ve been doing a lot of Azure IaaS work via ARM lately in PowerShell. As a result, I’ve unfortunately found out how bad the documentation and behavior is for the Azure PowerShell module but I’ve persisted and have overcome!

이 프로젝트의 일환으로 Azure 저장소 계정 컨테이너로 여러 파일을 업로드해야 했습니다. PowerShell의 Copy-Item cmdlet을 파일 복사에 사용해 왔기 때문에 Azure의 경우에도 비슷한 기능이 있을 것으로 생각했지만 Azure PowerShell 모듈에서는 실망스러운 결과를 얻었습니다. 대신, 하나의 파일을 공통 저장 컨테이너로 전송하려면 세 개의 별도 cmdlet을 사용해야 했습니다.

이를 어떻게 해결할지 한 번 알아낸 후에는 매번 파일을 Azure 저장 컨테이너로 전송하는 방법을 기억하고 싶지 않았습니다. 따라서 PowerShell 개발자라면 누구나 하는 대로, Copy-AzureItem이라는 사용하기 쉬운 함수를 만들어 파일을 Azure Blob Storage로 복사했습니다. 이 함수는 제게 많은 시간을 절약해 주었으며 여러분에게도 도움이 되기를 바랍니다.

작동 방법은 다음과 같습니다:

먼저 Azure ARM 저장 컨테이너에 파일을 넣으려면 세 가지 다른 “객체”가 필요합니다. 저장 계정, 저장 계정 컨테이너 및 블롭 또는 파일 자체입니다. 파일을 업로드할 때 각각의 “객체”를 지정해야 합니다. 이를 위해 한 줄에 세 가지 다른 cmdlet을 사용할 수 있습니다.

Get-AzStorageAccount @saParams | Get-AzStorageContainer @scParams | Set-AzureStorageBlobContent@bcParams

알 수 있듯이 각 cmdlet에 다양한 매개변수를 제공하기 위해 splatting을 사용하고 있습니다.

다 Azure에 파일 복사하려고 이렇게 다 해야 해요? 싫어요! 대신, 이렇게 하는 게 어때요?

Copy-AzureItem -FilePath C:\MyFile.exe -ContainerName azcontainer

훨씬 쉬워요! 기능에서 리소스 그룹과 저장소 계정을 기본값으로 설정하지만 간단히 업데이트할 수 있어요.

그럼 더 이상 말이 필요 없어요. 내 Github 저장소에서 이 기능을 다운로드하세요. 그게 귀찮다면 여기서 복사해서 붙여넣으세요.

function Copy-AzureItem
{
	<#
	.SYNOPSIS
		이 기능은 파일을 Azure 저장소 계정에 업로드하는 과정을 단순화합니다. 이 기능을 사용하려면 이미 Login-AzureAccount로 Azure 구독에 로그인해야 합니다. 업로드된 파일은 저장된 Blob의 파일 이름과 같을 것입니다.
		
	.PARAMETER FilePath
		로컬 경로에서 Azure 저장소 계정 컨테이너로 업로드하려는 파일의 경로입니다.
	
	.PARAMETER ContainerName
		파일이 위치할 Azure 저장소 계정 컨테이너의 이름입니다.
	
	.PARAMETER ResourceGroupName
		저장소 계정이 있는 리소스 그룹의 이름입니다.
	
	.PARAMETER StorageAccountName
		파일이 들어갈 컨테이너의 저장소 계정의 이름입니다.
	#>
	[CmdletBinding()]
	param
	(
		[Parameter(Mandatory,ValueFromPipelineByPropertyName)]
		[ValidateNotNullOrEmpty()]
		[ValidateScript({ Test-Path -Path $_ -PathType Leaf })]
		[Alias('FullName')]
		[string]$FilePath,
	
		[Parameter(Mandatory)]
		[ValidateNotNullOrEmpty()]
		[string]$ContainerName,
	
		[Parameter()]
		[ValidateNotNullOrEmpty()]
		[string]$ResourceGroupName = 'ResourceGroup',
	
		[Parameter()]
		[ValidateNotNullOrEmpty()]
		[string]$StorageAccountName = 'StorageAccount'
	)
	process
	{
		try
		{
			$saParams = @{
				'ResourceGroupName' = $ResourceGroupName
				'Name' = $StorageAccountName
			}
			
			$scParams = @{
				'Container' = $ContainerName
			}
			
			$bcParams = @{
				'File' = $FilePath
				'Blob' = ($FilePath | Split-Path -Leaf)
			}
			Get-AzureRmStorageAccount @saParams | Get-AzureStorageContainer @scParams | Set-AzureStorageBlobContent @bcParams
		}
		catch
		{
			Write-Error $_.Exception.Message
		}
	}
}

Source:
https://adamtheautomator.com/copy-files-to-azure-blob-storage/