【问题标题】:Upload csv file to Azure BLOB Storage将 csv 文件上传到 Azure BLOB 存储
【发布时间】:2015-04-24 13:20:20
【问题描述】:

我正在尝试将文件(csv 文件)上传到 azure blob 存储。 我找到了一个脚本(见下文),但它在第一个 try catch 块中一直失败。 当您运行脚本时,它会询问您两件事:

  • localpath(我的位于我的 c 盘上的临时文件夹中:c:\temp\test.csv)
  • storagecontainer:我创建了一个名为“kepastorage”的存储,并在其中创建了一个容器“testfiles”。

        [CmdletBinding(SupportsShouldProcess = $true)]
    param(
            [Parameter(Mandatory = $true)]
            [string]$LocalPath,
    
        # The name of the storage container to copy files to.
        [Parameter(Mandatory = $true)]
        [string]$StorageContainer,
    
        # If specified, will recurse the LocalPath specified.
        [Parameter(Mandatory = $false)]
        [switch]$RecurseLocalPath,
    
        # If specified, will create the storage container.
        [Parameter(Mandatory = $false)]
        [switch]$CreateStorageContainer,
    
        # If specified, will copy files to the container if the container already exists.
        [Parameter(Mandatory = $false)]
        [switch]$Force
    )
    
    # The script has been tested on Powershell 3.0
    Set-StrictMode -Version 3
    
    # Following modifies the Write-Verbose behavior to turn the messages on globally for this session
    $VerbosePreference = "Continue"
    
    # Check if Windows Azure Powershell is avaiable
    if ((Get-Module -ListAvailable Azure) -eq $null)
    {
        throw "Windows Azure Powershell not found! Please install from http://www.windowsazure.com/en-us/downloads/#cmd-line-tools"
    }
    
    workflow UploadFilesInParallel
    {
        param(
            # The name of the storage container to copy files to.
            [Parameter(Mandatory = $true)]
            [string]$StorageContainer,
    
            # An array of files to copy to the storage container.
            [Parameter(Mandatory = $true)]
            [System.Object[]]$Files
        )
    
        if ($Files.Count -gt 0)
        {
            foreach -parallel ($file in $Files) 
            {
                $blobFileName = Split-Path -Path $file.FullName -NoQualifier
                try
                {
                    Set-AzureStorageBlobContent -Container $StorageContainer `
                        -File $file.FullName -Blob $blobFileName `
                        -ConcurrentTaskCount 0 -Force
                }
                catch
                {
                    $warningMessage = "Unable to upload file " + $file.FullName
                    Write-Warning -Message $warningMessage
                }
            }
        }
    }
    
    # Ensure the local path given exists. Create it if switch specified to do so.
    if (-not (Test-Path $LocalPath -IsValid))
    {
        throw "Source path '$LocalPath' does not exist.  Specify an existing path."
    }
    
    # Get a list of files from the local folder.
    if ($RecurseLocalPath.IsPresent)
    {
        # $files = ls -Path $LocalPath -File -Recurse
        $files = @(ls -Path $LocalPath -File -Recurse)
    }
    else
    {
        # $files = ls -Path $LocalPath -File
        $files = @(ls -Path $LocalPath -File)
    }
    
    if ($files -ne $null -and $files.Count -gt 0)
    {
        # Create the storage container.
        if ($CreateStorageContainer.IsPresent)
        {
            $existingContainer = Get-AzureStorageContainer | 
                Where-Object { $_.Name -like $StorageContainer }
    
            if ($existingContainer)
            {
                $msg = "Storage container '" + $StorageContainer + "' already exists."
                if (!$Force.IsPresent -and !$PSCmdlet.ShouldContinue(
                        "Copy files to existing container?", $msg))
                {
                    throw "Specify a different storage container name."
                }
            }
            else
            {
                if ($PSCmdlet.ShouldProcess($StorageContainer, "Create Container"))
                {
                    $newContainer = New-AzureStorageContainer -Name $StorageContainer
                    "Storage container '" + $newContainer.Name + "' created."
                }
            }
        }
    
        # Upload the files to storage container.
        $fileCount = $files.Count
        if ($PSCmdlet.ShouldProcess($StorageContainer, "Copy $fileCount files"))
        {
            $time = [DateTime]::UtcNow
            UploadFilesInParallel -StorageContainer $StorageContainer -Files $files
            $duration = [DateTime]::UtcNow - $time
    
            "Uploaded " + $files.Count + " files to blob container '" + $StorageContainer + "'."
            "Total upload time: " + $duration.TotalMinutes + " minutes."
        }
    }
    else
    {
        Write-Warning "No files found."
    }
    

有人知道我做错了什么吗? 谢谢。

【问题讨论】:

  • 您拥有的 only 捕获并不能告诉您实际的错误是什么。对于调试,您可以尝试在 catch 语句中添加 $_.Exception.Message 以让您更好地集中注意力

标签: powershell csv azure


【解决方案1】:

您可以尝试这里解释的这个 PowerShell 脚本:Uploading a file to Azure Blob Storage

$SubscriptionName = ""
$SubscriptionId = ""
$DestContainer = ""
$StorageAccountName = ""
Import-AzurePublishSettingsFile -PublishSettingsFile "<Location of the publishsettings-file>"
Set-AzureSubscription -SubscriptionId $SubscriptionId -CurrentStorageAccountName $StorageAccountName
Select-AzureSubscription -SubscriptionName $SubscriptionName
Set-AzureStorageBlobContent -File "<File you want to upload>" -Container $DestContainer

【讨论】:

  • 感谢您的反馈。我已将这段脚本添加到我的回复中。
猜你喜欢
  • 1970-01-01
  • 2019-12-01
  • 2017-01-24
  • 2017-08-19
  • 2020-07-13
  • 2019-07-10
  • 1970-01-01
  • 1970-01-01
  • 2019-08-13
相关资源
最近更新 更多