由C#上传的Sonatype Nexus rest api

问题描述 投票:1回答:1

我需要通过C#将工件上载到Nexus,但无法理解如何解释“ cUrl -F”参数来告知请求名称扩展名以及将其存储在哪个存储库中。有人可以分享一个解决方案吗?

c# .net nexus
1个回答
0
投票

这是对我有用的东西:

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类(以帮助进行测试)的包装器接口。

© www.soinside.com 2019 - 2024. All rights reserved.