Overview
What you will learn
This tutorial walks you through the steps and examples to help organization developers quickly upload, download, and preview files in Drive.Learning objectives
Help developers quickly build their own app and integrate Drive file upload, preview, and download capabilities by calling DingTalk basic APIs.Target audience
All DingTalk developers.Prerequisites
- You have obtained developer permissions.
- You have installed an IDE or other development tools.
- You have created a folder named “Test Folder” in Drive for testing purposes only.
- You have installed Node.js and completed the environment configuration.
- You have installed Maven and completed the environment configuration.
- You have installed JDK and completed the environment configuration.
Development process
- Use silent login to obtain the name and role of the currently signed-in user. For sign-in, refer to silent login for web apps (H5 micro apps).
-
Upload files to Drive.
- Call the File Search API to search for the folder named “Test Folder” in Drive and return the
dentryUuidof the folder.
- Call the File Search API to search for the folder named “Test Folder” in Drive and return the
Note
Before calling this API, make sure there is only one folder named “Test Folder” in Drive. Otherwise, an error may occur. 2. Call the Get File Upload Information API with thedentryUuid to obtain the file upload information, including the resourceUrls and headers parameter values.
3. Upload the file using the OSS header sign request method. For details, refer to step 2 in the Get File Upload Information API.
4. Call the Submit File API to submit the file to Drive and obtain the returned spaceId, parentId, path, and uuid parameter values.
3. Preview files in Drive.
-
Use the
uuidobtained from the last submission during file upload to construct a URL and open the file for preview. -
Download files from Drive.
-
Call the Get Space List API to obtain Drive space information, including the
spaceId. -
Call the Get File or Folder List API with the
spaceIdto obtain thefileIdand file namename. -
Call the Get File Download Information API with the
fileIdandnameto obtain the file download information, including theinternalResourceUrlsandheadersparameter values. - Use the information above to perform the download. For the download code, refer to step 2 of Get File Download Information. Note
-
Call the Get Space List API to obtain Drive space information, including the
Note
The file path is the current project path plus the file name. Therefore, after the file is downloaded, it is automatically saved to the project directory. Check it accordingly.Step 1: Create an app
- Sign in to the Developer Console.
- Click App Development > Internal App > DingTalk App > Create App.
-
Enter app information.
Item Required Description App name Yes Enter the app name. The minimum length is 2 characters. App description Yes Briefly describe the product or service provided by the app. The minimum length is 4 characters. App icon No Upload the app icon. The icon must be in JPG/PNG format, at least 240 px * 240 px, with a 1:1 aspect ratio, no rounded corners, and no larger than 2 MB. - Click Save to enter the app details page. After creation, click Basic Information > Credentials and Basic Information to view the Client ID, Client Secret, and Agent ID of the app.
Step 2: Configure the web app
- On the app details page, click App Capabilities > Add App Capability.
- Select Add web app.
-
Configure the web app (H5) information:
Item Description App home page URL Enter the app home page URL. This example uses http://localhost:5173?corpid=$CORPID$for subsequent testing. This example is for local testing only. imagePC home page URL - After configuration, click Save.
Step 3: Add API permissions
For formal testing, add the following permissions:- File Search permission:
Storage.Dentry.Search - Organization storage file upload information read permission:
Storage.UploadInfo.Read - Organization storage file write permission:
Storage.File.Write - Drive app space read permission:
Drive.Space.Read - Organization storage file read permission:
Storage.File.Read - Organization storage file download information read permission:
Storage.DownloadInfo.Read
Step 4: Release the app
- After app configuration, you need to release the app. On the app details page, click App Release > Version Management and Release.
- Click Create New Version to enter the version details page.
-
Configure version information:
After configuration, click Save at the bottom.
Item Description App version number Enter the app version number. Use the default version. Version description Enter the version description. Customize as needed. Content to release App capabilities displayed on the workbench: - Select web app -
On the success dialog page, click Publish directly.
If you are not an Organization Admin, releasing the app requires approval from an Organization Admin. Releasing for visibility to yourself only does not require admin approval.
Step 5: Build the service
- Make sure you have completed the steps above and obtained the parameter information required to run the demo below.
- Download the file-demo.zip demo.
-
Open the IDE and import the downloaded demo.
The sample code is divided into backend (backend code directory) and frontend (frontend code directory).
-
Open the backend code directory. In the resources directory, edit the
application.propertiesfile and enter theclientId (app Client ID)andclientSecret (app Client Secret)parameters. -
Click to start the backend service.
Note
- Before starting the backend service, make sure Maven and JDK are installed correctly and the relevant environment is configured. If this is your first time installing the IDE, modify the relevant configuration files in the IDE.
- Make sure ports 5173 and 8080 are not in use.
-
Open the frontend code directory, edit
vite.config.ts, and enter the correctclientId (app Client ID). - Click the frontend project file, right-click, and select Terminal to open it.
-
In the terminal window, run the following commands:
-
npm install -
npm run devNote: On Windows, use
npm run dev:rawto start.
-
- The frontend and backend services have now started successfully.
Step 6: Test the app
- Sign in to the DingTalk Client-side and select the organization where the app is located.
- Click Workbench > Add, search for the organization app you created above, and add it.
- Open the app and click Select Local File to upload a local file to Drive. After selecting the file, click Submit. On the file information confirmation page, you can see the user ID, File Name, and File Size.
- After successful upload, the result is shown below:
Note
You can click the link to open the file directly, or click the Open File button to view the file. After successful upload, you can view the uploaded file in the “Test Folder” of Drive.- Click the file download button to download the file.
Important
If the error message “auth level of org is not enough” appears during file download, it indicates that the current organization authentication level is too low and needs to be upgraded before use. For details, refer to the error code description in the Get File Download Information API. After successful download, you can view the downloaded file in the project directory.- You have now completed all operations for uploading, previewing, and downloading files in Drive.