如何使用Zoho Deluge将PDF上传至共享存储资源
用Zoho Deluge实现PDF跨网盘上传并生成公开链接方案
第一步:获取Zoho Books发票PDF二进制内容
先通过Zoho Books API获取发票PDF的二进制数据,使用invokeUrl函数:
// 替换为你的Zoho Books组织ID和目标发票ID orgId = "YOUR_ZOHO_BOOKS_ORG_ID"; invoiceId = "TARGET_INVOICE_ID"; // 构造API请求URL pdfUrl = "https://books.zoho.com/api/v3/invoices/" + invoiceId + "/pdf?organization_id=" + orgId; // 发送GET请求获取二进制PDF内容 response = invokeUrl [ url: pdfUrl type: GET connection: "zoho_books_connection" // 提前在Zoho环境中创建的Books连接 output: "binary" ]; // 检查请求是否成功 if(response.getStatusCode() != 200) { info "获取PDF失败: " + response.getResponseText(); return; } pdfBinary = response.getBinaryContent(); pdfFileName = "Invoice_" + invoiceId + ".pdf";
方案1:上传至Zoho WorkDrive(最优简便,同生态免额外授权)
Zoho WorkDrive与Zoho生态集成度最高,直接用内置zoho.workdrive类操作,无需额外OAuth配置:
// 1. 上传PDF到指定WorkDrive文件夹(替换为你的文件夹ID) folderId = "YOUR_WORKDRIVE_FOLDER_ID"; uploadResponse = zoho.workdrive.uploadFile(folderId, pdfFileName, pdfBinary); if(uploadResponse.get("status") != "success") { info "WorkDrive上传失败: " + uploadResponse.get("message"); return; } fileId = uploadResponse.get("data").get("id"); // 2. 设置文件为公开可下载权限 shareResponse = zoho.workdrive.updateShareSettings(fileId, "public", "download"); if(shareResponse.get("status") != "success") { info "设置公开权限失败: " + shareResponse.get("message"); return; } // 3. 获取公开下载链接 publicLink = shareResponse.get("data").get("link"); info "WorkDrive公开下载链接: " + publicLink;
方案2:上传至谷歌网盘
需提前在Zoho环境中创建谷歌网盘的OAuth连接,再通过invokeUrl调用Drive API:
// 1. 构造上传元数据与请求体 metadata = { "name": pdfFileName, "mimeType": "application/pdf" }; boundary = "----DelugeBoundary" + random(); body = "--" + boundary + "\r\n"; body += "Content-Disposition: form-data; name='metadata'\r\n"; body += "Content-Type: application/json\r\n\r\n"; body += metadata.toJsonString() + "\r\n"; body += "--" + boundary + "\r\n"; body += "Content-Disposition: form-data; name='file'; filename='" + pdfFileName + "'\r\n"; body += "Content-Type: application/pdf\r\n\r\n"; body += pdfBinary + "\r\n"; body += "--" + boundary + "--"; // 2. 发送上传请求 uploadResponse = invokeUrl [ url: "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart" type: POST connection: "google_drive_connection" // 提前创建的谷歌Drive连接 headers: {"Content-Type": "multipart/form-data; boundary=" + boundary} content: body ]; if(uploadResponse.getStatusCode() != 200) { info "谷歌网盘上传失败: " + uploadResponse.getResponseText(); return; } fileId = uploadResponse.getJSONObject().get("id"); // 3. 设置文件为公开可读权限 permissionResponse = invokeUrl [ url: "https://www.googleapis.com/drive/v3/files/" + fileId + "/permissions" type: POST connection: "google_drive_connection" headers: {"Content-Type": "application/json"} content: {"role": "reader", "type": "anyone"}.toJsonString() ]; if(permissionResponse.getStatusCode() != 200) { info "设置谷歌网盘权限失败: " + permissionResponse.getResponseText(); return; } // 4. 生成直接下载链接 publicLink = "https://drive.google.com/uc?export=download&id=" + fileId; info "谷歌网盘公开下载链接: " + publicLink;
方案3:上传至Dropbox
需提前在Zoho环境中创建Dropbox的OAuth连接,调用Dropbox API完成操作:
// 1. 构造Dropbox目标路径(替换为你的存储路径) dropboxPath = "/Invoices/" + pdfFileName; // 2. 上传PDF文件 uploadResponse = invokeUrl [ url: "https://content.dropboxapi.com/2/files/upload" type: POST connection: "dropbox_connection" // 提前创建的Dropbox连接 headers: { "Content-Type": "application/octet-stream", "Dropbox-API-Arg": {"path": dropboxPath, "mode": "add"}.toJsonString() } content: pdfBinary ]; if(uploadResponse.getStatusCode() != 200) { info "Dropbox上传失败: " + uploadResponse.getResponseText(); return; } // 3. 创建公开共享链接 shareResponse = invokeUrl [ url: "https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings" type: POST connection: "dropbox_connection" headers: {"Content-Type": "application/json"} content: { "path": dropboxPath, "settings": {"requested_visibility": "public"} }.toJsonString() ]; if(shareResponse.getStatusCode() != 200) { info "创建Dropbox共享链接失败: " + shareResponse.getResponseText(); return; } // 转换为直接下载链接 publicLink = shareResponse.getJSONObject().get("url"); directDownloadLink = publicLink.replace("dl=0", "dl=1"); info "Dropbox公开下载链接: " + directDownloadLink;
关键注意事项
- 谷歌网盘、Dropbox的连接需在Zoho Creator/Flow的连接管理中提前创建,完成OAuth授权流程。
- Zoho WorkDrive操作无需额外授权,只要当前Deluge运行环境拥有WorkDrive权限即可。
- 获取PDF时必须将
invokeUrl的output参数设为binary,否则会得到乱码文本。
内容的提问来源于stack exchange,提问作者Simak
相关产品推荐
相关产品推荐

