通过vps命令行向dropbox上传文件
vps空间比较小,分段下载大文件
# Part 1: 0 to 1GB-1
curl -r 0-1073741823 -o part1 https://example.com/yourfile.iso# Part 2: 1GB to 2GB-1
curl -r 1073741824-2147483647 -o /tmp/part1 https://dn721502.ca.archive.org/0/items/windows-1809/Windows%201809.iso -k# Part 3: 2GB to 3GB-1
curl -r 2147483648-3221225471 -o /tmp/part1 https://dn721502.ca.archive.org/0/items/windows-1809/Windows%201809.iso -k# Part 4: 3GB to end (3.8GB)
curl -r 3221225472- -o /tmp/part1 https://dn721502.ca.archive.org/0/items/windows-1809/Windows%201809.iso -k
import os
import requests
import jsonACCESS_TOKEN = 'token'
LOCAL_FILE = '/tmp/part1' # e.g. '/tmp/part1'
DROPBOX_PATH = '/filename.ext' # final path in Dropbox (with filename)
CHUNK_SIZE = 150 * 1024 * 1024 # 150 MBdef upload_large_file():file_size = os.path.getsize(LOCAL_FILE)print(f'[INFO] File size: {file_size} bytes')with open(LOCAL_FILE, 'rb') as f:# Step 1: start upload sessiontry:chunk = f.read(CHUNK_SIZE)print(f'[INFO] Starting upload session with first chunk size: {len(chunk)} bytes')headers = {"Authorization": f"Bearer {ACCESS_TOKEN}","Content-Type": "application/octet-stream",}url = "https://content.dropboxapi.com/2/files/upload_session/start"response = requests.post(url, headers=headers, data=chunk)response.raise_for_status()session_id = response.json()['session_id']print(f'[SUCCESS] Upload session started. Session ID: {session_id}')except requests.exceptions.RequestException as e:print(f'[ERROR] Failed to start upload session: {e}')print(f'[DEBUG] Response content: {getattr(e.response, "content", None)}')returnoffset = len(chunk)# Step 2: append chunkswhile offset < file_size:try:chunk = f.read(CHUNK_SIZE)if not chunk:print('[INFO] No more data to read for append.')breakprint(f'[INFO] Appending chunk at offset {offset}, size: {len(chunk)} bytes')headers = {"Authorization": f"Bearer {ACCESS_TOKEN}","Content-Type": "application/octet-stream","Dropbox-API-Arg": json.dumps({"cursor": {"session_id": session_id,"offset": offset},"close": False})}url = "https://content.dropboxapi.com/2/files/upload_session/append_v2"response = requests.post(url, headers=headers, data=chunk)response.raise_for_status()print(f'[SUCCESS] Chunk appended at offset {offset}')offset += len(chunk)except requests.exceptions.RequestException as e:print(f'[ERROR] Failed to append chunk at offset {offset}: {e}')print(f'[DEBUG] Response content: {getattr(e.response, "content", None)}')return# Step 3: finish upload sessiontry:print(f'[INFO] Finishing upload session at offset {offset}')headers = {"Authorization": f"Bearer {ACCESS_TOKEN}","Content-Type": "application/octet-stream","Dropbox-API-Arg": json.dumps({"cursor": {"session_id": session_id,"offset": offset},"commit": {"path": DROPBOX_PATH,"mode": "add","autorename": True,"mute": False}})}url = "https://content.dropboxapi.com/2/files/upload_session/finish"response = requests.post(url, headers=headers, data=b'')response.raise_for_status()print(f'[SUCCESS] Upload finished. File saved to {DROPBOX_PATH}')print('[INFO] Dropbox API response:')print(response.json())except requests.exceptions.RequestException as e:print(f'[ERROR] Failed to finish upload session: {e}')print(f'[DEBUG] Response content: {getattr(e.response, "content", None)}')if __name__ == '__main__':upload_large_file()
将/tmp/part1上传至/filename.ext