简体   繁体   中英

Download Github Artifact

I am trying to download a Github Artifact from Java code. For this I am using the GitHub API for Java . With the API I can authenticate and list all the GHWorkflowRuns and their GHArtifacts.

If I try to download from GHArtifact.getArchiveDownloadUrtl() I just get a 403 response.

When trying the same URL from a browser (unauthenticated in this case) I get

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

and I checked the personal access token so it should have sufficient access privileges. There is no extra 'Actions' scope in the configuration dialog but I checked Workflow which includes everything on Repository.

There is another function called GHArtifact.download() but I have no clue how to use it. Does anyone know how to download the artifacts or how to use that download function?

Edit: tgdavies mentioned repository.readTar() which has a similar signature. Going from that I tried to create code like this:

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

but my compiler complains with

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>)

I hope this better explains where I am lost.

I don't have a project with artifacts, but this is how you use the download 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);
    }
}

Change permissions of the PAT (personal access token) and grant the scope it demands.
As guest, you might run into this issue: https://github.com/actions/upload-artifact/issues/51 .

It may sound dumb, but it really took me some time to figure out how to really use that download function. Finally I had success with this:

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;
            });
    }
}

And to resolve the authentication issue: When I used the browser to access the API URL I had forgotten about missing authentication. But same so happened when programmatically I opened the URL and tried the download there.

That's why it is important for me to stay with API functionality - the API takes care of the authentication internally.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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