Click here to Skip to main content
15,867,704 members
Articles / Programming Languages / C#
Article

NETS3

Rate me:
Please Sign up or sign in to vote.
4.75/5 (4 votes)
26 Mar 2012CPOL4 min read 22.9K   564   12   2
NETS3 is an Open Source tool built using .NET technology for accessing Amazon S3 content.

Image 1

1.0 Introduction

NETS3 is an Open Source application built using the Microsoft .NET technology. This application provides access to S3 contents in an easy and user friendly manner. Users are free to use the application as well as the source code as per their needs/ requirements. Feel free to report any changes, feedback, or bugs at ak.tripathi@yahoo.com with subject line NETS3.

2.0 Background

Nowadays Amazon Cloud has become a very popular place for storage. Storing any content on the cloud helps us overcome data management issues. This application may be used for easier access to the cloud and for basic learning about Amazon S3.

3.0 Using the code

  • Download NETS3 source code
  • Search and double click the NETS3.sln file
  • Hit the F5 button to run as well as debug the application

3.1 How to get all the buckets

C#
private DataTable GetAllBuckets()
{
    DataTable buckets = new DataTable();
    try
    {
        using (AmazonS3Client _s3Client = 
               new AmazonS3Client(SessionManager.AccessKeyId, 
               SessionManager.SecretAccessKeyId))
        {
            ListBucketsResponse response = _s3Client.ListBuckets();
            buckets.Columns.Add(new DataColumn("Name"));
            buckets.Columns.Add(new DataColumn("CreationDate"));
            DataRow row;
            //Loop through the records obtained to create a data table 
            foreach (S3Bucket bucket in response.Buckets)
            {
                row = buckets.NewRow();
                row[0] = bucket.BucketName;
                row[1] = string.Format("{0:dd-MMM-yyyy hh:mm:ss}", 
                         Convert.ToDateTime( bucket.CreationDate));
                buckets.Rows.Add(row);
            }
        }
    }
    catch (Exception err)
    {
        MessageBox.Show("Error Occured" + Environment.NewLine + err.Message);
    }
    return buckets;
}

Image 2

3.2 How to get the content(s) of a bucket

C#
using (AmazonS3Client _s3Client = 
   new AmazonS3Client(SessionManager.AccessKeyId, 
   SessionManager.SecretAccessKeyId))
{
    ListObjectsRequest listObjectReq = new ListObjectsRequest();
    listObjectReq.WithBucketName(bucketName);

    ListObjectsResponse resp = client.ListObjects(listObjectReq);
    data.Columns.Add(new DataColumn("Key"));
    data.Columns.Add(new DataColumn("LastModified"));
    data.Columns.Add(new DataColumn("ETag"));
    data.Columns.Add(new DataColumn("Owner"));
    data.Columns.Add(new DataColumn("Size"));
    data.Columns.Add(new DataColumn("StorageClass"));

    DataRow  row;

    //Loop through the objects obtained to create a data table 
    foreach(S3Object  obj in resp.S3Objects)
    {
        row = data.NewRow();
        row["Key"] = obj.Key;
        row["LastModified"] = string.Format("{0:dd-MMM-yyyy hh:mm:ss}", 
             Convert.ToDateTime( obj.LastModified));
        row["ETag"] = obj.ETag;
        row["Owner"] = obj.Owner != null ? obj.Owner.DisplayName : "";
        row["Size"] = obj.Size.ToString();
        row["StorageClass"] = obj.StorageClass;
        data.Rows.Add(row);
    }
}

3.3 How to create a new bucket

C#
using (AmazonS3Client _s3Client = 
         new AmazonS3Client(SessionManager.AccessKeyId, SessionManager.SecretAccessKeyId))
{
    PutBucketResponse response = _s3Client.PutBucket(
          new PutBucketRequest().WithBucketName(bucketName));
}

3.4 How to upload content to a bucket

C#
using (AmazonS3Client s3Client = new AmazonS3Client(
        SessionManager.AccessKeyId, SessionManager.SecretAccessKeyId))
{
    //Create a unique id for the file 
    string fileName = "C:\Temp\SomeImage.png";
    string key = Guid.NewGuid().ToString() + Path.GetExtension(fileName);
    
     PutObjectRequest request = new PutObjectRequest();
    request.WithBucketName(bucketName)
    .WithCannedACL(S3CannedACL.PublicRead)
    .WithKey(key).InputStream = StreamFile(fileName);

    S3Response response = s3Client.PutObject(request);
                  
}

private Stream StreamFile(string fileName)
{
    Byte[] byteArray = File.ReadAllBytes(fileName);            
    MemoryStream mStream = new MemoryStream();
    mStream.Write(byteArray, 0, byteArray.Length);
    return mStream;
}

3.5 How to download content from a bucket

C#
private bool DownloadFile(string bucketName, string key)
{
    string destination = "C:\Temp\";

     if (!Directory.Exists(destination))
                Directory.CreateDirectory(destination);

    using (AmazonS3 _s3Client = Amazon.AWSClientFactory.CreateAmazonS3Client(
                    SessionManager.AccessKeyId, SessionManager.SecretAccessKeyId))
    {
        GetObjectRequest getObjectRequest = 
           new GetObjectRequest().WithBucketName(bucketName).WithKey(key);

        using (S3Response getObjectResponse = client.GetObject(getObjectRequest))
        {
            using (Stream s = getObjectResponse.ResponseStream)
            {
                using (FileStream fs = new FileStream( Path.Combine(destination, _fileName), 
                                       FileMode.Create, FileAccess.Write))
                {
                    byte[] data = new byte[32768];
                    int bytesRead = 0;
                    do
                    {
                        bytesRead = s.Read(data, 0, data.Length);
                        fs.Write(data, 0, bytesRead);
                    }
                    while (bytesRead > 0);
                    fs.Flush();
                }
            }
        }             
    }
}

3.6 How to delete content from a bucket

C#
private bool DeleteFile(string fileName, string bucketName)
{
    bool isSuccess = false;
    try
    {
        using (AmazonS3 _s3Client = 
            Amazon.AWSClientFactory.CreateAmazonS3Client(
            SessionManager.AccessKeyId, SessionManager.SecretAccessKeyId))
        {
            DeleteObjectRequest request = new DeleteObjectRequest();
            request.WithBucketName(bucketName).WithKey(fileName);
            _s3Client.DeleteObject(request);
        }
         isSuccess = true;
    }
    catch (AmazonS3Exception amazonS3Exception)
    {
        MessageBox.Show("Error occured-" + Environment.NewLine + "Error Code: " + 
          amazonS3Exception.ErrorCode + Environment.NewLine + "Message: " + amazonS3Exception.Message);
    }

    return isSuccess;
}

3.7 How to generate the public URL of a content

C#
private string GetPublicUrl(int expirationTime, string bucketName, 
               string fileName, bool generateSecuredUrl)
{
    string publicUrl = "";
    using (AmazonS3 _s3Client = Amazon.AWSClientFactory.CreateAmazonS3Client(
                    SessionManager.AccessKeyId, SessionManager.SecretAccessKeyId))
    {
        GetPreSignedUrlRequest request = new GetPreSignedUrlRequest();
        DateTime Expiration = DateTime.Now.AddHours(expirationTime);
        request.WithBucketName(bucketName).WithKey(fileName).WithExpires(
          Expiration).WithProtocol(generateSecuredUrl ? Protocol.HTTPS : Protocol.HTTP);
        publicUrl = client.GetPreSignedURL(request);
    }    
    return publicUrl;
}

4.0 User guide

1.0 Introduction

NETS3 is an Open Source application built using the Microsoft .NET technology. This application provides access to S3 contents in an easy and user friendly manner.

2.0 Technology details

2.1 Prerequisites

Microsoft .NET Framework 3.5 or greater.

2.2 How to use

Follow the steps below to use "NETS3".

  1. Download the files
  2. Search for the directory executable
  3. Search for the file NETS3.exe and double click it to run

3.0 User Manual

3.1 Login

Run NETS3.exe.

Image 3

