Using the File Store API

The API reference documentation describes the available operations that are possible with the File Store API.

This section describes some common operations you would perform with the API to manage the lifecycle of a digital asset.

How to get a list of files uploaded to the service

cUrl

Replace the values enclosed in [] with their required values.
curl -X GET 'https://[SERVER_URL]/__dxp/service/file-storage/api/v2/file?source=/' -H "x-api-key: [API_KEY]" -H "x-dxp-tenant: [TENANT_ID]"

Javascript

async function getFiles(serverHost, apiKey, tenantId) {
    const response = await fetch(`https://${serverHost}/__dxp/service/file-storage/api/v2/file?source=/`, {
        method: "GET",
        headers: {
            "x-api-key": apiKey,
            "x-dxp-tenant": tenantId,
        }
    });
    const body = await response.json();

    if (!response.ok) {
        throw new Error(`GET file failed: ${body.message}`);
    }

    console.log(body)
}

How to create a directory and add a file to the service

cUrl

Replace the values enclosed in [] with their required values.
curl -X POST https://[SERVER_URL]/__dxp/service/file-storage/api/v2/directory -H "x-api-key: [API_KEY]" -H "x-dxp-tenant: [TENANT_ID]" -d '{"source":"test-directory","access":"public"}'
curl -X POST https://[SERVER_URL]/__dxp/service/file-storage/api/v2/file -H "x-api-key: [API_KEY]" -H "x-dxp-tenant: [TENANT_ID]" -d '{"source":"test-directory/test.json","access":"public","data":"{\"test\":\"test\"}"}'

Javascript

async function createDirectoryAndFile(serverHost, apiKey, tenantId) {
    const directoryResponse = await fetch(`https://${serverHost}/__dxp/service/file-storage/api/v2/directory`, {
        method: "POST",
        headers: {
            "x-api-key": apiKey,
            "x-dxp-tenant": tenantId,
        },
        body: JSON.stringify({
            "source": "test-directory",
            "access": "public",
        }),
    });
    const directoryBody = await directoryResponse.json();

    if (!directoryResponse.ok) {
        throw new Error(`POST directory failed: ${directoryBody.message}`);
    }

    console.log(directoryBody);

    const fileResponse = await fetch(`https://${serverUrl}/__dxp/service/file-storage/api/v2/file`, {
        method: "POST",
        headers: {
            "x-api-key": apiKey,
            "x-dxp-tenant": tenantId,
        },
        body: JSON.stringify({
            "source": "test-directory/test.json",
            "access": "public",
            "data": JSON.stringify({test:"test"})
        }),
    });
    const fileBody = await fileResponse.json();

    if (!fileResponse.ok) {
        throw new Error(`POST file failed: ${fileBody.message}`);
    }

    console.log(fileBody);
}

How to update the version of a file stored in the service

Replace the values enclosed in [] with their required values.

cUrl

curl -X POST https://[SERVER_URL]/__dxp/service/file-storage/api/v2/file/pass:[{{file-id}}] -H "x-api-key: [API_KEY]" -H "x-dxp-tenant: [TENANT_ID]" -d '{"access":"private"}'

Javascript

async function updateFileAccess(serverHost, apiKey, tenantId, fileId) {
    const response = await fetch(`https://${serverHost}/__dxp/service/file-storage/api/v2/file/${fileId}`, {
        method: "PATCH",
        headers: {
            "x-api-key": apiKey,
            "x-dxp-tenant": tenantId,
        },
        body: JSON.stringify({
            "access": "private",
        }),
    });
    const body = await response.json();

    if (!response.ok) {
        throw new Error(`PATCH file failed: ${body.message}`);
    }

    console.log(body);
}

How to delete a file stored in the service

cUrl

Replace the values enclosed in [] with their required values.
curl -X DELETE https://[SERVER_URL]/__dxp/service/file-storage/api/v2/file/pass:[{{file-id}}] -H "x-api-key: [API_KEY]" -H "x-dxp-tenant: [TENANT_ID]"
curl -X GET https://[SERVER_URL]/__dxp/service/file-storage/{{polling-url-from-delete-response}} -H "x-api-key: [API_KEY]" -H "x-dxp-tenant: [TENANT_ID]"

Javascript

async function deleteFile(serverHost, apiKey, tenantId, fileId) {
    const deleteResponse = await fetch(`https://${serverHost}/__dxp/service/file-storage/api/v2/file/${fileId}`, {
        method: "DELETE",
        headers: {
            "x-api-key": apiKey,
            "x-dxp-tenant": tenantId,
        }
    });
    const deleteBody = await deleteResponse.json();

    if (!deleteResponse.ok) {
        throw new Error(`DELETE file failed: ${deleteBody.message}`);
    }

    console.log(deleteBody);

    let deleteComplete = false;
    do{
        const statusResponse = await fetch(`https://${serverHost}/__dxp/service/file-storage/${body.pollingUrl}`, {
            method: "GET",
            headers: {
                "x-api-key": apiKey,
                "x-dxp-tenant": tenantId,
            }
        });

        const statusBody = await statusResponse.json();

        if (!statusResponse.ok) {
            throw new Error(`GET status failed: ${statusBody.message}`);
        }

        console.log(statusBody)

        if(statusBody.status === 'SUCCEED'){
            deleteComplete = true;
        }

    }while(!deleteComplete)
}