Home > Software design >  How to set custom path when using New-AzureStorageContext
How to set custom path when using New-AzureStorageContext

Time:10-05

I am copying some files to azure blob storage. I am following Microsoft docs https://docs.microsoft.com/en-us/powershell/module/azure.storage/new-azurestoragecontainersastoken and https://docs.microsoft.com/en-us/powershell/module/azure.storage/new-azurestoragecontext to create a SAS token. I have the code working but I can't figure out how to set a custom path for URL that New-AzureStorageContext generates, because I would like to copy to a particular path and not the container root. Is there some sort of flag or something for New-AzureStorageContext that will allow me to set this?

# Set AzStorageContext
$destinationContext = New-AzureStorageContext -ConnectionString "DefaultEndpointsProtocol=https;AccountName=xxx;AccountKey=xxx;EndpointSuffix=xxx;"

# Generate SAS URI
$containerSASURI = New-AzureStorageContainerSASToken -Context $destinationContext -ExpiryTime(get-date).AddSeconds(3600) -FullUri -Name "xxx" -Permission rw -Protocol HttpsOnly

My issue is that New-AzureStorageContainerSASToken generates a URL the I use for azure copy destination azcopy copy "xxx" $containerSASURI but it copies to the container root and I would like it to copy to a specific directory e.g \test\demo

CodePudding user response:

Assuming you're manually copying the Container SAS URL from PowerShell and using it in azcopy, simplest would be to insert the path in the SAS URL.

For example, if your Container SAS URL looks something like:

https://account.blob.core.windows.net/container-name?sv=2020-08-04&se=2021-10-04T18:30:00Z&sr=c&sp=w&sig={signature}

and you want to upload the files in test/demo folder inside this container, you will change this SAS URL to something like:

https://account.blob.core.windows.net/container-name/test/demo?sv=2020-08-04&se=2021-10-04T18:30:00Z&sr=c&sp=w&sig={signature}

and use that in azcopy. All the files are then uploaded into test/demo folder inside container-name blob container.

  • Related