API - Download a file
So far, you've learned how to authenticate and list files within a specific folder. Now, let's take it a step further and explore broader file management capabilities. In this section, you'll learn how to:
- Check the status of a file
- Download a file
- List all accessible files
List files
First let list files. We already discussed listing files in single folder. Now we'll list files in general. for that we can use GET /vault_rest/files/{type}/{start}/{count}
endpoint.
type
: Accepts either "my" or "all".my
returns only your own files.- all includes all file you can access that can include your files as well as any files shared with you...
start
andcount
: These parameters allow you to paginate results by specifying where to begin and how many files to retrieve.
Lets see an example
# Get your files
try {
$response = Invoke-RestMethod -Uri "$baseUrl/files/my/0/20" -Headers $headers -Method Get
# Write-Host ( $response | ConvertTo-Json | Out-String)
Write-Output $response.items | Select-Object file_id, name, size, creation_date | Format-Table -AutoSize
} catch {
Write-Host "Error Status Code: $($_.Exception.Response.StatusCode.Value__)"
Write-Host "Error Message: $($_.Exception.Message)"
return
}
Now lets look at one single file
File status
We can get status of single file with GET /vault_rest/file/status/{file_id}
endpoint. File status will have data on access rights, file type and security scanning.
# Get file status
try {
$response = Invoke-RestMethod -Uri "$baseUrl/file/status/e3de1b4640cb40af8d386f81ad3f2d79" -Headers $headers -Method Get
Write-Host ( $response | ConvertTo-Json | Out-String)
} catch {
Write-Host "Error Status Code: $($_.Exception.Response.StatusCode.Value__)"
Write-Host "Error Message: $($_.Exception.Message)"
return
}
Download file
Now that we listed files, checked file status, lets downloaded it. For that well use GET /vault_rest/file/{file_id}
endpoint. In our example we had file_id and from that we got file name. You can get file name and id by listing your files.
# Download file
# First we need file_name and name
$file_id = "e3de1b4640cb40af8d386f81ad3f2d79"
# Step 1: Get file name
try {
$file = Invoke-RestMethod -Uri "$baseUrl/file/status/$file_id" -Headers $headers -Method Get
Write-Host ( $file | ConvertTo-Json | Out-String)
} catch {
Write-Host "Error Status Code: $($_.Exception.Response.StatusCode.Value__)"
Write-Host "Error Message: $($_.Exception.Message)"
return
}
# Selecting detination where to save our file.
$outputFile = "C:\Users\user\Documents\Test\$($file.name)"
# Step 2: Get ID of root folder
try {
# Downloading file
Invoke-RestMethod -Uri "$baseUrl/file/$($file.file_id)" -Headers $headers -Method Get -OutFile $outputFile
# Testing success
if (Test-Path $outputFile) {
Write-Host "File $outputFile downloaded successfully."
} else {
Write-Host "File $outputFile download failed."
}
} catch {
Write-Host "Error Status Code: $($_.Exception.Response.StatusCode.Value__)"
Write-Host "Error Message: $($_.Exception.Message)"
return
}