繁体   English   中英

下载神器Github

[英]Download Github Artifact

我正在尝试从 Java 代码下载 Github 工件。 为此,我将GitHub API 用于 Java 使用 API,我可以验证并列出所有 GHWorkflowRuns 及其 GHArtifacts。

如果我尝试从GHArtifact.getArchiveDownloadUrtl()下载,我只会收到 403 响应。

当从浏览器尝试相同的 URL 时(在这种情况下未经身份验证)我得到

{
  message:  "You must have the actions scope to download artifacts."
  documentation_url:    "https://docs.github.com/rest/reference/actions#download-an-artifact"
}

我检查了个人访问令牌,因此它应该具有足够的访问权限。 配置对话框中没有额外的“操作”scope,但我检查了包含存储库上所有内容的工作流。

还有另一个 function 叫做GHArtifact.download()但我不知道如何使用它。 有谁知道如何下载工件或如何使用该下载 function?

编辑:tgdavies 提到 repository.readTar() 具有类似的签名。 从那开始,我尝试创建这样的代码:

GHWorkflowRun run = ...
List<GHArtifact> artifacts = run.listArtifacts().toList();
for (GHArtifact artifact: artifacts) {
    if ("desiredname".equals(run.getName())) {
        artifact.download(is -> {
            return null;
        }, null);
    }
}

但我的编译器抱怨

error: method download in class GHArtifact cannot be applied to given types;
                artifact.download(is -> {
                        ^
  required: InputStreamFunction<T>
  found:    (is)->{ re[...]ll; },<null>
  reason: cannot infer type-variable(s) T
    (actual and formal argument lists differ in length)
  where T is a type-variable:
    T extends Object declared in method <T>download(InputStreamFunction<T>)

我希望这能更好地解释我迷路的地方。

我没有带有工件的项目,但这是您使用下载 API 的方式:

import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;

import java.io.File;
import java.io.IOException;

import static org.apache.commons.io.FileUtils.copyInputStreamToFile;

public class Test {
    public static void main(String[] args) throws IOException {

        GitHub github = GitHub.connect("your user id", "your access token");
        GHRepository repository = github.getRepository("tgdavies/cardcreator");
        repository.readTar(is -> {
            copyInputStreamToFile(is, new File("foo.tar"));
            return null;
        }, null);
    }
}

更改 PAT(个人访问令牌)的权限并授予其所需的 scope。
作为访客,您可能会遇到这个问题: https://github.com/actions/upload-artifact/issues/51

这听起来可能很愚蠢,但我确实花了一些时间才弄清楚如何真正使用该下载 function。最后我成功了:

GHWorkflowRun run = ...
List<GHArtifact> artifacts = run.listArtifacts().toList();
for (GHArtifact artifact: artifacts) {
    if ("desiredname".equals(run.getName())) {
            File target = ...
            artifact.download(is -> {
                Files.copy(is, target.toPath(), StandardCopyOption.REPLACE_EXISTING);
                return null;
            });
    }
}

并解决认证问题: 当我使用浏览器访问 API URL 时,我忘记了缺少认证。 但是当我以编程方式打开 URL 并尝试在那里下载时,同样如此。

这就是为什么保留 API 功能对我来说很重要 - API 负责内部身份验证。

暂无
暂无

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

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