Golang Api to Upload Image to S3

When your application handles a large number of files, you need to store the files in a certain storage system outside your awarding server. One of the best storage services is Amazon S3. This commodity will show you how to create an Amazon S3 Bucket, create access fundamental id and secret, and upload files to Amazon S3 with Go.

What is Amazon S3

Amazon S3 or Uncomplicated Storage Service is a storage service provided past Amazon AWS. We tin use it to store and protect any corporeality of data for a range of use cases, such equally data lakes, websites, mobile applications, backup and restore, archive, enterprise applications, IoT devices, and big data analytics.
To store data to S3 with a Go awarding, showtime, nosotros need to create the bucket and admission credentials.
If y'all already accept your bucket and credentials, y'all can skip to this section.

Create an S3 bucket

To create your S3 bucket, first, go to the AWS console page. Then search S3 in the search bar. You will exist directed to the S3 dashboard page.
Click Create Saucepan button.

Create S3 Bucket


Input your Bucket name and select your prefered AWS Region. Leave other setting equally is for now.

Input S3 Bucket Name


Then click Create Bucket push button at the lesser of the page. The bucket will be created.

Create IAM to admission the bucket

After the bucket is created, we need to create an access key id and secret then our Go awarding tin can access the bucket. To create the credential, go to the AWS console page, then search IAM in the search bar. So click IAM to go to the IAM dashboard page.
On the IAM dashboard folio, click Users on the left sidebar. So click Add Users.

Create IAM User


Input the User proper noun and cheque the Access key - Programmatic access checkbox. And then click the Next push.

Input IAM User Name


We need to attach S3 access policy to the user.
  • Click Attach existing policies directly.
  • Search S3 in the filter.
  • Then tick AmazonS3FullAccess and click Adjacent push.

    Attach Policy


    You can add together tags if you desire. It is optional.

    Add Tags


    Review the user cosmos to make sure the parameters are correct. Then click Create User.

    Review


    The user is successfully created.

    IAM Created


    Save the Access Key ID and Hugger-mugger admission key. It is needed to access the bucket from Go application.

How to upload file to S3 with Go

