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

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

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

问题

这是你的内容的翻译:

  1. 我在这里的第一篇帖子,因为我是新手脚本/编程。
  2. 我有一个要求,需要通过REST API从外部服务下载文件,然后将这些文件上传到SharePoint。我已经编写了一个可以完成此操作的脚本,但它使用了中间本地存储,因为脚本使用两个不同的函数来实现此目的(使用 **Invoke-RestMethod** 本地下载,然后使用 **Connect-PnPOnline** **Add-PNPFile** 从本地上传到SharePoint
  3. 我想要做的是,在不使用本地存储的情况下实现这一点,因为我想将其移到无服务器Azure函数中。
  4. 以下是我的脚本。欢迎提供有关如何简化以实现去除本地存储的任何反馈。
  5. 请注意,这仍然是一个原型,所以一旦我完成基本功能,我还将外部化所有凭据信息。
  6. ```plaintext
  7. # 用于连接API并下载录音的脚本
  8. # 定义连接到API的链接
  9. $download_endpoint = 'https://api.externalservice.com/download';
  10. # 定义下载文件
  11. $recording = 'C:\Users\UserName\filename.mp3';
  12. # 定义用于连接到API的安全凭据
  13. $api_key = 'pk_xxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
  14. $api_secret = 'sk_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
  15. # 连接到API
  16. $auth = $api_key + ':' + $api_secret
  17. $Encoded = [System.Text.Encoding]::UTF8.GetBytes($auth)
  18. $authorizationInfo = [System.Convert]::ToBase64String($Encoded)
  19. $headers = @{@"Authorization"="Basic $($authorizationInfo)"}
  20. Invoke-RestMethod -Uri $download_endpoint -OutFile $recording -Headers $headers
  21. # 设置用于SharePoint网站和库的变量
  22. $tenant = "companyname.onmicrosoft.com";
  23. $siteURL = "https://companyname.sharepoint.com/site";
  24. $libraryName = "Test";
  25. # 定义用于连接到SharePoint的凭据
  26. $applicationID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
  27. $certpath = "C:\Users\UserName\companyname.pfx";
  28. $password = "XXXXXXXXXXXXXX";
  29. # 使用现代身份验证连接到SharePoint Online
  30. Connect-PnPOnline -Url $siteURL -ClientId $applicationID -CertificatePath $certpath -CertificatePassword (ConvertTo-SecureString -AsPlainText $password -Force) -Tenant $tenant
  31. # 将文件上传到库
  32. Add-PnPFile -Path $recording -Folder $libraryName
  33. # 从SharePoint Online断开连接
  34. 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.

  1. # Script to connect to API and download recordings
  2. # Define links to the API
  3. $download_endpoint = 'https://api.externalservice.com/download'
  4. # Define the download file
  5. $recording = 'C:\Users\UserName\filename.mp3'
  6. # Define security credentials for connecting to the API
  7. $api_key = 'pk_xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
  8. $api_secret = 'sk_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
  9. # Connect to the API
  10. $auth = $api_key + ':' + $api_secret
  11. $Encoded = [System.Text.Encoding]::UTF8.GetBytes($auth)
  12. $authorizationInfo = [System.Convert]::ToBase64String($Encoded)
  13. $headers = @{"Authorization"="Basic $($authorizationInfo)"}
  14. Invoke-RestMethod -Uri $download_endpoint -OutFile $recording -Headers $headers
  15. # Set variables for the SharePoint site and library
  16. $tenant = "companyname.onmicrosoft.com"
  17. $siteURL = "https://companyname.sharepoint.com/site"
  18. $libraryName = "Test"
  19. # Define credentials for connecting to SharePoint
  20. $applicationID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  21. $certpath = "C:\Users\UserName\companyname.pfx"
  22. $password = "XXXXXXXXXXXXXX"
  23. # Connect to SharePoint Online using modern authentication
  24. Connect-PnPOnline -Url $siteURL -ClientId $applicationID -CertificatePath $certpath -CertificatePassword (ConvertTo-SecureString -AsPlainText $password -Force) -Tenant $tenant
  25. # Upload the file to the library
  26. Add-PnPFile -Path $recording -Folder $libraryName
  27. # Disconnect from SharePoint Online
  28. Disconnect-PnPOnline

答案1

得分: 1

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

  1. $req = Invoke-WebRequest -Uri $download_endpoint -Headers $headers -UseBasicParsing
  2. # 在此连接,保持不变
  3. # 然后上传使用`-Stream`
  4. # 此外,在这个ParameterSet中,`-FileName`是必需的
  5. 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:

  1. $req = Invoke-WebRequest -Uri $download_endpoint -Headers $headers -UseBasicParsing
  2. # Connect here, stays the same
  3. # Then upload uses `-Stream`
  4. # Also, in this ParameterSet `-FileName` is Mandatory
  5. 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:

确定