Reputation: 5520
I have Storage Account in Azure which contains files inside the folders. I want to move the .txt and .csv files from one folder to other folder in same Storage Account Container using PowerShell script.
So, can anyone suggest me how to do that?
Upvotes: 3
Views: 13364
Reputation: 91
This is my code from the answer above.
$context = New-AzStorageContext -StorageAccountName TESTACC -StorageAccountKey PpzcfuKhruAhQL0ZMR
$Blobs = Get-AzStorageBlob -Container "raw-voi-2021" -Blob 2021-11/*2021-09* -Context $context
foreach ($blob in $Blobs) {
$blob.Name
$file=$blob.Name.Split("/")[1]
Start-AzStorageBlobCopy -SrcBlob $blob.Name -SrcContainer "raw-voi-2021" -context $context -DestContainer "raw-voi-2021" -DestBlob "2021-09/$file" -DestContext $context
Get-AzStorageBlob -Container "raw-voi-2021" -blob "2021-09/$file" -Context $context |select name, length,lastmodified
}
Upvotes: 0
Reputation: 41
Please note the copy is asynchronous, so make sure the blob is in the destination location before you delete the source. It is said that a copy within the same container is ‘instantaneous’, more like a move operation (which does not exist, nor needs to be in the API because of this)
Upvotes: 0
Reputation: 2336
Adding more info to the above suggestion, the easiest way is to use Storage Explorer
This So thread provides some idea (PS) on your query.
Upvotes: 0
Reputation: 7483
Add the complete steps with Swishonary's reply:
$context = New-AzureStorageContext -StorageAccountName {accountName} -StorageAccountKey {Enter your storage account key here}
$Blobs = Get-AzStorageBlob -Container "SourceContainer" -Blob SourceFolder/*.csv -Context $context
foreach ($blob in $Blobs) {
$blob.Name
# Copy to DestinationFolder
Start-AzStorageBlobCopy -SrcBlob "SourceFolder/SourceFile" -SrcContainer "<SourceContainer>" -DestContainer "<DestinationContainer>" -DestBlob "DestinationFolder/DestinationFile"
# Delete the source blob
Remove-AzStorageBlob -Container "SourceContainer" -Blob $blob.Name
}
Upvotes: 1
Reputation: 93
Please use azcopy in Powershell to acheive this.
azcopy login
azcopy copy 'https://<SourceStorageaccount>.blob.core.windows.net/<SourceContainer>/<SourceFile>' 'https://<DestinationStorageaccount>.blob.core.windows.net/<DestinationContainer>/<DestinationFile>'
The AzCopy command-line utility offers high-performance, scriptable data transfer for Azure Storage. You can use AzCopy to transfer data to and from Blob storage and Azure Files.
Alternatively, I think this can also be used:
Start-AzStorageBlobCopy -SrcBlob "SourceFolder/SourceFile" -SrcContainer "<SourceContainer>" -DestContainer "<DestinationContainer>" -DestBlob "DestinationFolder/DestinationFile"
At the moment, there seems to be no Powershell cmdlets in the Az.Storage module to move files. They would have to be copied to the destination and deleted from the source.
Upvotes: 0