Sonatype Nexus rest api upload by C#
本文关键字:by upload api Nexus rest Sonatype | 更新日期: 2023-09-27 18:09:33
我需要通过c#上传一个工件到Nexus,但不能理解如何解释"cUrl -F"参数来告诉请求名称扩展和它应该去的存储库。有人能分享一下解决方案吗?
这是我最终成功的方法:
public async Task<HttpResponseMessage> UploadArtifact(string artifactPath, string artifactName, string artifactId)
{
using var systemClient = new HttpClient
{
DefaultRequestHeaders =
{
Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(Encoding.ASCII.GetBytes($"{_nexusUsername}:{_nexusPassword}")))
}
};
var client = _httpClient ?? new SystemHttpClient(systemClient);
var data = _file.ReadAllBytes(artifactPath);
using var content = new ByteArrayContent(data);
var postUrl = string.Format(CultureInfo.InvariantCulture, _settings.Value.NexusBaseUrl, artifactId, artifactName);
return await client.PutAsync(new Uri(postUrl), content).ConfigureAwait(false);
}
它的旋度等价于curl -v -u admin:admin123 --upload-file pom.xml http://localhost:8081/repository/maven-releases/org/foo/1.0/foo-1.0.pom
下面是一个使用上述方法的例子:
var response = await _nexusClient
.UploadArtifact(zipFilePath, zipFileName, request.ApplicationName).ConfigureAwait(false);
下面是包含上述方法的类的完整实现:
public interface INexusClient
{
Task<HttpResponseMessage> UploadArtifact(string artifactPath, string artifactName, string artifactId);
}
public sealed class NexusClient : INexusClient
{
private readonly IFile _file;
private readonly IHttpClient _httpClient;
private readonly string _nexusPassword;
private readonly string _nexusUsername;
private readonly IOptions<AppConfigurationSettings> _settings;
public NexusClient(IOptions<AppConfigurationSettings> settings, IFile file, IHttpClient httpClient, IEnvironment environment)
{
_settings = settings ?? throw new InvalidOperationException($"{nameof(settings)} must not be null");
_file = file;
_httpClient = httpClient;
if (environment == null) throw new InvalidOperationException($"{nameof(environment)} must not be null");
_nexusUsername = environment.GetEnvironmentVariable("nexusUsername");
_nexusPassword = environment.GetEnvironmentVariable("nexusPassword");
if (string.IsNullOrEmpty(_nexusUsername)) throw new InvalidOperationException($"{nameof(_nexusUsername)} is not configured in this environment");
if (string.IsNullOrEmpty(_nexusPassword)) throw new InvalidOperationException($"{nameof(_nexusPassword)} is not configured in this environment");
}
public async Task<HttpResponseMessage> UploadArtifact(string artifactPath, string artifactName, string artifactId)
{
using var systemClient = new HttpClient
{
DefaultRequestHeaders =
{
Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(Encoding.ASCII.GetBytes($"{_nexusUsername}:{_nexusPassword}")))
}
};
var client = _httpClient ?? new SystemHttpClient(systemClient);
var data = _file.ReadAllBytes(artifactPath);
using var content = new ByteArrayContent(data);
var postUrl = string.Format(CultureInfo.InvariantCulture, _settings.Value.NexusBaseUrl, artifactId, artifactName);
return await client.PutAsync(new Uri(postUrl), content).ConfigureAwait(false);
}
}
*注意IFile, IHttpClient和IEnvironment是我分别用于File, HttpClient和Environment类的包装器接口(以帮助测试)。