Convert this node js code to golang

‘use strict’

const AWS = require(‘aws-sdk’)
AWS.config.update({ region: process.env.AWS_REGION })
const s3 = new AWS.S3()

// Change this value to adjust the signed URL’s expiration
const URL_EXPIRATION_SECONDS = 300

// Main Lambda entry point
exports.handler = async (event) => {
return await getUploadURL(event)
}

const getUploadURL = async function(event) {
const randomID = parseInt(Math.random() * 10000000)
const Key = ${randomID}.jpg

// Get signed URL from S3
const s3Params = {
Bucket: process.env.UploadBucket,
Key,
Expires: URL_EXPIRATION_SECONDS,
ContentType: ‘image/jpeg’,

// This ACL makes the uploaded object publicly readable. You must also uncomment
// the extra permission for the Lambda function in the SAM template.

// ACL: 'public-read'

}

console.log('Params: ', s3Params)
const uploadURL = await s3.getSignedUrlPromise(‘putObject’, s3Params)

return JSON.stringify({
uploadURL: uploadURL,
Key
})
}

Hello, Abhishek Kumar Gupta, and welcome to the forum!

It is unlikely that anyone on this site will translate your entire node program into Go and give it to you. There are other web sites where you can pay people to do that for you, but the goal of the members on this site is to help guide you so you can solve the problem (mostly) yourself.

With that goal in mind, at this time, my suggestions to you are:

  1. Read the documentation for the AWS SDK for Go.
  2. Check out the examples.
  3. If you have any specific questions (e.g.: “How do I set up a Lambda entry point in Go?” or “How do I specify an S3 upload timeout in Go?”, etc.), feel free to ask them here.
1 Like

Great advice. One more resource that will be useful: it looks like that node.js code is reading an environment variable for region. Check this go by example for a succinct example on how to do that in go:

https://gobyexample.com/environment-variables

Though I’m honestly not sure if it’s needed for AWS_REGION - since the AWS SDK will use that environment variable anyway so there’s no need to set it in the config. But for UploadBucket it might come in handy.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.