Learn how to make the most out of the Dropbox Community here đ.
Learn how to make the most out of the Dropbox Community here đ.
Hey DB-Des ,
thank you so much for the detailed answer.
I already saw the examples and explanations, but could not adapt it to my specific use case.
Meanwhile I solved the issue.
Two things I recognized/learned:
For each file in a batch, in parallel, call /files/upload_session/append_v2 as needed to upload the full contents of the file over multiple requests.
I solved everything for my case, but wanted to drop these thoughts for people having the same problem.
Additionally my code that worked for me:
async function filesUploadSessionAppendBatch(
content: ArrayBufferLike,
entries: AppendBatchEntry[],
) {
// this gets a token or a new one if it is expired
const accessToken = await dropboxMaker.getToken();
const response = await fetch(
"https://content.dropboxapi.com/2/files/upload_session/append_batch",
{
method: "POST",
headers: {
Authorization: `Bearer ${accessToken}`,
"Dropbox-API-Arg": JSON.stringify({
entries,
}).replace(/[\u007f-\uffff]/g, getSafeUnicode),
"Content-Type": "application/octet-stream",
},
body: content,
},
);
return response.json();
}
export async function uploadFiles(files: UploadFileData[]) {
const num_sessions = files.length;
try {
if (num_sessions < 1) {
return {};
}
const dbx = await dropboxMaker.client();
if (num_sessions === 1) {
// upload one file
await dbx.filesUpload({
...files[0],
});
} else {
// upload multiple files
// merges all files in one ArrayBuffer (to make one single request....will still be far away from the upload limit)
const allContent = concatArrayBuffers(
...files.map(({ contents }) => contents),
);
const startResponse = await dbx.filesUploadSessionStartBatch({
num_sessions,
});
const { session_ids } = startResponse.result;
const batchData: Array<FullBatchEntry> = files.reduce(
(
acc: { offset: number; entries: Array<FullBatchEntry> },
cur,
index,
) => {
acc.entries.push({
close: true,
cursor: {
session_id: session_ids[index],
offset: 0,
// always start with 0 for a new session. Donât upcount.
},
commit: {
autorename: true,
/** @ts-expect-error Wrong type used (see CommitInfo) */
mode: "add",
mute: false,
path: cur.path,
},
length: cur.contents.byteLength,
});
// not needed anymore
// acc.offset += cur.contents.byteLength;
return acc;
},
{
offset: 0,
entries: [],
},
).entries;
await filesUploadSessionAppendBatch(
allContent,
batchData.map(({ cursor, length, close }) => ({
cursor,
length,
close,
})),
);
await dbx.filesUploadSessionFinishBatchV2({
entries: batchData.map(({ commit, cursor, length }) => ({
commit,
cursor: {
...cursor,
// point to the end of the upload file part...so length
offset: length,
},
})),
});
}
} catch (error) {
console.log(error);
}
}
Thanks again for your help đ
If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X or Facebook.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!