ziqx-drive-sdk
SKILL.md
@ziqx/drive Skill
This skill provides comprehensive instructions on how to use the @ziqx/drive package for managing file uploads to Ziqx Drive. It handles both server-side signed URL generation and client-side direct uploads.
Overview
@ziqx/drive is split into two main components:
ZDrive(Server-side): Used to securely generate signed upload URLs using your Drive credentials.ZDriveClient(Client-side): Used in the browser to upload files directly to the signed URLs and perform image processing (like resizing) before upload.
Installation
npm install @ziqx/drive
Technical Implementation
1. Server-side: Generating Signed URLs
To allow a client to upload a file directly to Ziqx Drive, you must first generate a signed URL on your server. This prevents exposing your ZDRIVE_SECRET to the client.
API Reference: ZDrive
constructor(driveKey: string, driveSecret: string)generatePutUrl(fileName: string): Promise<SignUrlResponse>
Logic Flow
- Initialize
ZDrivewith your credentials. - Call
generatePutUrlwith the intended filename. - Return the
urlto the client.
2. Client-side: Uploading Files
The client uses the signed URL to perform a multipart/form-data POST request directly to Ziqx Drive.
API Reference: ZDriveClient
uploadFile(uploadUrl: string, file: File): Promise<UploadResponse>resizeImage(file: File, maxWidth?: number, quality?: number): Promise<File>(Browser only)
Logic Flow
- Obtain the signed URL from your server.
- (Optional) Resize the image using
ZDriveClient.resizeImage. - Call
uploadFilewith the signed URL and theFileobject.
Next.js Implementation Guide
This is the recommended pattern for Next.js applications using Server Actions.
A. Server Action (actions/drive.ts)
Create a server action to securely generate the signed URL.
"use server";
import { ENV } from "@/constants/envs";
import { ZDrive } from "@ziqx/drive";
/**
* Generates a signed upload URL for Ziqx Drive.
* This should ALWAYS be called from the server to protect credentials.
*/
export async function generateSignedUrl(fileName: string) {
// Initialize with drive credentials from environment variables
const drive = new ZDrive(ENV.ZDRIVE_KEY!, ENV.ZDRIVE_SECRET!);
const signed = await drive.generatePutUrl(fileName);
if (signed.success && signed.url) {
return signed.url;
} else {
console.error("❌ Error generating URL:", signed.message);
return null;
}
}
B. Client Component Usage
Use ZDriveClient in your frontend components to handle the actual upload process.
"use client";
import { ZDriveClient } from "@ziqx/drive";
import { generateSignedUrl } from "@/actions/drive";
// ... inside your component
const handleUpload = async (options: any) => {
const { file, onSuccess, onError } = options;
const client = new ZDriveClient();
try {
// 1. Get signed URL from the server action
const signedUrl = await generateSignedUrl(file.name);
if (!signedUrl) throw new Error("Failed to get signed URL");
// 2. Upload to Ziqx Drive directly from the browser
const uploadRes = await client.uploadFile(signedUrl, file);
if (!uploadRes.success || !uploadRes.filename) {
throw new Error("Upload to ZDrive failed");
}
// 3. Handle success
onSuccess(uploadRes.filename);
} catch (err) {
console.error("Upload Error:", err);
onError(new Error("Upload failed"));
}
};
Best Practices for AI Agents
- Always use Environment Variables: Never hardcode
driveKeyordriveSecret. Ensure they are stored in.envand accessed viaprocess.envor a config constants file. - Server vs Client separation: Remind the developer that
ZDriveis for server-side (Node.js/Server Actions/API Routes) andZDriveClientis for client-side (Browser).ZDriveClientuses browser-nativeFormDataandfetch. - File Naming: When generating a signed URL, ensure the filename passed to
generatePutUrlmatches or is appropriate for the file being uploaded. - Error Handling: Always check for
signed.successon the server anduploadRes.successon the client. - Image Optimization: If the user is uploading images, suggest using
client.resizeImage(file, 1024, 0.8)before callinguploadFileto save bandwidth and storage.
Examples
Simple Client-side Upload Logic
const signedUrl = await generateSignedUrl(file.name);
if (!signedUrl) throw new Error("Failed to get signed URL");
// Upload to Object Storage
const client = new ZDriveClient();
const uploadRes = await client.uploadFile(signedUrl, file);
if (!uploadRes.success || !uploadRes.filename)
throw new Error("Upload to ZDrive failed");
console.log("Uploaded filename:", uploadRes.filename);
Weekly Installs
3
Repository
ziqx/ziqx-ai-skillsFirst Seen
5 days ago
Security Audits
Installed on
amp3
cline3
opencode3
cursor3
kimi-cli3
codex3