从REST API下载文件并上传到Sharepoint,无需中间存储。

huangapple go评论56阅读模式
英文:

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

问题

这是你的内容的翻译:

我在这里的第一篇帖子,因为我是新手脚本/编程。

我有一个要求,需要通过REST API从外部服务下载文件,然后将这些文件上传到SharePoint。我已经编写了一个可以完成此操作的脚本,但它使用了中间本地存储,因为脚本使用两个不同的函数来实现此目的(使用 **Invoke-RestMethod** 本地下载,然后使用 **Connect-PnPOnline** 和 **Add-PNPFile** 从本地上传到SharePoint)

我想要做的是,在不使用本地存储的情况下实现这一点,因为我想将其移到无服务器Azure函数中。

以下是我的脚本。欢迎提供有关如何简化以实现去除本地存储的任何反馈。

请注意,这仍然是一个原型,所以一旦我完成基本功能,我还将外部化所有凭据信息。

```plaintext
# 用于连接API并下载录音的脚本

# 定义连接到API的链接
$download_endpoint = 'https://api.externalservice.com/download';

# 定义下载文件
$recording = 'C:\Users\UserName\filename.mp3';

# 定义用于连接到API的安全凭据
$api_key = 'pk_xxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$api_secret = 'sk_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

# 连接到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

# 设置用于SharePoint网站和库的变量
$tenant = "companyname.onmicrosoft.com";
$siteURL = "https://companyname.sharepoint.com/site";
$libraryName = "Test";

# 定义用于连接到SharePoint的凭据
$applicationID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$certpath = "C:\Users\UserName\companyname.pfx";
$password = "XXXXXXXXXXXXXX";

# 使用现代身份验证连接到SharePoint Online
Connect-PnPOnline -Url $siteURL -ClientId $applicationID -CertificatePath $certpath -CertificatePassword (ConvertTo-SecureString -AsPlainText $password -Force) -Tenant $tenant

# 将文件上传到库
Add-PnPFile -Path $recording -Folder $libraryName

# 从SharePoint Online断开连接
Disconnect-PnPOnline
英文:

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

答案1

得分: 1

Add-PnpFile有一个-Stream参数,您可以使用它来替代-Path。在这种情况下,您可以将Invoke-RestMethod更改为Invoke-WebRequest,并使用返回的对象的.RawContentStream Property中包含的MemoryStream。代码保持基本相同,只需要进行以下修改:

$req = Invoke-WebRequest -Uri $download_endpoint -Headers $headers -UseBasicParsing

# 在此连接,保持不变
# 然后上传使用`-Stream`
# 此外,在这个ParameterSet中,`-FileName`是必需的

Add-PnPFile -Stream $req.RawContentStream -Folder $libraryName -FileName somefile.ext
英文:

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

huangapple
  • 本文由 发表于 2023年3月7日 12:01:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75657936.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定