Enterprise Application: Uploading files to Amazon S3 in C#, Java, and Golang

Estimated read time 3 min read

Uploading files to Amazon S3 requires the AWS SDK for the respective programming language. Below are examples of file upload to Amazon S3 using the AWS SDK in C#, Java, and Golang. Please make sure you have the appropriate AWS SDK installed for your language.

C# File Upload to AWS S3:

using Amazon.S3;
using Amazon.S3.Transfer;
using System;
using System.IO;
using System.Threading.Tasks;

class Program
{
    static async Task Main()
    {
        string accessKey = "Your_AWS_Access_Key";
        string secretKey = "Your_AWS_Secret_Key";
        string bucketName = "Your_S3_Bucket_Name";
        string filePath = "Path_to_Your_File";

        var transferUtility = new TransferUtility(accessKey, secretKey, Amazon.RegionEndpoint.USWest2);

        using (var fileStream = new FileStream(filePath, FileMode.Open))
        {
            await transferUtility.UploadAsync(fileStream, bucketName, Path.GetFileName(filePath));
        }

        Console.WriteLine("File uploaded to AWS S3 in C#.");
    }
}

Java File Upload to AWS S3:

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.PutObjectRequest;

import java.io.File;

public class S3FileUploadExample {
    public static void main(String[] args) {
        String accessKey = "Your_AWS_Access_Key";
        String secretKey = "Your_AWS_Secret_Key";
        String bucketName = "Your_S3_Bucket_Name";
        String filePath = "Path_to_Your_File";

        BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);

        AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                .withCredentials(new AWSStaticCredentialsProvider(credentials))
                .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("s3.amazonaws.com", "us-west-2"))
                .build();

        s3Client.putObject(new PutObjectRequest(bucketName, new File(filePath).getName(), new File(filePath)));

        System.out.println("File uploaded to AWS S3 in Java.");
    }
}

Golang (Go) File Upload to AWS S3:

package main

import (
    "fmt"
    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/aws/credentials"
    "github.com/aws/aws-sdk-go/aws/session"
    "github.com/aws/aws-sdk-go/service/s3"
    "os"
)

func main() {
    accessKey := "Your_AWS_Access_Key"
    secretKey := "Your_AWS_Secret_Key"
    bucketName := "Your_S3_Bucket_Name"
    filePath := "Path_to_Your_File"

    sess, err := session.NewSession(&aws.Config{
        Region:      aws.String("us-west-2"),
        Credentials: credentials.NewStaticCredentials(accessKey, secretKey, ""),
    })

    if err != nil {
        fmt.Println("Error creating session:", err)
        return
    }

    file, err := os.Open(filePath)
    if err != nil {
        fmt.Println("Error opening file:", err)
        return
    }
    defer file.Close()

    s3Client := s3.New(sess)

    _, err = s3Client.PutObject(&s3.PutObjectInput{
        Bucket: aws.String(bucketName),
        Key:    aws.String(file.Name()),
        Body:   file,
    })
    if err != nil {
        fmt.Println("Error uploading file to AWS S3:", err)
        return
    }

    fmt.Println("File uploaded to AWS S3 in Golang.")
}

Remember to replace placeholders like “Your_AWS_Access_Key”, “Your_AWS_Secret_Key”, “Your_S3_Bucket_Name”, and “Path_to_Your_File” with your actual AWS credentials, S3 bucket name, and the local file path you want to upload. Additionally, ensure you have the necessary permissions configured in your AWS account.

Related Articles