API - Upload a file
In this chapter we'll learn how to upload file. Uploading file using API is multi-step process.
- Initiate a group transfer
- Initiate a file transfer
- Upload a file
- Complete a file transfer
- Complete a group transfer
Initiate group transfer
You can upload multiple files in single group upload. To initiate group transfer use GET/vault_rest/transfer``endpoint. Here is an example:
# Initiate group transfertry { $response = Invoke-RestMethod -Uri "$baseUrl/transfer" -Headers $headers -Method Get #Write-Host ( $file | ConvertTo-Json | Out-String) Write-Host "Group ID: $($response.group_id)"} catch { Write-Host "Error Status Code: $($_.Exception.Response.StatusCode.Value__)" Write-Host "Error Message: $($_.Exception.Message)" return}Initiate file transfer
Now we'll create empty file on server where we'll stream data from our local file. for that we use GET /vault_rest``/transfer_fileendpoint. We need to send some essential data with headers.
transfer_method– Method used for transferring the file as Binary data either Stream or Chunk.group_id– The transfer group ID.file_size– Size of the file in bytes.file_name– Name of the file to be uploaded. (Example: Testing image 2.png → Testing image 2)
For larger files, this method may cause issues due to size limitations. In such cases, you should upload the file in chunks (typically 10MB per chunk). To enable chunked upload, make sure to include the chunk_offset header in your request.
# Initiate file transfer$upload_file = "C:\Users\user\Documents\Test\image.jpg" # check if local file existsif (Test-Path $upload_file) { # Get file size $file_size = (Get-Item $upload_file).Length # Get file name $file_name = Split-Path -Path $upload_file -Leaf} else { Write-Host "File does not exist."}# Prepare headers$headers_ft = @{ Authorization = "Bearer $token" transfer_method = "Stream" group_id = $group_id file_name = $file_name file_size = $file_size}try { $response = Invoke-RestMethod -Uri "$baseUrl/transfer_file" -Headers $headers_ft -Method Get #Write-Host ( $file | ConvertTo-Json | Out-String) $file_id = $response.file_id Write-Host "File ID: $($response.file_id)"} catch { Write-Host "Error Status Code: $($_.Exception.Response.StatusCode.Value__)" Write-Host "Error Message: $($_.Exception.Message)" return}Upload a file
For uploading file we'll use PATCH/vault_rest/transfer_file endpoint. For this transfer we need to sed some headers:
group_id– The group transfer ID received from the Initialize Group Transfer stepfile_id– The file ID received from the Initialize File Transfer step
#Upload files$upload_file = "C:\Users\user\Documents\Test\image.jpg" # Prepare headers$headers_ft = @{ Authorization = "Bearer $token" file_id = $file_id group_id = $group_id}try { $file_bytes = [System.IO.File]::ReadAllBytes($upload_file) $response = Invoke-RestMethod -Uri "$baseUrl/transfer_file" -Headers $headers_ft -Method Patch -Body $file_bytes -ContentType "application/octet-stream" # Write-Host ( $response | ConvertTo-Json | Out-String) Write-Host "File uploaded: $($response.result)"} catch { Write-Host "Error Status Code: $($_.Exception.Response.StatusCode.Value__)" Write-Host "Error Message: $($_.Exception.Message)" return}Complete file transfer
For uploading file we'll use POST/vault_rest/transfer_file endpoint. For this transfer we need to sed some headers:
group_id– The group transfer ID received from the Initialize Group Transfer stepfile_id– The file ID received from the Initialize File Transfer step
# Close file transfer$headers_ft = @{ Authorization = "Bearer $token" file_id = $file_id group_id = $group_id}try { $response = Invoke-RestMethod -Uri "$baseUrl/transfer_file" -Headers $headers_ft -Method Post # Write-Host ( $response | ConvertTo-Json | Out-String) Write-Host "File transfer closed: $($response.result)"} catch { Write-Host "Error Status Code: $($_.Exception.Response.StatusCode.Value__)" Write-Host "Error Message: $($_.Exception.Message)" return}Complete group transfer
For uploading file we'll use POST/vault_rest/transfer endpoint. For this transfer we need to sed some headers:
group_id– The group transfer ID received from the Initialize Group Transfer step
# Close group transfer$headers_ft = @{ Authorization = "Bearer $token" group_id = $group_id}try { $response = Invoke-RestMethod -Uri "$baseUrl/transfer" -Headers $headers_ft -Method Post # Write-Host ( $response | ConvertTo-Json | Out-String) Write-Host "Group transfer closed: Success"} catch { Write-Host "Error Status Code: $($_.Exception.Response.StatusCode.Value__)" Write-Host "Error Message: $($_.Exception.Message)" return}Conclusion
Process described in this document will upload one smaller file. To upload more files, repeat steps 2, 3, 4 for each file. In case of bigger files upload file in more chunks of around 10MB by repeating step 3.
Whole example
Here is whole ting on one place. Make sure to change configuration variable at the beginning of the code.
### OPSWAT MetaDefender MFT - File upload example #### file to upload$upload_file = "C:\Users\vagrant\Documents\Test\image.jpg" # Configuration$baseUrl = "http://localhost:8010/vault_rest"$username = "username"$password = "password"# ========================================================= ## Encode credentials using CRLF separator and Base64$rawCreds = "$username`r`n$password"$encodedCreds = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($rawCreds))# grapgical headerWrite-Host "#=============================================#"Write-Host "| OPSWAT - MetaDefender Managed File Transfer |"Write-Host "+---------------------------------------------+"Write-Host "| https://www.opswat.com/ |"Write-Host "#=============================================#"# Step 1: Authenticate and get token$authHeaders = @{}try {} catch {}$token = $authResponse.tokenWrite-Host "Authenticated. Token: $token"$headers = @{}# Initiate group transfertry {} catch {}# Initiate file transfer# check if local file existsif (Test-Path $upload_file) {} else {}# Prepare headers$headers_ft = @{}try {} catch {}#Upload files# Prepare headers$headers_ft = @{}try {} catch {}# Close file transfer$headers_ft = @{}try {} catch {}# Close group transfer$headers_ft = @{}try {} catch {}