Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save e0x70i/547f979143eb71ffc9530af4f7cd6df1 to your computer and use it in GitHub Desktop.
Save e0x70i/547f979143eb71ffc9530af4f7cd6df1 to your computer and use it in GitHub Desktop.
Powershell upload to dropbox converted to powershell2
<#
Ported to powershell2 from script by Laurent Kempé
http://laurentkempe.com/2016/04/07/Upload-files-to-DropBox-from-PowerShell/
.SYNOPSIS
This is a Powershell script to upload a file to DropBox using their REST API.
.DESCRIPTION
This Powershell script will upload file to DropBox using their REST API with the parameters you provide.
.PARAMETER SourceFilePath
The path of the file to upload.
.PARAMETER TargetFilePath
The path of the file on DropBox.
.ENV PARAMETER DropBoxAccessToken
The DropBox access token.
#>
function Invoke-DropboxUpload {
Param(
[Parameter(Mandatory=$true)]
[string]$SourceFilePath,
[Parameter(Mandatory=$true)]
[string]$TargetFilePath,
[Parameter(mandatory=$true)]
[string]$ApiKey
)
$url = "https://content.dropboxapi.com/2/files/upload"
$file = [IO.File]::ReadAllBytes($SourceFilePath)
[net.httpWebRequest] $req = [net.webRequest]::create($url)
$arg = '{ "path": "' + $TargetFilePath + '", "mode": "add", "autorename": true, "mute": false }'
$authorization = "Bearer " + $ApiKey
$req.method = "POST"
$req.Headers.Add("Authorization", $authorization)
$req.Headers.Add("Dropbox-API-Arg", $arg)
$req.ContentType = 'application/octet-stream'
$req.ContentLength = $file.length
$req.TimeOut = 50000
$req.KeepAlive = $true
$req.Headers.Add("Keep-Alive: 300");
$reqst = $req.getRequestStream()
$reqst.write($file, 0, $file.length)
$reqst.flush()
$reqst.close()
[net.httpWebResponse] $res = $req.getResponse()
$resst = $res.getResponseStream()
$sr = new-object IO.StreamReader($resst)
$result = $sr.ReadToEnd()
$result
$res.close()
}
@Vibhu2
Copy link

Vibhu2 commented Jun 26, 2020

please make it a function or make it a module add some examples it makes work a lot more easier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment