Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8914669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:46:59+00:00 2026-06-15T04:46:59+00:00

The background to this question is based on a virtual file system I’m developing.

  • 0

The background to this question is based on a virtual file system I’m developing. The concept I’m using is virutal path providers for different types of storage type i.e local file system, dropbox and amazon s3. My base class for a virtual file looks like this:

public abstract class CommonVirtualFile : VirtualFile {
    public virtual string Url {
        get { throw new NotImplementedException(); }
    }
    public virtual string LocalPath {
        get { throw new NotImplementedException(); }
    }
    public override Stream Open() {
        throw new NotImplementedException();
    }
    public virtual Stream Open(FileMode fileMode) {
        throw new NotImplementedException();
    }
    protected CommonVirtualFile(string virtualPath) : base(virtualPath) { }
}

The implementation of the second Open method is what my question is all about. If we look at my implementation for the local file system i.e saving a file on disk it looks like this:

public override Stream Open(FileMode fileMode) {
    return new FileStream("The_Path_To_The_File_On_Disk"), fileMode);
}

If I would like to save a file on the local file system this would look something like this:

    const string virtualPath = "/assets/newFile.txt";
    var file = HostingEnvironment.VirtualPathProvider.GetFile(virtualPath) as CommonVirtualFile;
    if (file == null) {
        var virtualDir = VirtualPathUtility.GetDirectory(virtualPath);
        var directory = HostingEnvironment.VirtualPathProvider.GetDirectory(virtualDir) as CommonVirtualDirectory;
        file = directory.CreateFile(VirtualPathUtility.GetFileName(virtualPath));
    }
    byte[] fileContent;
    using (var fileStream = new FileStream(@"c:\temp\fileToCopy.txt", FileMode.Open, FileAccess.Read)) {
        fileContent = new byte[fileStream.Length];
        fileStream.Read(fileContent, 0, fileContent.Length);
    }
    // write the content to the local file system
    using (Stream stream = file.Open(FileMode.Create)) {
        stream.Write(fileContent, 0, fileContent.Length);
    }

What I want is that if I switch to my amazon s3 virtual path provider I want this code to work directly without any changes so to sum things up, how can I solve this using the amazon s3 sdk and how should i implement my Open(FileMode fileMode) method in my amazon s3 virtual path provider?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T04:47:03+00:00Added an answer on June 15, 2026 at 4:47 am

    Hey i stood for this problem, too, and i solved it implementing a stream.

    Here is my way i did it maybe it helps:

    public static Stream OpenStream(S3TransferUtility transferUtility, string key)
        {                     
            byte[] buffer  = new byte[Buffersize + Buffersize/2];
    
            S3CopyMemoryStream s3CopyStream =
                new S3CopyMemoryStream(key, buffer, transferUtility)
                .WithS3CopyFileStreamEvent(CreateMultiPartS3Blob);
    
            return s3CopyStream;
        }
    

    My Stream with constructor overrides the close and write(array, offset, count) methods and upload the stream to amazon s3 partly.

    public class S3CopyMemoryStream : MemoryStream
        {
    
            public S3CopyMemoryStream WithS3CopyFileStreamEvent(StartUploadS3CopyFileStreamEvent doing)
            {
                S3CopyMemoryStream s3CopyStream = new S3CopyMemoryStream(this._key, this._buffer, this._transferUtility);
    
                s3CopyStream.StartUploadS3FileStreamEvent = new S3CopyMemoryStream.StartUploadS3CopyFileStreamEvent(CreateMultiPartS3Blob);
    
                return s3CopyStream;
            }
    
            public S3CopyMemoryStream(string key, byte[] buffer, S3TransferUtility transferUtility)
                : base(buffer)
            {
                if (buffer.LongLength > int.MaxValue)
                    throw new ArgumentException("The length of the buffer may not be longer than int.MaxValue", "buffer");
    
                InitiatingPart = true;
                EndOfPart = false;
                WriteCount = 1;
                PartETagCollection = new List<PartETag>();
    
                _buffer = buffer;
                _key = key;
                _transferUtility = transferUtility;
            }
    

    The event StartUploadS3FileStreamEvent invokes a call that initiate, uploadpart and complete the upload.

    Alternatively you could implement a FileStream which is much easier because you can use

    TransferUtilityUploadRequest request =
                new TransferUtilityUploadRequest()
                .WithAutoCloseStream(false).WithBucketName(
                    transferUtility.BucketName)
                    .WithKey(key)
                    .WithPartSize(stream.PartSize)
                    .WithInputStream(stream) as TransferUtilityUploadRequest;
    
            transferUtility.Upload(request);
    

    at the close method of the overriden FileStream. The disadvantage is that you have to write the whole data to the disk first and then you can upload it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background: This is a follow-up question to this thread about handling EINTR for system
Based on this question : How to set emacsclient background as Emacs background? I
For the background to this question, see How to I serialize a large graph
Please refer to this background question. After constructing this COUNT, how would I then
This question came to my mind when I learned C++ with a background of
Background I admit, this question stems from an ultimate lack of deep understanding of
This question is in regards to this blog entry. https://blog.stackoverflow.com/2008/07/easy-background-tasks-in-aspnet/ All the way at
This question is more UI/Design-ish than hard-core programming is. Background: I've been coding in
I'm coming largely from a c++ background, but I think this question applies to
Background (question further down) I've been Googling this back and forth reading RFCs and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.