Upload client
The SDK provides an Upload client to upload files using predefined configurations to a Sitecore Content Hub instance. The currently supported configurations are:
-
Package import
-
Create asset
-
Update asset main file
-
Update asset alternative file
In the following code examples, the client
variable refers to the ContentHubClient
instance. When using the JavaScript SDK, you can choose your own variable name, but it is 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
:
const uploadSource = new LocalUploadSource("file://c:/my-package.zip");
const request = new UploadRequest(uploadSource, "ImportPackageConfiguration", "Import");
request.actionParameters = {
Type: "Package",
};
const result = await client.uploads.uploadAsync(request);
Upload and create an asset from a remote source
For example, the following snippet uploads and creates a new asset using the AssetUploadConfiguration
:
const uploadSource = new HttpUploadSource("https://picsum.photos/200");
const request = new UploadRequest(uploadSource, "AssetUploadConfiguration", "NewAsset");
const result = await client.uploads.uploadAsync(request);
Upload and create an asset from a buffer source
For example, the following snippets upload and create a new asset using the AssetUploadConfiguration
:
const fs = require("fs");
const uri = new URI("file://c:/img.jpg");
const buffer = fs.readFileSync(uri.path());
const uploadSource = new ArrayBufferUploadSource(buffer, "myFile");
const request = new UploadRequest(uploadSource, "AssetUploadConfiguration", "NewAsset");
const result = await client.uploads.uploadAsync(request);
const reader = new FileReader();
reader.onloadend = async () = {
const buffer = reader.result;
if (buffer instanceof ArrayBuffer) {
const source = new ArrayBufferUploadSource(buffer, file.name);
await client.uploads.uploadAsync(new UploadRequest(source, "AssetUploadConfiguration", "NewAsset"))
}
};
reader.readAsArrayBuffer(file);