Upload client

This SDK provides a client for uploading files with predefined configurations to a Sitecore Content Hub instance. Supported configurations are:

  • Package import

  • Create asset

  • Update asset main file

  • Update asset alternative file

Important

To upload assets using the SDK, you must have Read permission for the M.UploadConfiguration definition.

Note

The client variable in the following code examples refers to the IMClient instance. When using the Web SDK, you can choose the variable name, 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:

RequestResponse

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 an asset using AssetUploadConfiguration:

RequestResponse

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

Do not use the ByteArrayUploadSource to upload local files, because the entire contents will be kept in memory and the underlying array-based implementation has a limit of approximately 2 gigabytes. Instead, use LocalUploadSource.

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

RequestResponse

// `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 an asset using the AssetUploadConfiguration:

RequestResponse

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);

Do you have some feedback for us?

If you have suggestions for improving this article,