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.
Input your
Bucket name
and select your prefered AWS Region
. Leave other setting equally is for now.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
.
Input the User proper noun and cheque the
Access key - Programmatic access
checkbox. And then click the Next
push.We need to attach S3 access policy to the user.
- Click
Attach existing policies directly
. - Search S3 in the filter.
- Then tick
AmazonS3FullAccess
and clickAdjacent
push.
You can add together tags if you desire. It is optional.
Review the user cosmos to make sure the parameters are correct. Then clickCreate User
.
The user is successfully created.
Save theAccess Key ID
andHugger-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.
| |
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. | |
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)
| |
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
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