I am uploading a PNG image to local blob storage using Docker/Azurite.
The file on my drive is 167.08 KiB. When I upload the file without setting the headers, it's also 167.08 KiB, however the Content Type is set to application/octet-stream
and is therefore not served correctly.
Easy enough fix, I thought. I set the Content Type header to image/png
- but now the blob is only 167.06 KiB, and the file seems "corrupted". It cannot be viewed or opened by any image editor.
Can someone advise? Code below.
167.06 KiB, file is invalid:
public async Task<Uri> Upload(string container, string destination, MemoryStream stream)
{
stream.Position = 0;
var blob = this.Blob(container, destination);
var headers = new BlobHttpHeaders() { ContentType = "image/png" };
var uploadOptions = new BlobUploadOptions() { HttpHeaders = headers };
await blob.UploadAsync(stream, uploadOptions);
return blob.Uri;
}
167.08 KiB, file has incorrect Content Type:
public async Task<Uri> Upload(string container, string destination, MemoryStream stream)
{
stream.Position = 0;
var blob = this.Blob(container, destination);
await blob.UploadAsync(stream);
return blob.Uri;
}
If anyone else stumbles across this, I reset the stream position immediately before uploading, and that resolved the issues.
public async Task<Uri> Upload(string container, string destination, MemoryStream stream)
{
//stream.Position = 0; <------ THIS NEEDS TO BE MOVED...
var blob = this.Blob(container, destination);
var headers = new BlobHttpHeaders() { ContentType = "image/png" };
var uploadOptions = new BlobUploadOptions() { HttpHeaders = headers };
stream.Position = 0; // <------- HERE
await blob.UploadAsync(stream, uploadOptions);
return blob.Uri;
}