dropbox-sdk-dotnet icon indicating copy to clipboard operation
dropbox-sdk-dotnet copied to clipboard

DropbboxClient Upload closes Stream

Open glindstr opened this issue 2 years ago • 1 comments

When calling a DropboxClient to upload a file via a stream, the function has the side effect of closing the stream:

using var dropbboxClient = new DropboxClient("apitoken");
using var stream = new MemoryStream(Encoding.UTF8.GetBytes("a text file"));
var request = new UploadArg("my new text file.txt");
var result = await dropbboxClient.Files.UploadAsync(request, stream);
stream.Seek(0, SeekOrigin.Begin); //Exception. Stream is closed

What happens: dropbboxClient.Files.UploadAsync closes the stream after upload.

What should happen: dropbboxClient.Files.UploadAsync should not close the stream. This is a hidden side effect that shouldn't happen.

Temporary Workaround: Copy the stream into another stream.

AWS has some nice convenience features parallel to this with their s3 SDK.

var putRequest = new PutObjectRequest()
{
    BucketName = Bucket,
    Key = s3FileKey,
    AutoCloseStream = false,
    AutoResetStreamPosition = true,
    InputStream = stream,
};

The default of the sdk should be to NOT close the stream as this is a hidden side effect. However a nice enhancement would be to provide convenience properties in the class UploadArg of "AutoCloseStream" and "AutoResetStreamPosition" which can be set.

Thank you for reviewing.

glindstr avatar Jan 13 '23 23:01 glindstr

Thanks for writing this up! I'll ask the team to look into changing this behavior and offer options for it.

greg-db avatar Jan 16 '23 16:01 greg-db