We Want to Hear From You! What Do You Want to See on the Community? Tell us here!
Forum Discussion
alenavarro
4 years agoExplorer | Level 4
Files are no longer uploaded to Dropbox from PowerShell script
Hi everyone!
Hope you can help me out with this one:
We have a VOIP provider which stores calls for a maximum of 30 days, hence, we download them and upload the .wav files to Dropbox so we can have them forever stored.
Until a few days ago, we used the following script to automate the process, HOWEVER, now the calls are downloaded, folders are created on Dropbox, but the files are not uploaded. Did you guys change something in the API or is there something missing now in the code?
function Upload-FileToDropbox {
Param(
[Parameter(Mandatory=$true)]
[string]$SourceFilePath,
[Parameter(Mandatory=$true)]
[string]$TargetFilePath,
[Parameter(Mandatory=$true)]
[string]$DropBoxAccessToken
)
$arg = '{ "path": "' + $TargetFilePath + '", "mode": "add", "autorename": true, "mute": false }'
$authorization = "Bearer $DropBoxAccessToken"
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", $authorization)
$headers.Add("Dropbox-API-Arg", $arg)
$headers.Add("Content-Type", 'application/octet-stream')
Write-Verbose "Uploading file: $SourceFilePath"
try {
Invoke-RestMethod -Uri https://content.dropboxapi.com/2/files/upload -Method Post -InFile $SourceFilePath -Headers $headers
}
catch {
Write-Error $_.Message
}
}
function Create-FolderInDropbox {
Param(
[Parameter(Mandatory=$true)]
[string]$FolderPath,
[Parameter(Mandatory=$true)]
[string]$DropBoxAccessToken
)
$JSONBody = @{ "path"=$FolderPath; "autorename"=$false} | ConvertTo-Json
$authorization = "Bearer $DropBoxAccessToken"
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", $authorization)
$headers.Add("Content-Type", 'application/json')
Write-Verbose "Creating folder: $FolderPath"
Invoke-RestMethod -Uri https://api.dropboxapi.com/2/files/create_folder_v2 -Method Post -Headers $headers -Body $JSONBody
Write-Error $_.Message
}
function Rename-WaveFile {
Param(
[Parameter(Mandatory=$true)]
[System.IO.FileSystemInfo]$File
)
$XMLFile = [xml](Get-Content ($File.Fullname).Replace(".wav", ".xml"))
$StartDate = [DateTime]::Parse($XMLFile.Recording.start).ToString("yyyyMMdd-hh.mm.ss")
$AgentID = $XMLFile.Recording.userid
$FileID = $File.BaseName
$NewFileName = "$FileID-$StartDate-$AgentID.wav"
Write-Verbose "Renaming $($File.Name) to $NewFileName"
$NewFile = Rename-Item -Path $File.Fullname -NewName $NewFileName -Confirm:$false -Force -PassThru
return $NewFile
}
#Confidential credentials
$DropboxAccessToken = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
$User = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
$Password ="xxxxxxxxxx"
$ScriptPath = Split-Path -Parent $MyInvocation.MyCommand.Definition
#Declare variables
$DownloaderExe = "C:\FuzeDownloader\RecordingsDownloader\RecordingsDownloader.exe"
#$Today = (Get-Date).ToString("yyyy-MM-dd")
$Today = "2022-04-21"
$DropboxFolderPath = "/FuzeRecordings/$Today"
$WavPath = "C:\FuzeDownloader\RecordingsDownloader\RecordingsDownloaderOutput\"
$numbers = 0
#Download Wav files
Push-Location $ScriptPath
$DownloaderProc = Start-Process -FilePath $DownloaderExe -ArgumentList "-d$Today -u$User -p$Password" -PassThru -NoNewWindow
while (Get-Process -Id $DownloaderProc.Id) {
if ($(Get-Date) -gt $DownloaderProc.StartTime.AddHours(8)) {
Write-Error -Message "Script has been running for over 8 hours. Something's wrong."
exit 666
}
Start-Sleep -Seconds 5
}
#Create Folder for today's calls
Create-FolderInDropbox -FolderPath $DropboxFolderPath -DropBoxAccessToken $DropboxAccessToken
$WavFiles = Get-ChildItem -Path $WavPath -Include "*.wav" -Recurse
foreach ($Wav in $WavFiles) {
#Rename Wave files to show date, time and user name
$RenamedFile = Rename-WaveFile $Wav
#Once renamed, upload it to Dropbox
Upload-FileToDropbox -SourceFilePath $RenamedFile.FullName -TargetFilePath "$DropboxFolderPath/$($RenamedFile.Name)" -DropBoxAccessToken $DropboxAccessToken
$numbers++
------------------------------------------------------------------------
I've replaced access token and credentials with "xxxxxxx"
No error message is thrown on the output.
Thanks in advance for the help!
7 Replies
- Greg-DB4 years ago
Dropbox Community Moderator
Whenever you make an API call, such as to /2/files/upload like in this code, you would either get a response from the API (such as the result or some API error), or some error from your network client if the connection failed. I understand you're not getting an error message in the output, so you may need to debug this or refer to the documentation for your platform for information on how to access the error, e.g., when using 'Invoke-RestMethod'. That's not made by Dropbox though so I can't offer help with that specifically.
Based on the description and timing though, this may be due to a recent change to our TLS configuration. Dropbox recently retired support for TLS 1.0 and 1.1. The Dropbox API servers now only support connections using TLS 1.2. You'll need to update your app/network client/environment to use TLS 1.2 in order to continue making Dropbox API calls.
- alenavarro4 years agoExplorer | Level 4
Thanks for the answer.
I was able to debug a bit, here's the error I get:
This is indeed an error with Upload-FileToDropbox function...and it looks like a WriteErrorException
Weird, as stated before, the script ran for almost two years without any changes made to it....
We tried running it on a different PC, with most of the updates for the OS (TLS), but still the same....
Any other ideas we might try? @Greg-DB
- Greg-DB4 years ago
Dropbox Community Moderator
The 'Microsoft.PowerShell.Commands.WriteErrorException' error type isn't made by Dropbox, so I can't offer too much insight on that. If a Dropbox API call fails, the Dropbox API would return a response body, but I don't see one here.
I do still expect this is related to the TLS change though, so I recommend looking into making sure your environment uses TLS 1.2. (In the case of a TLS failure, that would be a failure of the actual network connection and so would fail before the Dropbox API call occurs, meaning there wouldn't be an error message from the Dropbox API itself.)
- alenavarro4 years agoExplorer | Level 4
Well, I double checked and the PCs are running TLS 1.2
Should I just check that protocol and disable all others? I don't think that would be recommended
Also, where else can I ask about this?
I'm still insisting that the script worked for TWO YEARS and just a few days ago stopped working...
So if it isn't related to TLS, what else can it be?
- Greg-DB4 years ago
Dropbox Community Moderator
Can you elaborate on what you mean when you say "the PCs are running TLS 1.2"? Can you check if 'Invoke-RestMethod' in particular is using TLS 1.2 for these network requests?
The Dropbox API servers do currently only support TLS 1.2, so it would be fine to enable only TLS 1.2 client-side for this as far as the Dropbox API calls are concerned. Network clients should automatically use the best available version though, so that shouldn't be necessary anyway.
In any case, this is the right forum for help with the Dropbox API. You can also open an API ticket here if you'd prefer to communicate privately.
- alenavarro4 years agoExplorer | Level 4
This is what I mean by that:
How do I check if Invoke-RestMethod is using TLS 1.2?
- Greg-DB4 years ago
Dropbox Community Moderator
It's not clear if those settings mean that Invoke-RestMethod will use TLS 1.2. I can't offer help specifically for Invoke-RestMethod as it's not made by Dropbox, so I recommend referring to its documentation for information on configuring it.
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.6,036 PostsLatest Activity: 2 days ago
The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.
If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X or Facebook.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!