I have looked at previous posts on this topic and still have not managed to be able to upload a large file to SharePoint using the Resumable Upload. I have some specific areas where I have questions. My current code is:
$fileName = "myfile.jpg"
$createUploadSessionUri = "https://graph.microsoft.com/v1.0/sites/$($site.id)/drive/items/$($drive.id):/$($fileName):/createUploadSession"
$uploadSession = Invoke-MgGraphRequest -Method POST -Uri $createUploadSessionUri -Body $body -ContentType 'application/json'
Write-Host $uploadSession.uploadUrl #correctly creates upload session
Write-host $uploadSession.expirationDateTime
#### Send bytes to the upload session
$path = "myfile.jpg"
$fileInBytes = [System.IO.File]::ReadAllBytes($path)
$fileLength = $fileInBytes.Length
write-host "fileinbytes length=$fileLength" # correct length of file 4653349
## Split the file up
$PartSizeBytes = 320 * 1024 # 327680
$index = 0
$start = 0 #start at 0
$end = 0
while ($fileLength -gt $end) {
$start = $index * $PartSizeBytes # first iteration = 0
$end = $start + $PartSizeBytes - 1 # first iteration = 327679
$body = $fileInBytes[$start..$end]
write-host $body.Length.ToString()
$headers = @{
'Content-Length' = $body.Length.ToString()
'Content-Range' = "bytes $start-$end/$fileLength"
}
write-Host "bytes $start-$end/$fileLength" # correct content range "bytes 0-327679/4653349"
write-host $body # correct number of bytes 327679
$response = Invoke-RestMethod -Method 'Put' -Uri $uploadSession.uploadUrl -Body $body -Headers $headers -SkipHeaderValidation # <<<<<fail
$index++
}
So, everything seems to be correct according to documentation, except it errors out on the rest call to upload the chunks.
Bad Request Bad Request - Invalid Content Length HTTP
| Error 400. There is an invalid content length or chunk length
| in the request.
Questions are:
- Do I have to pass in a Content-Type to the upload call?
- Am I sending the correct body (255 216...)?
- Are there any other headers I'm missing, OR that I should not be including
- Is the PartSizeBytes variable appropriate?
- Am I properly chunking the file?