Using the ownCloud Python package I have written a small script (see below) to upload files via a bash alias. While the desktop clients are great for quickly sharing files I often find using this script more efficient and it also works in headless environments.
With Nextcloud 15+ the ability to āHide Download Linkā was added via the files interface. Iām wondering if this feature is accessible from the Python package. In the same vein, Iām wondering if thereās a function to set expiration.
Iām fairly new to writing and reading Python and also know that the class was originally for ownCloud and hasnāt been updated in some time. If thereās another Python package for Nextcloud or WebDAV that I should be using please point me in that direction.
Ideally, the goal is to be able to run it like this:
python 3 nextcloud.py Some/File/image.jpg true 86400
which would upload the file, hide the download link, then set the expiration to one day.
Looking forward to the communities thoughts.
import sys
from pathlib import Path
domain = 'https://example.com/'
user = 'python'
password = '12345abcxyz'
remoteDirectory = 'public-uploads'
fullFilePath = str( ' '.join( sys.argv[1:] ) )
try:
pathParts = fullFilePath.rsplit( '/', 1 )
localName = pathParts[1]
except IndexError:
localName = fullFilePath
fileName = remoteDirectory + '/' + localName.replace(" ", "_")
if not Path( fullFilePath ).exists() :
print ( "File not found, check the following: \n1) Did you include the absolute path to file? \n2) Did you use quotes around the filename?" )
sys.exit (1)
oc = owncloud.Client( domain )
oc.login( user, password )
oc.put_file( fileName, fullFilePath )
linkData = oc.share_file_with_link( fileName )
print ('Public link: %s' % ( linkData.get_link() ) )