简体   繁体   English

从带有 node.js 的私有存储库下载 zip 版本

[英]Download zip release from private repository with node.js

I'm trying to download zip release from a private repository, i've tried many solutions but none seems to work.我正在尝试从私人存储库下载 zip 版本,我尝试了很多解决方案,但似乎都没有。

Here my code:这是我的代码:

function Download(url, path, options) {
updateHeader(options.stageTitles.Downloading)
let received_bytes = 0;
let total_bytes = 0;

// eslint-disable-next-line
console.log('-----------------')
// eslint-disable-next-line
console.log(url, path)
console.log('-----------------')

var req = request(
    {
        method: 'GET',
        uri: url,
        headers: {
            Authorization: `token ${options.gitRepoToken}`,
            'Accept': 'application/octet-stream',
            'User-Agent': 'request module',
        },
        //encoding: null
    }
);

// eslint-disable-next-line
console.log(req)

var out = fs.createWriteStream(path);
req.pipe(out);

req.on('response', data => {
    // eslint-disable-next-line
    console.log(data.headers, data)
    total_bytes = parseInt(data.headers['content-length']);
});

req.on('data', chunk => {
    received_bytes += chunk.length;
    showProgress(received_bytes, total_bytes);
});

req.on('end', () => {
    Install(options)
});}

url variable equal something like: https://github.com/SolberLight/PRIVATE_REPO_NAME/releases/download/1.0/MY_ZIP_NAME.zip url 变量等于: https://github.com/SolberLight/PRIVATE_REPO_NAME/releases/download/1.0/MY_ZIP_NAME.ZADCDBD79A8D84175C2Z9B192

Looks like I'm always getting a content size of 9 bytes with a "not found" response so I'm guessing that my headers aren't correct?看起来我总是得到 9 字节的内容大小和“未找到”响应,所以我猜我的标题不正确?

thanks for your help !感谢您的帮助 !

EDIT 1: Made some progress but now it seems that the blob is not the right size编辑 1:取得了一些进展,但现在看来 blob 的大小不合适

 (async () => {
    //get latest release
    var response = await axios.get(
        `https://api.github.com/repos/${repoWithOwner}/releases/latest`,
        {
            headers: authHeaders,
        }
    );
    var assets = response.data.assets;
    for (var i = 0; i < assets.length; i++) {
        console.log(assets[i].url);
        response = await axios({
            method: "get",
            url: assets[i].url,
            responseType: "blob",
            headers: {
                //Accept: "application/octet-stream",
                ...authHeaders,
            },
        });
        // eslint-disable-next-line
        console.log(response.data, path)

        let reader = new FileReader()
        reader.onload = function() {
            if (reader.readyState == 2) {
                var buffer = new Buffer(reader.result)
                console.log(`Saving ${JSON.stringify({ fileName: 'myfile.zip', size: response.data.size })}`)
                outputFile(path, buffer, err => {
                    if (err) {
                        // eslint-disable-next-line
                        console.log(err.message)
                    } else {
                        // eslint-disable-next-line
                        console.log(path)
                    }
                })
            }
        }
        reader.readAsArrayBuffer(response.data)
    }
})();

You can use the Download repository archive (zip) endpoint, documented here您可以使用此处记录的下载存储库存档 (zip) 端点

Take into consideration the following:考虑以下几点:

  • You will perform a GET request to /repos/{owner}/{repo}/zipball/{ref}您将对 /repos/{owner}/{repo}/zipball/{ref} 执行 GET 请求

  • The first request will send a redirect (302 response), you need to follow the redirect properly, in this case, since you're using axios, you will need to handle it manually.第一个请求将发送一个重定向(302 响应),您需要正确地遵循重定向,在这种情况下,由于您使用的是 axios,您需要手动处理它。 The response will include a zipball_url you will use in the following request.响应将包含您将在以下请求中使用的zipball_url

  • Since you are downloading a private repo release, take into consideration these links are temporary and expire after five minutes.由于您正在下载私有 repo 版本,请注意这些链接是临时的,并且会在五分钟后过期。

  • The first request accepts a user or installation token (in case you're consuming the API as a GitHub Application).第一个请求接受用户或安装令牌(如果您将 API 作为 GitHub 应用程序使用)。 Read more about the difference between a GitHub App and a GitHub OAuth App here此处阅读有关 GitHub 应用程序和 GitHub OAuth 应用程序之间区别的更多信息

  • If you are using a GitHub OAuth application, ensure you have the repo scope , for GitHub Application, ensure you have the Contents permissions如果您使用的是 GitHub OAuth 应用程序,请确保您拥有repo scope ,对于 ZD3B7C913CDsFD6EBFEC0E9EC32 权限应用程序,确保您拥有Content

Check out the following working example:查看以下工作示例:

Download a zip release from GitHub从 GitHub 下载 zip 版本 View in Fusebit 在 Fusebit 中查看
const owner = 'stack-overflow-demos';

// For the purpose of this example, we have 1 release, so it will be always the first item.
const releases = await installationClient.rest.repos.listReleases({
  owner: 'stack-overflow-demos',
  repo: 'private-demo'
});

// Get release information (e.g zipball URL)
const release = await installationClient.rest.repos.getRelease({
  owner: 'stack-overflow-demos',
  repo: 'private-demo',
  release_id: releases.data[0].id,
});

const { token } = await installationClient.auth();

// According to official GitHub docs https://docs.github.com/en/rest/reference/repos#download-a-repository-archive-zip
// You will get a zipball URL you can use to download the zip file, you need to handle properly the redirects since a second GET
// request is needed, this time with an authenticated URL.
const { zipball_url, tag_name } = release.data;

const response = await getZipFile(zipball_url, token);

// Handle redirects properly if a responseUrl is returned that means you need to perform the redirect,
// you don't need to send the access token to this request, since is a signed URL
// Note: For private repositories, these links are temporary and expire after five minutes.
const {
  request: {
    res: { responseUrl },
  },
} = response;

if (responseUrl) {
  await downloadZipFile(responseUrl, `/tmp/'${tag_name}.zip`);
}

// Get information about the zip file you're about to download
const getZipFile = async (url, token) => {
  return await axios({
    redirect: 'manual',
    method: 'get',
    url,
    headers: {
      Authorization: `token ${token}`,
      Accept: 'application/vnd.github.v3+json',
    },
  });
};

// Download the zip file from the temporary secure URL returned from the first request
const downloadZipFile = async (url, filePath) => {
  const response = await axios({
    method: 'get',
    url,
    responseType: 'stream',
    headers: {
      Accept: 'application/octet-stream',
    },
  });

  return new Promise((resolve, reject) => {
    console.log(`writing to ${filePath}`);
    const dest = fs.createWriteStream(filePath);
    let progress = 0;
    response.data
      .on('end', () => {
        console.log('🎉 Done downloading file.');
        resolve(filePath);
      })
      .on('error', (err) => {
        console.error('🚫 Error downloading file.');
        reject(err);
      })
      .on('data', (d) => {
        progress += d.length;
        console.log(`🕛 Downloaded ${progress} bytes`);
      })
      .pipe(dest);
  });
};

This example is using Octokit这个例子是使用Octokit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM