Copy an artifact from a remote repository to another
using OrasProject.Oras.Registry.Remote.Auth;
using OrasProject.Oras.Registry.Remote;
using OrasProject.Oras.Registry;
using Microsoft.Extensions.Caching.Memory;
namespace OrasProject.Oras.Tests.Examples;
public static class CopyArtifact
{
// This example demonstrates how to copy an artifact by tag/digest from a remote repository to another.
// For production use: Implement proper exception handling, cancellation, and dependency injection.
public static async Task CopyArtifactAsync()
{
const string srcRegistry = "source.io"; // change to your source registry
const string srcRepository = "src/test"; // change to your source repository
const string dstRegistry = "target.io"; // change to your destination registry
const string dstRepository = "dst/test"; // change to your destination repository
// Create a HttpClient instance for making HTTP requests.
var httpClient = new HttpClient();
// Create simple credential providers with static credentials.
var srcCredential = new SingleRegistryCredentialProvider(srcRegistry, new Credential
{
RefreshToken = "src_refresh_token" // change to your actual refresh token
});
var dstCredential = new SingleRegistryCredentialProvider(dstRegistry, new Credential
{
Username = "username", // change to your actual username
Password = "password" // change to your actual password
});
// Create a memory cache for caching access tokens to improve auth performance.
var memoryCache = new MemoryCache(new MemoryCacheOptions());
// Create repository instances to interact with the source and destination repositories.
var sourceRepository = new Repository(new RepositoryOptions
{
Reference = Reference.Parse($"{srcRegistry}/{srcRepository}"),
Client = new Client(httpClient, srcCredential, new Cache(memoryCache)),
});
var destinationRepository = new Repository(new RepositoryOptions
{
Reference = Reference.Parse($"{dstRegistry}/{dstRepository}"),
Client = new Client(httpClient, dstCredential, new Cache(memoryCache)),
});
// Copy the artifact identified by reference (tag or digest) from the source repository to the destination
const string reference = "tag"; // could also be a digest like "sha256:..."
var copiedRoot = await sourceRepository.CopyAsync(reference, destinationRepository, "");
}
}