• Node.js 操作百度网盘实现文件上传(小文件上传,大文件分片上传)


    Node.js 操作百度网盘实现文件上传(小文件上传,大文件分片上传)

    前提准备:获取百度网盘的授权码

    https://pan.baidu.com/union/doc/al0rwqzzl

    const fs = require('fs');
    const crypto = require('crypto');
    const path = require('path');
    const FormData = require('form-data');
    const axios = require('axios');
    
    const access_token = '需要自己创建应用获取授权码'
    
    async function readFile(filePath) {
        return new Promise((resolve, reject) => {
            fs.readFile(filePath, (err, data) => {
                if (err) {
                    reject(err);
                } else {
                    resolve(data);
                }
            });
        });
    }
    
    function getFileInfo(filePath) {
        return new Promise((resolve, reject) => {
            fs.stat(filePath, (err, stats) => {
                if (err) {
                    reject(err);
                } else {
                    resolve({
                        size: stats.size,
                        isFile: stats.isFile(),
                        name: path.basename(filePath)
                    });
                }
            });
        });
    }
    
    function getSlice(filePath, start, end) {
        return new Promise((resolve, reject) => {
            const sliceStream = fs.createReadStream(filePath, {
                start,
                end
            });
            let chunk = '';
            sliceStream.on('data', (data) => {
                chunk += data;
            });
            sliceStream.on('end', () => {
                resolve(chunk);
            });
            sliceStream.on('error', (err) => {
                reject(err);
            });
        });
    }
    
    async function getFileMd5List(filePath) {
        try {
            const fileInfo = await getFileInfo(filePath);
            const sliceSize = 4 * 1024 * 1024; // 4MB
    
            if (fileInfo.size <= sliceSize) {
                // 如果文件小于等于4MB,直接计算整个文件的MD5并返回
                const fileData = await readFile(filePath);
                const fileMd5 = crypto.createHash('md5').update(fileData).digest('hex');
                return JSON.stringify([fileMd5]);
            } else {
                // 如果文件大于4MB,分片计算MD5
                const sectionCount = Math.ceil(fileInfo.size / sliceSize);
                const md5List = [];
    
                for (let i = 0; i < sectionCount; i++) {
                    const sliceData = await getSlice(filePath, i * sliceSize, (i + 1) * sliceSize - 1);
                    const sliceMd5 = crypto.createHash('md5').update(sliceData).digest('hex');
                    md5List.push(sliceMd5);
                }
                return JSON.stringify(md5List);
            }
        } catch (error) {
            console.error('Error in getFileMd5List:', error);
            return null;
        }
    }
    
    async function uploadFileToBaidu(filePath, accessToken) {
        try {
            const fileInfo = await getFileInfo(filePath);
            let md5List = await getFileMd5List(filePath);
            console.log(md5List)
            const precreateRes = await axios.post('https://pan.baidu.com/rest/2.0/xpan/file', {
                path: '/apps/后台管理系统/' + fileInfo.name,
                size: fileInfo.size,
                isdir: fileInfo.isFile ? 0 : 1,
                rtype: 1,
                autoinit: 1,
                block_list: md5List
            }, {
                params: {
                    method: 'precreate',
                    access_token: accessToken
                },
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                }
            });
            const uploadId = precreateRes.data.uploadid;
            if (fileInfo.isFile && fileInfo.size <= 4 * 1024 * 1024) {
                // 小文件上传
                const form = new FormData();
                form.append('file', fs.createReadStream(filePath));
                const uploadSingle = await axios.post('https://d.pcs.baidu.com/rest/2.0/pcs/file', form, {
                        params: {
                            method: 'upload',
                            access_token: accessToken,
                            path: '/apps/后台管理系统/' + fileInfo.name,
                            ondup: 'overwrite'
                        },
                        headers: {
                            ...form.getHeaders(),
                        }
                    })
                ;
                return;
            } else {
                const sliceSize = 4 * 1024 * 1024;
                const sectionCount = Math.ceil(fileInfo.size / sliceSize);
                md5List = []
                for (let i = 0; i < sectionCount; i++) {
                    const form = new FormData();
                    form.append('file', await getSlice(filePath, i * sliceSize, (i + 1) * sliceSize - 1));
                    if (i === sectionCount - 1) {
                        form.append('file', await getSlice(filePath, i * sliceSize, fileInfo.size - 1));
                    }
                    const sectionItem = await axios.put('https://d.pcs.baidu.com/rest/2.0/pcs/superfile2', form, {
                        params: {
                            method: 'upload',
                            access_token: accessToken,
                            type: 'tmpfile',
                            path: '/apps/后台管理系统/' + fileInfo.name,
                            uploadid: uploadId,
                            partseq: i
                        },
                        headers: {
                            ...form.getHeaders(),
                        }
                    });
                    md5List.push(sectionItem.data.md5)
                }
                console.log(md5List)
                const uploadArray = await axios.post('https://pan.baidu.com/rest/2.0/xpan/file', {
                    path: '/apps/后台管理系统/' + fileInfo.name,
                    size: fileInfo.size,
                    isdir: fileInfo.isFile ? 0 : 1,
                    uploadid: uploadId,
                    block_list: JSON.stringify(md5List)
                }, {
                    params: {
                        method: 'create',
                        access_token: accessToken
                    },
                    headers: {
                        'User-Agent': 'pan.baidu.com',
                        'Content-Type': 'application/x-www-form-urlencoded',
                    },
                });
                console.log(uploadArray.data)
            }
        } catch (e) {
            console.log('error', e)
        }
    }
    
    // uploadFileToBaidu(path.resolve(__dirname, './WeChatProjects.zip'), access_token)
    // uploadFileToBaidu(path.resolve(__dirname, './auth.js'), access_token)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174

    在这里插入图片描述
    在这里插入图片描述

  • 相关阅读:
    自定义注解
    TouchGFX界面开发 | 项目代码结构分析
    requests库进行爬虫ip请求时遇到的错误解决方法
    leetcode 355 设计推特
    新能源行业供应商协同管理系统:可视化管理供应商,助力企业降本生效
    史上最全的Selenium三大等待介绍
    矩 阵 压 缩
    【springMvc】通过url向后台传递带有#井号的参数处理
    办学许可证申请流程,收藏起来慢慢看!
    Java静态代理模式
  • 原文地址:https://blog.csdn.net/ximing020714/article/details/132767248