Enter Access Key Id and Secret Access Key Id and hit the Login button. Successful login will redirect you to the NETS3 xonsole, where you will see your buckets listing and can manipulate them.

Image 4

3.2 How to create a new bucket

  • 3.2.1 Once you are logged in to the NETS3 Console, hit CTRL+N.
  • Image 5

    Enter the name of the bucket you want to create and hit on the Create button.

  • 3.2.2 You will receive a message box,
  • Image 6

3.3 How to upload the content to a bucket

  • 3.3.1 Right click on the bucket to which you want to upload the content.
  • Image 7

  • 3.3.2 A context menu will appear. Click (left click) on “Upload Content”. This will bring a new window to upload any file/content.
  • Image 8

  • 3.3.3 Browse the file you wish to upload. You will see the browsed files in the Selected File(s) grid with add mark.
  • 3.3.4 Click on the “Upload” button to upload the selected content to Amazon S3.

3.4 How to delete content from a bucket

  • 3.4.1 Click on the bucket from the buckets tree from which you wish to delete content. You will see the files belonging to the bucket on the right side grid.
  • Image 9

  • 3.4.2 Right click on the file/ content you wish to delete.
  • 3.4.3 Click on the “Delete” menu to delete the content.

3.5 How to download content from a bucket

  • 3.5.1 Click on the bucket from the buckets tree in the left side, from which you wish to download content. You will see the files belonging to the bucket on the right.
  • Image 10

  • 3.5.2 Right click on the file/ content you wish to dDownload.
  • 3.5.3 Click on the “Download” menu to download the content. By default, the files are downloaded on to your desktop within a folder “NETS3Downloads”. You may change this location. See section 3.7 to know how this path is changed.

3.6 How to generate the URL of any content/ file

  • 3.6.1 Click on the bucket from the buckets tree present in the left, from which you wish to share content or file. You will see the files belonging to the bucket on the right side.
  • Image 11

  • 3.6.2 Right click on the file/ content you wish to share.
  • 3.6.3 Click on the “Generate URL” menu to generate the URL to share the content or file.
  • Image 12

  • 3.6.4 You may generate the secured URL as per your requirement by varying the Expiration Time, choosing HTTP, etc. The URL may be given to anyone to access the contents for a certain time.

3.7 How to change the file download location

  • 3.7.1 Go to Settings >> Preferences
  • Image 13

  • 3.7.2 Browse the location where you wish to save the files downloaded.

4.0 Feedback/ Bug reporting

Feel free to write your suggestions, feedback, user experiences to ak.tripathi@yahoo.com with subject line NETS3.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder Aspirea Technologies Pvt Ltd
India India
• 8 years of experience in IT Industry as a Developer.
• Experience of End-To-End Software Development and Implementation (Entire SDLC i.e Software Development Life Cycle)
• Real time Exposure to Banking, Finance and Energy industry.
• Expertise in distributed application architecture as well as web based applications using Microsoft.NET platform.
• Expertise in database design, SQL programming and SQL performance tuning.
• Expertise in Web Services and WCF Services.
• Experience of Rich Internet Application using Adobe Flex.
• Experience in migration of legacy application to latest technology, migration of VB application to .NET.
• Knowledge of OOPS and Design Concepts.
• Expertise in Agile/ Scrum software development processes.

Specialties
• Languages\ Technologies-
.NET Framework 1.1/2.0/3.0/3.5, C#.NET, VB.NET, ASP.NET, VB6, AJAX, ASP.NET, Adobe Flex 3.0, Web Services, Windows Communication Foundation (WCF), LINQ, SQL Server, Oracle, MySql, MS Access, HTML, XML, JavaScript, C# Script, CSS and XSLT.

• Methodology/ Concepts-
OOPS, Data Structures, Design Concepts and Agile/ Scrum Software Development

Comments and Discussions

 
QuestionAnonymous access is forbidden for this operation Pin
phoenicyan19-Jun-13 9:00
phoenicyan19-Jun-13 9:00 
GeneralMy vote of 5 Pin
kiraniru26-Mar-12 21:06
kiraniru26-Mar-12 21:06 
Excellent app

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.