Download single Asset from private repository in c# #110870
Replies: 3 comments 5 replies
-
Hello! You need to use your file's URL like this: https://raw.githubusercontent.com/[proj]/[repo]/main/[file] |
Beta Was this translation helpful? Give feedback.
-
I asked Copilot to check and correct it, again, this may not be correct but it points out valid points that you can use or may realize answer from Copilot : This C# method is designed to download a specific asset file from a GitHub repository's release. Here's a breakdown of what it does:
The method is marked as A better approach would be to public static async Task DownloadSingleAssetFile(string Token, string DesideredVersion, string ASSETNAME, string DestinationPath)
{
var url = $"https://github.com/MYNAME/REPONAME/releases/download/{DesideredVersion}/{ASSETNAME}";
using (var client = new System.Net.Http.HttpClient())
{
var credentials = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}:", Token);
credentials = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(credentials));
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", credentials);
var contents = await client.GetByteArrayAsync(url);
System.IO.File.WriteAllBytes(DestinationPath, contents);
}
} Please replace |
Beta Was this translation helpful? Give feedback.
-
@IT-Emilio , @jdevstatic I download GitHub files using Octokit.NET and HttpClient: var gitHubClient = new GitHubClient(new ProductHeaderValue(_settings.AppName));
var gitHubCredentials = new Credentials(token: _settings.GitHubToken);
gitHubClient.Credentials = gitHubCredentials;
// get raw file url
var repositoryContents =
await gitHubClient.Repository.Content.GetAllContentsByRef(
environmentSettings.Owner,
environmentSettings.Repository,
environmentSettings.Branch);
var firstOrDefault = repositoryContents.FirstOrDefault(c => c.Path == path);
var downloadUrl = firstOrDefault?.DownloadUrl;
// download raw file content
using var httpClient = new HttpClient();
var credentials = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}:", _settings.GitHubToken);
credentials = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(credentials));
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", credentials);
return await httpClient.GetAsync(downloadUrl, cancellationToken)
.ConfigureAwait(false); |
Beta Was this translation helpful? Give feedback.
-
Hi everyone,
I am building a C# application that should be able to download an asset from a specific release of my private repository. I have created a personal token to access the files and have given it the necessary permissions. According to the Git documentation, I should use a URL like: “https://github.com/{user}/{repo}/releases/download/{tag}/{asset-name}”
However, no matter what I try, I keep receiving a “404” response from the server. This is strange because when I paste the same URL into the browser, the resource downloads correctly.
Here is a code example of what I did on my application, but I have tried several options and none of these seems to work:
So, I’m wondering if it’s even possible to do what I’m trying to achieve. It seems odd that it’s impossible to download a single asset from a repository. I’ve found other people online who seem to be having the same problem, but I haven’t found any solutions.
Interestingly, when I try to use a URL like: “https://github.com/{user}/{repo}/archive/refs/tags/{release-tag}.zip” with the same code, it works correctly.
I hope someone can help me with this. Thanks for your time!
Guidelines
Beta Was this translation helpful? Give feedback.
All reactions