Chunkin file upload? "Caller is not authorized"

Hey all,

I’m coding up the ability for a user to upload a picture in my app. I think I coded the chunking function correctly but I’m running into access errors?

Uncaught (in promise) Error: Call was rejected:
  Request ID: 8bb18dfb85d58fbbdf41d29833ebe0e4a61a19c51f6401323e42f2b651333d4d
  Reject code: 4
  Reject text: Caller is not authorized

My code Looks like so

const encodeArrayBuffer = (file) => Array.from(new Uint8Array(file));
const MAX_CHUNK_SIZE = 1024 * 500; // 500kb

async function chunkFile(fl) {
    const file = await fl.arrayBuffer();
    const fileSize = fl.size;

    let chunk = 1;
    const chunks: number[][] = [];
    for (
        let byteStart = 0;
        byteStart < fileSize;
        byteStart += MAX_CHUNK_SIZE, chunk++
    ) {
        const videoSlice = file.slice(
            byteStart,
            Math.min(fileSize, byteStart + MAX_CHUNK_SIZE)
        );
        const sliceToNat = encodeArrayBuffer(videoSlice);
        chunks.push(sliceToNat);
    }

    return chunks;
}

export const storeJpg = async (key, filePath): Promise<String> => {
    const authClient = await AuthClient.create();
    const identity = await authClient.getIdentity();
    const agent = new HttpAgent({
        identity,
    });
    await agent.fetchRootKey();
    const store: _SERVICE = Actor.createActor(idlFactory, {
        agent: agent,
        canisterId,
    });

    const chunks = await chunkFile(filePath);
    const batch = await store.create_batch({})

    for (const chunk of chunks) {
        await store.create_chunk({content: chunk, batch_id: batch.batch_id});
    }

    return store.commit_batch({batch_id: batch.batch_id, operations: [{
            'CreateAsset': {key: key, content_type: "image/jpg"}
        }]});
}

any advice?

the answer is to do

dfx canister --no-wallet call "<asset canister>" authorize '(principal " <your principal>  ")'
1 Like

you need to call the authorize method on the asset canister from a controller. By default only the canister creator can upload assets, otherwise anyone could go areound messing with your assets

5 Likes

Hah I figured, I just had absolutely no idea what to do :blush: