NextCloud Python上传文件和分享链接

上传文件

import requests

def upload_file(file_path, upload_url, username, password):
    # Read the file data
    with open(file_path, 'rb') as file:
        file_data = file.read()

    # Set the headers and authentication
    headers = {
        'OCS-APIRequest': 'true',
    }
    auth = (username, password)

    # Upload the file
    response = requests.post(upload_url, headers=headers, auth=auth, data=file_data)

    # Check the response status
    if response.status_code == 200:
        print("File uploaded successfully.")
    else:
        print("File upload failed. Status code:", response.status_code)

# Example usage
file_path = '/path/to/file.txt'
upload_url = 'https://your-nextcloud-instance/remote.php/dav/files/USERNAME/destination_folder/file.txt'
username = 'your_username'
password = 'your_password'

upload_file(file_path, upload_url, username, password)

分享链接

import requests
import json

def generate_download_link(file_path, share_url, username, password):
    # Set the headers and authentication
    headers = {
        'OCS-APIRequest': 'true',
        'Content-Type': 'application/json',
    }
    auth = (username, password)

    # Share the file
    share_data = {
        'path': file_path,
        'shareType': 3,  # Share with public link
        'permissions': 1,  # Read-only permissions
    }
    response = requests.post(share_url, headers=headers, auth=auth, data=json.dumps(share_data))

    # Check the response status
    if response.status_code == 200:
        share_response = response.json()
        download_link = share_response['ocs']['data']['url']
        print("Download link:", download_link)
    else:
        print("Sharing file failed. Status code:", response.status_code)

# Example usage
file_path = '/path/to/file.txt'
share_url = 'https://your-nextcloud-instance/ocs/v2.php/apps/files_sharing/api/v1/shares'
username = 'your_username'
password = 'your_password'

generate_download_link(file_path, share_url, username, password)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值