Streaming upload to google cloud storage from multipart section

103 Views Asked by At

In an ASP.NET Core 6.0 Web API, I'm trying to upload file to google cloud storage. Based on this example, I have:

var boundary = MultipartRequestHelper.GetBoundary(
    MediaTypeHeaderValue.Parse(uploadObject.Request.ContentType),
    _fileUploadOptions.MaximumBoundaryLimit);

var reader = new MultipartReader(boundary, uploadObject.Request.Body);
var section = await reader.ReadNextSectionAsync(cancellationToken);
var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse(
    section.ContentDisposition,
    out var contentDispositionHeader);

using var client = await StorageClient.CreateAsync(_credential);

var body = new Google.Apis.Storage.v1.Data.Object
{
    Bucket = _storageOptions.DefaultBucketName,
    ContentDisposition = contentDispositionHeader.Name.Value,
    Name = "test.mp4"
};

var result = await client.UploadObjectAsync(body, section.Body,
                                            cancellationToken: cancellationToken);

where:

uploadObject.Request is HttpRequest

This code throws an error

The format of value 'bytes 0-945000/0' is invalid

If I do something like this:

using var ms = new MemoryStream();
await section.Body.CopyToAsync(ms, cancellationToken);

before client.UploadObjectAsync, this code will run successfully.

In the Microsoft example, they also has ProcessStreamedFile function which called before upload process.

So my question is what this exception means and how I can fix it? Also is it necessary to use section.Body.CopyToAsync before using section.Body in client.UploadAsync?

Thank you in advance

0

There are 0 best solutions below