上传图片- PowerShell示例

下面的示例PowerShell脚本处理文件夹中的一系列文件,并将文件上传到配置文件图片API端点。这假设图像是以该人的ID作为文件名命名的,并且文件夹只包含图像文件

Add-Type - assemblyname System.Net.Http $interactUrl = "{到您的交互内网的URL}" $profileSourceApiKey = "{配置文件源API密钥-这是在交互中创建配置文件源时设置的" $profileSourceId = "{交互中配置文件源ID}" $filePath = "{文件系统上文件的路径}" [Net. Net]ServicePointManager]::SecurityProtocol = [Net. Net.]安全ProtocolType]::Tls12 function ProcessFiles(){ Get-ChildItem $FilePath | Foreach-Object { $personId = $_.BaseName $baseUri = $interactUrl + "/api/umi/" + $profileSourceId + "/upload/" + $personId + "/picture" SendFile $baseUri $_.FullName } } function SendFile([string]$uri, [string]$path){ $httpClientHandler = New-Object System.Net.Http.HttpClientHandler $httpClient = New-Object System.Net.Http.Httpclient $httpClientHandler $packageFileStream = New-Object System.IO.FileStream @($path, [System.IO.FileMode]::Open) $contentDispositionHeaderValue = New-Object System.Net.Http.Headers.ContentDispositionHeaderValue "form-data" $contentDispositionHeaderValue.Name = "fileData" $contentDispositionHeaderValue.FileName = (Split-Path $path -leaf) $streamContent = New-Object System.Net.Http.StreamContent $packageFileStream $streamContent.Headers.ContentDisposition = $contentDispositionHeaderValue $streamContent.Headers.ContentType = New-Object System.Net.Http.Headers.MediaTypeHeaderValue "image/jpeg" $content = New-Object System.Net.Http.MultipartFormDataContent $content.Add($streamContent) $httpClient.DefaultRequestHeaders.Add("X-ApiKey", $profileSourceApiKey) try { $response = $httpClient.PostAsync($Uri, $content).Result } catch [Exception] { # log it } finally { if($null -ne $httpClient) { $httpClient.Dispose() } if($null -ne $response) { $response.Dispose() } } } ProcessFiles

Baidu
map