powershellsharepointinvoke-restmethod

Download a file from REST API and upload to Sharepoint without need for intermediate storage


My first post here, as I am newb to scripting/programming.

I have a requirement to download files from an external service via a REST API and then upload those files to SharePoint. I have put together a script that can do this, but it uses intermediate local storage, as the script uses two different functions to achieve this (download locally using Invoke-RestMethod and upload from local to SharePoint using Connect-PnPOnline and Add-PNPFile

What I would like to do, is achieve this without using local storage, as I would like to move this to a serverless Azure Function.

Below is my script. Any feedback on streamlining this to achieve the removal of local storage are appreciated.

Just a note, this is still a prototype, so I will also externalise all the credential information once I have the base functionality working.

# Script to connect to API and download recordings

# Define links to the  API
$download_endpoint = 'https://api.externalservice.com/download'

# Define the download file
$recording = 'C:\Users\UserName\filename.mp3'

# Define security credentials for connecting to the API
$api_key = 'pk_xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
$api_secret = 'sk_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

# Connect to the API
$auth = $api_key + ':' + $api_secret
$Encoded = [System.Text.Encoding]::UTF8.GetBytes($auth)
$authorizationInfo = [System.Convert]::ToBase64String($Encoded)
$headers = @{"Authorization"="Basic $($authorizationInfo)"}

Invoke-RestMethod -Uri $download_endpoint -OutFile $recording -Headers $headers

# Set variables for the SharePoint site and library
$tenant = "companyname.onmicrosoft.com"
$siteURL = "https://companyname.sharepoint.com/site"
$libraryName = "Test"

# Define credentials for connecting to SharePoint
$applicationID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
$certpath = "C:\Users\UserName\companyname.pfx"
$password = "XXXXXXXXXXXXXX"

# Connect to SharePoint Online using modern authentication
Connect-PnPOnline -Url $siteURL -ClientId $applicationID -CertificatePath $certpath -CertificatePassword (ConvertTo-SecureString -AsPlainText $password -Force) -Tenant $tenant

# Upload the file to the library
Add-PnPFile -Path $recording -Folder $libraryName

# Disconnect from SharePoint Online
Disconnect-PnPOnline

Solution

  • Add-PnpFile has a -Stream parameter which you can use instead of -Path. In that sense, you could change Invoke-RestMethod for Invoke-WebRequest and use the MemoryStream included in the .RawContentStream Property of the returned object. The code remains about the same and only requires these modifications:

    $req = Invoke-WebRequest -Uri $download_endpoint -Headers $headers -UseBasicParsing
    
    # Connect here, stays the same
    # Then upload uses `-Stream`
    # Also, in this ParameterSet `-FileName` is Mandatory
    
    Add-PnPFile -Stream $req.RawContentStream -Folder $libraryName -FileName somefile.ext