如何使用NodeJS从gitlab同步下载文件

发布于 2025-01-11 12:05:49 字数 1846 浏览 0 评论 0原文

我需要从私人 gitlab 服务器下载文件,并且需要该方法是同步的。这是由之前的异步代码实现的,它工作得很好,因为我使用的是 Promise。但我在将其转换为同步时遇到问题。我在 SO 上看到的其他帖子要么最终使用了异步代码,要么没有标题选项。

            const https = require('https');
            const fs = require('fs');
            const gitlabUrl = 'https://gitlab.custom.private.com';
            const gitlabAcessToken = 'xmyPrivateTokenx';
            const gLfilePath = '/api/v4/projects/1234/repository/files/FolderOne%2Ftest.txt/raw?ref=main';
            const gLfileName='test.txt';
            
                
            function downloadFileFromGitlab(filePath, fileName) {
                    return new Promise((resolve, reject) => {
                        var options = {
                            path: filePath,
                            headers: {
                                'PRIVATE-TOKEN': gitlabAccessToken
                            }
                        };
                        
                        var url = gitlabUrl
                        var file = fs.createWriteStream(fileName);
                        
                        const request = https.get(url, options, (response) => {
                            response.pipe(file);
                            file.on('finish', () => {
                                file.close();
                                resolve();
                            });
                            file.on('error', (err) => {
                                file.close();
                                reject(err);
                            });
                        });
                
                        request.on('error', error => {
                            throw console.error(error);
                        });
                    });
                }

downloadFileFromGitlab(gLfilePath,gLfileName);

I need to download a file from a private gitlab server and I need the method to be synchronous. This was by previous async code and it works fine because I was using promises. But I'm having trouble converting it to synchronous. The other posts i've seen on SO either ended up using async code or didn't have options for headers.

            const https = require('https');
            const fs = require('fs');
            const gitlabUrl = 'https://gitlab.custom.private.com';
            const gitlabAcessToken = 'xmyPrivateTokenx';
            const gLfilePath = '/api/v4/projects/1234/repository/files/FolderOne%2Ftest.txt/raw?ref=main';
            const gLfileName='test.txt';
            
                
            function downloadFileFromGitlab(filePath, fileName) {
                    return new Promise((resolve, reject) => {
                        var options = {
                            path: filePath,
                            headers: {
                                'PRIVATE-TOKEN': gitlabAccessToken
                            }
                        };
                        
                        var url = gitlabUrl
                        var file = fs.createWriteStream(fileName);
                        
                        const request = https.get(url, options, (response) => {
                            response.pipe(file);
                            file.on('finish', () => {
                                file.close();
                                resolve();
                            });
                            file.on('error', (err) => {
                                file.close();
                                reject(err);
                            });
                        });
                
                        request.on('error', error => {
                            throw console.error(error);
                        });
                    });
                }

downloadFileFromGitlab(gLfilePath,gLfileName);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不气馁 2025-01-18 12:05:49

我能够使用curl解决这个问题

function downloadFileFromGitlab(filePath, fileName) {
    let curlCommand = "curl -s " + gitlabUrl + filePath + " -H 'PRIVATE-TOKEN:" + gitlabAccessToken +"'";
    let file = child_process.execSync(curlCommand);
    fse.writeFileSync(fileName,file);
}

I was able to figure it out using curl

function downloadFileFromGitlab(filePath, fileName) {
    let curlCommand = "curl -s " + gitlabUrl + filePath + " -H 'PRIVATE-TOKEN:" + gitlabAccessToken +"'";
    let file = child_process.execSync(curlCommand);
    fse.writeFileSync(fileName,file);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文