0

I have 2 methods to upload files to Blob Storage. 'UploadFileToContainer' is working fine, and i get files with data in container. The problem is 'UploadOrReplaceFileToContainer', where it's not possible to use file.Position = 0, returning the follow error:

System.NotSupportedException: 'Specified method is not supported.'

Can someone help?

public void UploadFileToContainer(Stream file, string fileName, string userId)
    {
        string uniqueFileName = GetFileNameUniqueId(fileName);
        BlobClient blob = _container.GetBlobClient(fileName);
        blob.Upload(file);
        file.Position = 0;
        BlobClient blob2 = _containerbackup.GetBlobClient(uniqueFileName);
        blob2.Upload(file);
        _log.LogFileUpload(fileName, uniqueFileName, userId, DateTime.Now);
    }

    public int UploadOrReplaceFileToContainer(Stream file, string fileName, string userId)
    {
        string uniqueFileName = GetFileNameUniqueId(fileName);
        if (_container.GetBlobs().Any(b => b.Name == fileName))
        {
            _container.DeleteBlob(fileName);
        }
        BlobClient blob = _container.GetBlobClient(fileName);
        blob.Upload(file);
        file.Position = 0; //here is where i get the error
        BlobClient blob2 = _containerbackup.GetBlobClient(uniqueFileName);
        blob2.Upload(file);
        int fileUploadId = _log.LogFileUpload(fileName, uniqueFileName, userId, DateTime.Now);
        return fileUploadId;
    }
Selim Yildiz
  • 5,254
  • 6
  • 18
  • 28
ferrub
  • 3
  • 2
  • Check out [Stream.Seek](https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.seek?view=netframework-4.8), and it's companion property, [CanSeek](https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.canseek?view=netframework-4.8) – Sam Axe Nov 25 '19 at 15:57
  • 1
    What type of stream is it? Some streams do not support seeking. – Magnus Nov 25 '19 at 16:00
  • @ferrub, ant update on this issue, could you use it with my way? – George Chen Nov 29 '19 at 05:27

1 Answers1

1

You are getting this error suppose your stream type canseek type is false. You could create a new MemoryStream and copy the stream to the MemoryStream then seek it or just set the position.

System.IO.MemoryStream blobstream = new System.IO.MemoryStream();
inputblob.CopyTo(blobstream);            
blobstream.Position = 0;
//blobstream.Seek(0, SeekOrigin.Begin);
blob.Upload(blobstream);

Also you could refer to other discussion about this:read a Stream and reset its position to zero.

George Chen
  • 13,703
  • 2
  • 11
  • 26