Need to see if your shared folder is taking up space on your dropbox 👨💻? Find out how to check here.
Forum Discussion
Rephoto
3 years agoHelpful | Level 5
Error while copying content to a stream in C#
Hi,
I encountered the problem "Error while copying content to a stream" when executing the above code. Is there any way to fix it?
try
{
var chunkSize = 16 * 1024 * 1024; // 16 MB
int numChunks = (int)Math.Ceiling((double)fileStream.Length / chunkSize);
byte[] buffer = new byte[chunkSize];
string sessionId = null;
var tasks = new List<Task>();
for (var idx = 0; idx < numChunks; idx++)
{
var byteRead = fileStream.Read(buffer, 0, chunkSize);
using var memStream = new MemoryStream(buffer, 0, byteRead);
if (idx == 0)
{
var result = await _client.Files.UploadSessionStartAsync(body: memStream);
sessionId = result.SessionId;
}
else
{
var cursor = new UploadSessionCursor(sessionId, (ulong)(chunkSize * idx));
if (idx == numChunks - 1)
{
await Task.WhenAll(tasks);
await _client.Files.UploadSessionFinishAsync(cursor, new CommitInfo(toPathDropbox), body: memStream);
}
else
{
tasks.Add(_client.Files.UploadSessionAppendV2Async(cursor, body: memStream));
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Rephoto If you wish to optimize your upload process, please refer to the Performance Guide: https://developers.dropbox.com/dbx-performance-guide
That offers some ways you may be able to improve your effective overall upload speed. For example, there are batching and parallelization features you can use.
For instance, as Здравко noted, the default upload session type is "sequential", but you can use "concurrent" to upload file pieces in parallel. That would require you to make some more changes to your code to properly send those pieces in parallel though.
32 Replies
Replies have been turned off for this discussion
- Rephoto3 years agoHelpful | Level 5
Oh sorry,
I will investigate further :(( - Rephoto3 years agoHelpful | Level 5
ЗдравкоHi, I tried again with the code below to upload small files (e.g. 10, 20MB). But the result just failed. :))
- Здравко3 years agoLegendary | Level 20
Rephoto wrote:ЗдравкоHi, I tried again with the code below to upload small files (e.g. 10, 20MB). But the result just failed. :))
...
Hm..🤔 Probably I miss something, but where in your code you did upload files actually? 🧐 I cannot figure out where is this place/codeline. Please point out where you pass your files content to. Also don't forget that you should close every single session before finishing - so to be ready for finish! 😉
Add: When you have some error, don't forget to post the actual error/unexpected result content too. There is valuable information describing the error.
- Rephoto3 years agoHelpful | Level 5
Здравко
Oh, So I'm missing the file upload part. But I don't know which one to use? - Здравко3 years agoLegendary | Level 20
Rephoto wrote:Здравко
Oh, So I'm missing the file upload part. But I don't know which one to use?In general, you should use UploadSessionAppendV2Async method to upload stream' data. In such case you can combine the batch with big files upload either sequential or concurrent. When you know that only small files are going to upload, using single call of UploadSessionStartAsync to prepare a session (together with data upload and close) may be more suitable (instead of using UploadSessionStartBatchAsync - neither data can be uploaded nor closing it).
Let's hope this clarifies matter.
- Rephoto3 years agoHelpful | Level 5
Здравко, Thank 😍
Can I do it like this? But it gives me an exception ""Error while copying content to a stream." arrivalawait _client.Files.UploadSessionFinishAsync(finish, memStream); - Здравко3 years agoLegendary | Level 20
Rephoto, Why are you trying to upload the same stream twice? 🤔 Previously you skipped upload at all, now just opposite! 🙂
Something else: Why you skipped finishing the all sessions in batch? In such a way, there is no optimization! Even more - you can do the same with a single call. 😉
- Rephoto3 years agoHelpful | Level 5
Why are you trying to upload the same stream twice?
Previously you skipped upload at all, now just opposite!
...
ЗдравкоSo after I run the UploadSessionStartAsync command, how do I finish uploading to dropbox? 🤔 Thanks
- Здравко3 years agoLegendary | Level 20
If you want to just upload a single small file and that's all, using UploadAsync is the bast. In such a way you create session, upload and close the content and finish the upload - all in this single call. Here is no any optimization. In your last code snippet you make it rather slower - 2 calls instead of one.
If you want to make multiple files upload at once and optimize the process of finish, you can use UploadSessionFinishBatchAsync as you have it done already (or rather tried to use).
That's it. Clarify to yourself what actually you want to do.
- Rephoto3 years agoHelpful | Level 5
I have a large number of photos (including about 100 photos under 10Mb). If I use UploadAsync it is getting a lot of too_many_write_operations/ errors.
And that's why I need a new solution to push these photos to Dropbox in a short time. To perform the next tasks
About Discuss Dropbox Developer & API
Make connections with 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!