我创建了一个powershell脚本来在owncloud上传文件。上传工作正常,但如何获取该上传文件的公共链接。
下面是脚本
$user = "admin"
$pass= "admin"
$secpasswd = ConvertTo-SecureString $pass -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($user, $secpasswd)
$body = "file=$(get-content c:\myupload.zip -raw)"
$targetname = "myupload.zip"
$oc = "http://myowncloud:8085/remote.php/webdav/NOC/"
Invoke-RestMethod -uri $oc$targetname -method Put -body $body -Credential $credential
我需要打印代码中的链接,如
http://myowncloud:8085/index.php/s/aTQr8JNxEYCw1Vz
答案 0 :(得分:1)
请注意,除非您明确这样做,否则ownCloud不会为您上传的文件创建公共链接。为此,您需要使用ocs Share API。在文档中,您会发现对于公共链接共享(即shareType = 3
),您必须使用文件本身的POST
执行单独的path
请求。
我稍微调整了您的代码,使其适用于较新的ownCloud实例(版本9及其后续版本come with a different WebDAV endpoint),并允许更好的网址组合:
# Upload the file
$body = $(get-content c:\test.txt -raw)
$targetname = "test.txt"
$oc = "http://demo.owncloud.com/"
$dav_endpoint = "remote.php/dav/files/admin/"
Invoke-RestMethod -Uri $oc$dav_endpoint$targetname -Method Put -Body $body -Credential $credential
# Create a public share for that file:
$headers = @{"Ocs-APIREQUEST"="true"}
$sharing_api = "ocs/v1.php/apps/files_sharing/api/v1/shares?format=json"
# Required parameters to create the share:
$body = @{
path = "/$($targetname)"
shareType = "3"
}
$response = Invoke-RestMethod -Uri $oc$sharing_api -Method Post -Headers $headers -Body $body -Credential $credential
# Print the public link URL:
echo $response.ocs.data.url
另外考虑到这只涵盖快乐路径,您的脚本会更正确&如果您检查每个请求的HTTP状态,请回复完成...
答案 1 :(得分:0)
一个小旁注。如果您使用Nextcloud而不是OwnCloud,那么这种方法完全相同,但在创建共享链接时您还有一些选项。文档在这里:https://docs.nextcloud.com/server/12/developer_manual/core/ocs-share-api.html Nextcloud是完全开源的,比Owncloud更快。