ThreeSharp and creating an empty folder

I’m building a web based S3 explorer/media manager for a work project as well as my DizCollect project. It is being written in .NET/C# ASP.NET MVC and I had a special need — creating empty folders for users to upload files to. Since I don’t store the files locally, I couldn’t just upload new files and have the folders build out.

I use the wonderful C# S3 library ThreeSharp.

The one thing it does not do out of the box is allow you to just create an empty folder. My behavior will be that a user clicks on a “new folder” button and is presented a text box. They enter the name of the new folder and it is created in the current location they are browsing in my S3 explorer. To get ThreeSharp to do this, you must add a content_type of  “binary/octet-stream” and specify the folder with a trailing ‘/’. The ‘/’ is very important and is the difference between a folder or a file being created.

I hope this helps someone. It took me several hours to figure this out and I could not find a good example online anywhere.

Here is my extension to the ThreeSharpWrapper class to create an empty folder

/// <summary>
/// Adds a folder (empty) to a bucket
/// </summary>
public void AddEmptyFolder(String bucketName, String keyName) {
using (ObjectAddRequest objectAddRequest = new ObjectAddRequest(bucketName, keyName))
{
objectAddRequest.QueryList.Add("content_type", "binary/octet-stream");
using (ObjectAddResponse objectAddResponse = this.service.ObjectAdd(objectAddRequest)) { }
}
}

then to call the function you would use:

wrapper.AddEmptyFolder("myBucket", "test/");  //@params: (str)bucketName, (str)folderName
  1. thanks ! that’s exactly what I was looking for :)

  2. hi,

    You wouldn’t happen to know how to upload a folder with its content (sub-folders + files) by any chance ? Or do I have to recreate the folder structure on S3 and upload files one by one ?

  3. You cannot do it in one big function call unfortunately. You’d have to create a recursive function that could read your directory structure and duplicate the folders and then copy the folders up.

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>