I created a powershell script to upload file on owncloud. Upload works fine but How do I get public Link for that uploaded file.
Below is script
$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
I need to print link from the code like
http://myowncloud:8085/index.php/s/aTQr8JNxEYCw1Vz
Note that ownCloud does not create a public link for the files you upload unless you explicitly do so. In order to do this, you need to use the ocs Share API. In the docs, you'll find that for public link share (i.e. shareType = 3
) you have to perform a separate POST
request with the path
of the file itself.
I've slightly adapted your code to make it work with newer instances of ownCloud (version 9 et seq. come with a different WebDAV endpoint) and also to allow better URL compositions:
# 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
Also take into account that this only covers the happy path and your script would be more correct & complete if you check for HTTP statuses on each request, the bodies of the replies...