read/write files in aws

mikha
mikha used Ask the Experts™
on
To get started with aws, I have created a s3 bucket , created a index html and few other pages to test my static web pages. I also created a EC2 instance. I'm writing an web application in .net core , where I want to read some input files (XML or Json ), and do some processing write the output and store those files back in S3 bucket.

How do I read/write files in the S3 bucket?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Architect - Coder - Mentor
Commented:
Hi,
First of all, you need to allow read and write access to objects:
https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_examples_s3_rw-bucket.html

Next, you can use the following helper package http://www.chilkatsoft.com/downloads_DotNet.asp
Samples for different tasks: https://www.example-code.com/csharp/s3.asp
AWS manage a formal .NET sdk, find it here https://aws.amazon.com/sdk-for-net/

You will have to install the library via Nuget, and the AWS excellent documentation site has plenty examples.
This is a link to an example writing an object to S3.
https://docs.aws.amazon.com/AmazonS3/latest/dev/UploadObjSingleOpNET.html

This example will create 2 objects in S3 in Ireland (eu-west-1). One will be a simple object creation, and the other will also add metadata to the object.
using Amazon.S3;
using Amazon.S3.Model;
using System;
using System.Threading.Tasks;

namespace Amazon.DocSamples.S3
{
    class UploadObjectTest
    {
        private const string bucketName = "*** bucket name ***";
        // For simplicity the example creates two objects from the same file.
        // You specify key names for these objects.
        private const string keyName1 = "*** key name for first object created ***";
        private const string keyName2 = "*** key name for second object created ***";
        private const string filePath = @"*** file path ***";
        private static readonly RegionEndpoint bucketRegion = RegionEndpoint.EUWest1; 

        private static IAmazonS3 client;

        public static void Main()
        {
            client = new AmazonS3Client(bucketRegion);
            WritingAnObjectAsync().Wait();
        }

        static async Task WritingAnObjectAsync()
        {
            try
            {
                // 1. Put object-specify only key name for the new object.
                var putRequest1 = new PutObjectRequest
                {
                    BucketName = bucketName,
                    Key = keyName1,
                    ContentBody = "sample text"
                };

                PutObjectResponse response1 = await client.PutObjectAsync(putRequest1);

                // 2. Put the object-set ContentType and add metadata.
                var putRequest2 = new PutObjectRequest
                {
                    BucketName = bucketName,
                    Key = keyName2,
                    FilePath = filePath,
                    ContentType = "text/plain"
                };
                putRequest2.Metadata.Add("x-amz-meta-title", "someTitle");
            }
            catch (AmazonS3Exception e)
            {
                Console.WriteLine(
                        "Error encountered ***. Message:'{0}' when writing an object"
                        , e.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine(
                    "Unknown encountered on server. Message:'{0}' when writing an object"
                    , e.Message);
            }
        }
    }
}

Open in new window

Author

Commented:
Thank you both for your helpful comments. I'm still going through the documentation.
out of curiosity, I saw developer guides for .net, java, php . I was wondering if aws has support or developer kit for other languages too.
Eduard GherguArchitect - Coder - Mentor

Commented:
Hi,
As long as AWS services are API based, as long as the requirements are fulfilled, you can use any programming language to access it.
Of course. AWS supports multiple languages.

Check out the AWS tools minisite for C++, Go, Java, Javascript, .NET, Node.js, PHP, Python, and Ruby  https://aws.amazon.com/tools/
Perl sdk - https://github.com/pplu/aws-sdk-perl

And of course, like Eduard said, at the end it is all API driven, so you can develop code in RPGLE to run on AS400 servers, if you so insist.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial