The following article describes how Upload API V2 can be used:
https://docs.stylelabs.com/en-us/contenthub/4.0.x/content/integrations/rest-api/upload/upload-api-v2.html.
Let’s demonstate the usage of API using Powershell 7. Uploading flow consists of three steps:
Request an upload
First, an asset needs to be created. It can be done using the following code:
$xAuthToken = "enter your auth token guid here" $hostName = "https://YourCHHostname"
$fileName = "Test.png" $filePath = "C:\TempDownloads\Test.png" $fileSize = 792
$createAssetBody = @{ file_name = $fileName file_size = $fileSize upload_configuration = @{ name = "AssetUploadConfiguration" } action = @{ name = "NewAsset" } } | ConvertTo-Json
$createUrl = $hostName + "/api/v2.0/upload"
$createResponse = Invoke-WebRequest -Uri $createUrl -Method 'POST' -Body $createAssetBody -H @{"x-auth-token" = $xAuthToken; "Content-Type" = "application/json"}
|
In the response, we will get the following data:
- Response body has upload_identifier and file_identifier
{ "upload_identifier": "uploadidentifier", "file_identifier": "fileidentifier" }
|
- Location response Header contains the URL where we need to upload the data.
For uploading the file, the approach described in this article is used: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-webrequest?view=powershell-7.2#example-5--submit-a-multipart-form-data-file
$uploadUrl = $createResponse.Headers.Location[0]
$fileStream = [System.IO.FileStream]::new($filePath, [System.IO.FileMode]::Open) $fileHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new('form-data') $fileHeader.Name = $fileName $fileHeader.FileName = Split-Path -leaf $filePath $fileContent = [System.Net.Http.StreamContent]::new($fileStream) $fileContent.Headers.ContentDisposition = $fileHeader $fileContent.Headers.ContentType = [System.Net.Http.Headers.MediaTypeHeaderValue]::Parse("text/plain")
$multipartContent = [System.Net.Http.MultipartFormDataContent]::new() $multipartContent.Add($fileContent)
Invoke-WebRequest -Body $multipartContent -Method 'POST' -Uri $uploadUrl -Headers @{"x-auth-token" = $xAuthToken}
$fileStream.Close()
|
Finalize the upload
To finalize the request, we need to post the response body received on the “Request an upload” step:
$finalizeUrl = $hostName + "/api/v2.0/upload/finalize"
Invoke-WebRequest -Uri $finalizeUrl -Method 'POST' -Body $createResponse.Content -H @{"x-auth-token" = $xAuthToken; "Content-Type" = "application/json"}
|
Full script
$xAuthToken = "enter your auth token guid here" $hostName = "https://YourCHHostname"
$fileName = "Test.png" $filePath = "C:\TempDownloads\Test.png" $fileSize = 792
$createAssetBody = @{ file_name = $fileName file_size = $fileSize upload_configuration = @{ name = "AssetUploadConfiguration" } action = @{ name = "NewAsset" } } | ConvertTo-Json
$createUrl = $hostName + "/api/v2.0/upload"
$createResponse = Invoke-WebRequest -Uri $createUrl -Method 'POST' -Body $createAssetBody -H @{"x-auth-token" = $xAuthToken; "Content-Type" = "application/json"}
$uploadUrl = $createResponse.Headers.Location[0]
$fileStream = [System.IO.FileStream]::new($filePath, [System.IO.FileMode]::Open) $fileHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new('form-data') $fileHeader.Name = $fileName $fileHeader.FileName = Split-Path -leaf $filePath $fileContent = [System.Net.Http.StreamContent]::new($fileStream) $fileContent.Headers.ContentDisposition = $fileHeader $fileContent.Headers.ContentType = [System.Net.Http.Headers.MediaTypeHeaderValue]::Parse("text/plain")
$multipartContent = [System.Net.Http.MultipartFormDataContent]::new() $multipartContent.Add($fileContent)
Invoke-WebRequest -Body $multipartContent -Method 'POST' -Uri $uploadUrl -Headers @{"x-auth-token" = $xAuthToken}
$fileStream.Close()
$finalizeUrl = $hostName + "/api/v2.0/upload/finalize"
Invoke-WebRequest -Uri $finalizeUrl -Method 'POST' -Body $createResponse.Content -H @{"x-auth-token" = $xAuthToken; "Content-Type" = "application/json"}
|