使用PowerShell将文件上传到Azure容器

f3temu5u  于 2023-04-07  发布在  Shell
关注(0)|答案(1)|浏览(217)

我需要在所有租户设备上运行脚本,以便将文件上传到Azure容器中
我看到这个脚本PowerShell Basics: How to Upload Files to Azure Storage,但我不能管理,使其工作。
我创建了一个SAS密钥,我尝试了帐户密钥和用户委派密钥
这是我的代码

#Variable
$FolderPath= "D:\BatteryReport"
 
#Check if Folder exists
If(!(Test-Path -Path $FolderPath))
{
    New-Item -ItemType Directory -Path $FolderPath
    Write-Host "New folder created successfully!" -f Green
}
Else
{
  Write-Host "Folder already exists!" -f Yellow
}

powercfg /batteryreport /output D:\BatteryReport\BatteryReport_$(hostname).html

Install-Module -Name Az -AllowClobber 

$StorageURL = "https://mytenant.blob.core.windows.net/pub"
$FileName = "BatteryReport_$(hostname).html"
$SASToken = "mySASkey"

$blobUploadParams = @{
    URI = "{0}/{1}?{2}" -f $StorageURL, $FileName, $SASToken
    Method = "PUT"
    Headers = @{
        'x-ms-blob-type' = "BlockBlob"
        'x-ms-blob-content-disposition' = "attachment; filename=`"{0}`"" -f $FileName
        'x-ms-meta-m1' = 'v1'
        'x-ms-meta-m2' = 'v2'
    }
    Body = $Content
    Infile = $FileToUpload
}
yyyllmsg

yyyllmsg1#

要使用SAS令牌发送文件,您可以使用New-AzStorageContext-SasToken参数以及Set-AzStorageBlobContent
示例:

# Get Context and Sends using Connect AzAccount
#Connect-AzAccount
#$StorageAccount =  Get-AzStorageAccount YourResourceGroupName YourStorageAccountName
#$Context = $StorageAccount.Context

# Get Context and Sends using only SAS Token
$SASToken = "mySASkey"
$Context = New-AzStorageContext -StorageAccountName YourStorageAccountName -SasToken $SASToken

#$StorageURL = "https://mytenant.blob.core.windows.net/pub"

$ContainerName = "myfiles"
$FileName = "BatteryReport_$(hostname).html"

$Blob1HT = @{
  File             = 'C:\temp\$FileName' #'D:\Images\$FileName'
  Container        = $ContainerName
  Blob             = $FileName
  Context          = $Context
  StandardBlobTier = 'Hot'
}

Set-AzStorageBlobContent @Blob1HT

参见:
https://stackoverflow.com/a/69031205/194717
文件:
https://learn.microsoft.com/en-us/powershell/module/az.storage/set-azstorageblobcontent?view=azps-9.5.0
https://learn.microsoft.com/en-us/powershell/module/az.storage/new-azstoragecontext?view=azps-9.5.0#example-9-create-a-context-by-using-an-sas-token
https://learn.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-powershell#upload-blobs-to-the-container

相关问题