logo
logo

Upload client

The SDK provides an Upload client to upload files using pre-defined configurations to a Sitecore Content Hub instance. The currently supported configurations are:

  • Package import
  • Create asset
  • Update asset main file
  • Update asset alternative file
Note

The client variable in the following code examples refers to the IMClient instance. When using the Web SDK, the variable name can be chosen freely, but it is also called client at instantiation in the documentation.

Upload and import a package from a local source

For example, the following snippet uploads and imports a package using the ImportPackageConfiguration:

var uploadSource = new LocalUploadSource("C:\\my-package.zip");

var request = new UploadRequest(uploadSource, "ImportPackageConfiguration", "Import")
{
    ActionParameters =
    {
        { "Type", "Package" }
    }
};

// Initiate upload and wait for its completion.
var response = await client.Uploads.UploadAsync(request).ConfigureAwait(false);

Upload and create an asset from a local file

For example, the following snippet uploads and creates a new asset using the AssetUploadConfiguration:

var uploadSource = new LocalUploadSource("C:\\my-image.jpg", "my-image.jpg");

var request = new UploadRequest(uploadSource, "AssetUploadConfiguration", "NewAsset");

// Initiate upload and wait for its completion.
var response = await client.Uploads.UploadAsync(request).ConfigureAwait(false);

// Extract ID of newly created asset from the location header.
var responseId = await client.LinkHelper.IdFromEntityAsync(response.Headers.Location).ConfigureAwait(false);

Upload and create an asset from a byte array

Warning

Avoid using the ByteArrayUploadSource to upload local files, as the entire contents will be kept in memory and the underlying array-based implementation is subject to a limit of approximately 2 gigabytes. See an example using the LocalUploadSource above.

For example, the following snippet uploads and creates a new asset using the AssetUploadConfiguration:

// `RetrieveBytes` is an arbitrary helper method for demonstration purposes to fetch the byte[] from a source.
var bytes = await RetrieveBytes().ConfigureAwait(false);
var uploadSource = new ByteArrayUploadSource(bytes, "my-image.jpg");

var request = new UploadRequest(uploadSource, "AssetUploadConfiguration", "NewAsset");

// Initiate upload and wait for its completion.
var response = await client.Uploads.UploadAsync(request).ConfigureAwait(false);

// Extract ID of newly created asset from the location header.
var responseId = await client.LinkHelper.IdFromEntityAsync(response.Headers.Location).ConfigureAwait(false);

Upload and create an asset from a remote source

For example, the following snippet uploads and creates a new asset using the AssetUploadConfiguration:

var uri = new Uri("https://picsum.photos/200", UriKind.Absolute);
var uploadSource = new HttpUploadSource(_client, uri);

var request = new UploadRequest(uploadSource, "AssetUploadConfiguration", "NewAsset");

// Initiate upload and wait for its completion.
var response = await client.Uploads.UploadAsync(request).ConfigureAwait(false);

// Extract ID of newly created asset from the location header.
var responseId = await client.LinkHelper.IdFromEntityAsync(response.Headers.Location).ConfigureAwait(false);

Can we improve this article ? Provide feedback