Once we accept the bucket and the access key, our Become app tin upload files to the S3. AWS provided official Go SDK to manage S3 (https://github.com/aws/aws-sdk-go/).
To upload a file to S3 we need to create an S3 uploader and call the Upload method of the uploader.
Apply the following code sample to create the uploader.

                                                                        1                                                                          2                                                                          3                                                                          4                                                                          5                                                                          six                                                                          7                                                                          viii                                                                          9                                                10                                                11                                                                          12                                                                                                    13                                                                          xiv                                                fifteen                                                16                                                17                                                                  
                                              import                        (                        // ...                                                                        "github.com/aws/aws-sdk-become/aws"                        "github.com/aws/aws-sdk-go/aws/credentials"                        "github.com/aws/aws-sdk-go/aws/session"                        "github.com/aws/aws-sdk-get/service/s3/s3manager"                        )                        // ...                                                                        s3Config                        :=                        &                        aws.Config{                                                  Region:                          aws.String("ap-southeast-one"),                                                                          Credentials:                          credentials.NewStaticCredentials("KeyID",                          "SecretKey",                          ""),                                                }                        s3Session                        :=                        session.New(s3Config)                        uploader                        :=                        s3manager.NewUploader(s3Session)                                          

On line 11, we create AWS config with our bucket'south region and access key ID and clandestine cardinal of the IAM. This config is needed to create a session for the uploader. Make certain to import all the necessary libraries.
Once the uploader is created, we can phone call the UploadWithContext method of the uploader to upload a file to the S3. Let's meet the sample code below.
                                              ane                                                ii                                                3                                                4                                                5                                                6                                                7                                                                  
                                              input                        :=                        &                        s3manager.UploadInput{                        Bucket:                        aws.String("jajaldoang-test-s3-upload"),                        // saucepan's name                                                                        Key:                        aws.String("myfiles/my_cat.jpg"),                        // files destination location                                                                        Trunk:                        bytes.NewReader(file),                        // content of the file                                                                        ContentType:                        aws.Cord("image/jpg"),                        // content type                                                                        }                        output,                        err                        :=                        uploader.UploadWithContext(context.Groundwork(),                        input)                                          
To upload the file, we need parameter UploadInput which contains the saucepan's name and attributes of the file. The context tin be used to limit upload time and cancelation.

Now attempt to call the function to upload a file and see if it is uploaded in the bucket. Notation that past default, you tin't access the file url because public access is not enabled. To enable information technology, you lot need to modify the bucket policy.

Conclusion

To upload files to Amazon S3, we tin use the official Become SDK provided past AWS. Nosotros need the bucket and access primal ID and cloak-and-dagger central to upload from the Go app. Once we have those, the Go code to upload the file is uncomplicated.

The complete code

The complete code is here (click to expand)
                                                                              1                                                                                2                                                                                three                                                                                4                                                                                5                                                                                6                                                                                7                                                                                viii                                                                                nine                                                    10                                                    xi                                                    12                                                    13                                                    14                                                    fifteen                                                    xvi                                                    17                                                    18                                                    19                                                    20                                                    21                                                    22                                                    23                                                    24                                                    25                                                    26                                                    27                                                    28                                                    29                                                    thirty                                                    31                                                    32                                                    33                                                    34                                                    35                                                    36                                                    37                                                    38                                                    39                                                    40                                                    41                                                    42                                                    43                                                    44                                                    45                                                    46                                                    47                                                    48                                                    49                                                    50                                                    51                                                    52                                                                        
                                                  parcel                          main                          import                          (                          "bytes"                          "context"                          "io/ioutil"                          "log"                          "github.com/aws/aws-sdk-go/aws"                          "github.com/aws/aws-sdk-get/aws/credentials"                          "github.com/aws/aws-sdk-go/aws/session"                          "github.com/aws/aws-sdk-go/service/s3/s3manager"                          )                          var                          uploader                          *                          s3manager.Uploader                          func                          main() {                          uploader                          =                          NewUploader()                          upload() }                          func                          NewUploader()                          *                          s3manager.Uploader                          {                          s3Config                          :=                          &                          aws.Config{                          Region:                          aws.String("ap-southeast-i"),                          Credentials:                          credentials.NewStaticCredentials("KeyID",                          "SecretKey",                          ""), 	}                          s3Session                          :=                          session.New(s3Config)                          uploader                          :=                          s3manager.NewUploader(s3Session)                          return                          uploader                          }                          func                          upload() {                          log.Println("uploading")                          file,                          err                          :=                          ioutil.ReadFile("./my_cat.jpg")                          if                          err                          !=                          nil                          {                          log.Fatal(err) 	}                          upInput                          :=                          &                          s3manager.UploadInput{                          Saucepan:                          aws.Cord("jajaldoang-exam-s3-upload"),                          // bucket'south name                                                                              Key:                          aws.Cord("myfiles/my_cat.jpg"),                          // files destination location                                                                              Torso:                          bytes.NewReader(file),                          // content of the file                                                                              ContentType:                          aws.Cord("paradigm/jpg"),                          // content type                                                                              }                          res,                          err                          :=                          uploader.UploadWithContext(context.Background(),                          upInput)                          log.Printf("res %+v\n",                          res)                          log.Printf("err %+v\n",                          err) }                                              

Encounter also

  • Become: Write Log to a File
  • Monitoring Errors in Jaeger
  • Profiling A Become App With pprof
  • Distributed Tracing with Jaeger in Become
  • Golang Role Timeout With Context

carvajalglinced.blogspot.com

Source: https://www.jajaldoang.com/post/upload-file-to-aws-s3-with-go/

0 Response to "Golang Api to Upload Image to S3"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel