We’re Still Here to Help (Even Over the Holidays!) - find out more here.
Forum Discussion
David W.152
10 years agoNew member | Level 1
API v2 upload_session/append_v2 doc help
Hi dropbox team,
I have no idea how to upload a larger file (>= 1GB) via upload_session/* API.
Can not find any full example code with that.
APIs:
/upload_session/start /upload_session/appen...
Greg-DB
Dropbox Community Moderator
10 years agoHi David, I unfortunately don't have a sample of using upload sessions in bash/curl directly, but here are some examples using the Python and .NET SDKs, which should serve as a useful reference for the necessary logic:
# This uses the Dropbox Python SDK https://github.com/dropbox/dropbox-sdk-python to upload a file to the Dropbox API from the local file as specified by `file_path` to the remote path as specified by `dest_path`. It also chooses whether or not to use an upload session based on the size of the file:
f = open(file_path)
file_size = os.path.getsize(file_path)
CHUNK_SIZE = 4 * 1024 * 1024
if file_size <= CHUNK_SIZE:
print dbx.files_upload(f.read(), dest_path)
else:
upload_session_start_result = dbx.files_upload_session_start(f.read(CHUNK_SIZE))
cursor = dropbox.files.UploadSessionCursor(session_id=upload_session_start_result.session_id,
offset=f.tell())
commit = dropbox.files.CommitInfo(path=dest_path)
while f.tell() < file_size:
if ((file_size - f.tell()) <= CHUNK_SIZE):
print dbx.files_upload_session_finish(f.read(CHUNK_SIZE),
cursor,
commit)
else:
dbx.files_upload_session_append_v2(f.read(CHUNK_SIZE),
cursor)
cursor.offset = f.tell()
f.close()
// This example uses the Dropbox .NET library https://github.com/dropbox/dropbox-sdk-dotnet to upload a file to a Dropbox account, using upload sessions for larger files:
private async Task Upload(string localPath, string remotePath)
{
const int ChunkSize = 4096 * 1024;
using (var fileStream = File.Open(localPath, FileMode.Open))
{
if (fileStream.Length <= ChunkSize)
{
await this.client.Files.UploadAsync(remotePath, body: fileStream);
}
else
{
await this.ChunkUpload(remotePath, fileStream, (int)ChunkSize);
}
}
}
private async Task ChunkUpload(String path, FileStream stream, int chunkSize)
{
ulong numChunks = (ulong)Math.Ceiling((double)stream.Length / chunkSize);
byte[] buffer = new byte[chunkSize];
string sessionId = null;
for (ulong idx = 0; idx < numChunks; idx++)
{
var byteRead = stream.Read(buffer, 0, chunkSize);
using (var memStream = new MemoryStream(buffer, 0, byteRead))
{
if (idx == 0)
{
var result = await this.client.Files.UploadSessionStartAsync(false, memStream);
sessionId = result.SessionId;
}
else
{
var cursor = new UploadSessionCursor(sessionId, (ulong)chunkSize * idx);
if (idx == numChunks - 1)
{
FileMetadata fileMetadata = await this.client.Files.UploadSessionFinishAsync(cursor, new CommitInfo(path), memStream);
Console.WriteLine (fileMetadata.PathDisplay);
}
else
{
await this.client.Files.UploadSessionAppendV2Async(cursor, false, memStream);
}
}
}
}
}
- ivanhigueram7 years agoExplorer | Level 4
Hi Greg! Is possible to update the SO Documentation links, they're broken now :(
- Greg-DB7 years ago
Dropbox Community Moderator
ivanhigueram I've updated my earlier post to embed the code snippets from the now-defunct StackOverflow Documentation pages directly. Hope this helps!
- ivanhigueram7 years agoExplorer | Level 4
Hi Greg-DB Thanks for replying back and point me to the code links. I recently opened another issue in the forum (https://www.dropboxforum.com/t5/API-Support-Feedback/Python-V2-How-to-do-batch-upload/m-p/329517) with my specific problem using batch upload.
Thanks!
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.
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, Facebook or Instagram.
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!