1

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
Jaydeep Chaudhari
  • 420
  • 1
  • 9
  • 21

2 Answers2

2

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...

Alfageme
  • 2,075
  • 1
  • 23
  • 30
  • Upload and create a public share part working fine, however it doesn't print public link URL. – Jaydeep Chaudhari Oct 17 '17 at 11:17
  • @JaydeepChaudhari woop, typo; sorry. Try again with `$response.ocs.data.url` – Alfageme Oct 17 '17 at 12:01
  • Just for Information to above solution work, you need to Allow option for `Allow Public Uploads` and Disable option for `Enforce Password Protection` from owncloud - settings - sharing menu. – Jaydeep Chaudhari Apr 09 '18 at 05:33
  • Passing the credentials for the second Invoke-RestMethod call did not work for me, I got error code 997 (unauthorized). I needed to set the authentication header manually as described at https://stackoverflow.com/q/27951561/5525302. – MarkusParker Jan 03 '20 at 20:02
0

A small sidenote. If you use Nextcloud instead of OwnCloud then this works exactly the same but you have a few more options when creating the sharing links. The documentation is here: https://docs.nextcloud.com/server/12/developer_manual/core/ocs-share-api.html Nextcloud is fully open source and faster then Owncloud.

Darwiche
  • 39
  • 1