I need to get public permanent (not signed) URL of a resource using golang and official aws go sdk. In Java AWS S3 SDK there's a method called getResourceUrl() what's the equivalent in go?
This is how you get presigned URLs using the go sdk:
func GetFileLink(key string) (string, error) {
svc := s3.New(some params)
params := &s3.GetObjectInput{
Bucket: aws.String(a bucket name),
Key: aws.String(key),
}
req, _ := svc.GetObjectRequest(params)
url, err := req.Presign(15 * time.Minute) // Set link expiration time
if err != nil {
global.Log("[AWS GET LINK]:", params, err)
}
return url, err
}
If what you want is just the URL of a public access object you can build the URL yourself:
https://<region>.amazonaws.com/<bucket-name>/<key>
Where <region> is something like us-east-2. So using go it will be something like:
url := "https://%s.amazonaws.com/%s/%s"
url = fmt.Sprintf(url, "us-east-2", "my-bucket-name", "some-file.txt")
Here is a list of all the available regions for S3.
Looks almost clean:
import "github.com/aws/aws-sdk-go/private/protocol/rest"
...
params := &s3.GetObjectInput{
Bucket: aws.String(a bucket name),
Key: aws.String(key),
}
req, _ := svc.GetObjectRequest(params)
rest.Build(req) // aws method to build URL in request object
url = req.HTTPRequest.URL.String() // complete URL to resource
Related
I am trying to upload base64 encoded string to aws s3 bucket in the form of PNG image. I had tried following code:
import (
awsconfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/feature/s3/manager"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
signature:= "iVBORw0KGgoAAAANSUhEUgAAAfQAAAC0CAYAAABi+d5SAAAAAXNSR0IArs4c6QAAHANJREFUeF7tnWusdkdZhm+kVUSxYg+2klirFWMrrdD+wPKjEg+QCtQayw8PqYApQUPaRhJDhBQUEyJqDyHGeihtQI1oLI02WklBYrWgrWBaRKxQEKgoLWjVWA5ac/utRd9v9333embeWWvNrHVNsrP39+1nZs1cM+++1zwz88zjRIIABCAAAQhAoHkCj2u+BTQAAhCAAAQgAAEh6AwCCEAAAhCAwAIIIOgL6ESaAAEIQAACEEDQGQMQgAAEIACBBRBA0BfQiTQBAhCAAAQggKAzBiAAAQhAAAILIICgL6ATa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HjdyQQACEIAABKoigKBX1R1UBgIQgAAEIJBHAEHP40YuCEAAAhCAQFUEEPSquoPKQAACEIAABPIIIOh53MgFAQhAAAIQqIoAgl5Vd1AZCEAAAhCAQB4BBD2PG7kgAAEIQAACVRFA0KvqDioDAQhAAAIQyCOAoOdxIxcEIAABCECgKgIIelXdQWUgAAEIQAACeQQQ9Dxu5IIABCAAAQhURQBBr6o7qAwEIAABCEAgjwCCnseNXBCAAAQgAIGqCCDoVXUHlYEABCAAAQjkEUDQ87iRCwIQgAAEIFAVAQS9qu6gMhCAAAQgAIE8Agh6HjdyQQACEIAABKoigKBX1R1UBgIQgAAEIJBHAEHP40YuCEAAAhCAQFUEEPSquoPKQAACEIAABPIIIOh53MgFAQhAAAIQqIoAgl5Vd1AZCEAAAhCAQB4BBD2PG7kgAAEIQAACVRFA0KvqDioDAQhAAAIQyCOAoOdxIxcEIAABCECgKgIIelXdQWUgAAEIQAACeQQQ9Dxu5IIABCAAAQhURQBBr6o7qAwEIAABCEAgjwCCnseNXBCAAAQgAIGqCCDoVXUHlYEABCAAAQjkEUDQ87iRCwIQgAAEIFAVAQS9qu6gMhCAAAQgAIE8Agh6HjdyQQACEIAABKoigKBX1R1UBgIQgAAEIJBH4P8AbePq4jPqwCcAAAAASUVORK5CYII="
decodedSignature, decodeErr := base64.StdEncoding.DecodeString(signature)
if decodeErr != nil {
// handle error here
}
r := bytes.NewReader(decodedSignature)
bucketName := "test"
filepath := "uploads"
contentType := "image/png"
cfg, err := awsconfig.LoadDefaultConfig(context.TODO(),
awsconfig.WithRegion(config.AwsRegion),
)
client := s3.NewFromConfig(cfg)
// Create an uploader with the client and options
uploader := manager.NewUploader(client)
cacheExpireTime := time.Now().AddDate(1, 0, 0)
cacheMaxAgeSecs := "31556926" // seconds in 1 year
// Upload the file to S3.
uploadResp, err := uploader.Upload(context.TODO(), &s3.PutObjectInput{
Bucket: aws.String(bucketName),
Key: aws.String(filePath),
Body: r,
ContentType: aws.String(contentType),
Expires: &cacheExpireTime,
CacheControl: aws.String(cacheMaxAgeSecs),
})
But when I try to load the s3 url on which the image is uploaded, then there is black screen as shown in the screenshot:
The Body parameter in the uploader.Upload function accepts io.Reader objects as body, meaning that you either need to use os.Open to open a png you have saved, or in this case, use bytes.NewReader(data) to make your data conform to the io.Reader interface (have the .Read() method).
I have created REST API using Go Iris framework. Now I want to deploy these API's on AWS with lambda function. I am using MySQL as database. Is it possible to deploy my Go executable file on AWS lambda or should I need to modify my code according to AWS lambda specifications? I am trying to find the solution, but not getting much information.
Here is one of my API end point.
package main
import (
"database/sql"
"github.com/kataras/iris"
"github.com/kataras/iris/middleware/logger"
"github.com/kataras/iris/middleware/recover"
)
type Reward struct {
Id int `json:"reward_id"`
LotteryID int `json:"lottery_id"`
RewardName string `json:"reward_name"`
Description string `json:"reward_description"`
Asset int `json:"reward_asset"`
AssetName string `json:"reward_asset_name"`
}
func dbConn() (db *sql.DB) {
dbDriver := "mysql"
dbUser := "xxx"
dbPass := "xxx"
dbName := "xxx"
db, err := sql.Open(xxxxxxxxx)
if err != nil {
panic(err.Error())
}
return db
}
func newApp() *iris.Application {
app := iris.New()
app.Logger().SetLevel("debug")
app.Use(recover.New())
app.Use(logger.New())
db := dbConn()
app.Get("/reward/{reward_id:int}", func(ctx iris.Context) {
id1 := ctx.Params().GetIntDefault("reward_id", 0)
stmtOut, err := db.Prepare("select id, lottery_id,reward_name,reward_description,reward_asset, reward_asset_name from rewards_table where id =?")
if err != nil {
panic(err.Error())
}
defer stmtOut.Close()
var id, lotteryId, rewardAsset int
var rewardName, rewardDescription, rewardAssetName string
err1 := stmtOut.QueryRow(id1).Scan(&id, &lotteryId, &rewardName, &rewardDescription, &rewardAsset, &rewardAssetName)
if err1 != nil {
panic(err.Error())
}
reward := Reward{
Id: id,
LotteryID: lotteryId,
RewardName: rewardName,
Description: rewardDescription,
Asset: rewardAsset,
AssetName: rewardAssetName,
}
ctx.JSON(&reward)
})
return app
}
func main() {
app := newApp()
app.Run(iris.Addr(":8080"), iris.WithoutServerError(iris.ErrServerClosed), iris.WithOptimizations)
}
I have few more API endpoints which do basic CRUD operations. I am thinking about using AWS lambda and AWS API Gateway.
should I need to modify my code according to AWS lambda specifications?
Yes. Your code for lambda will require to have a handler:
AWS Lambda function handler in Go
This is the entry point to your function.
Also it seems that your go program is a web server build on iris. If this is the case, you won't be able to use it anyway, as you can't invoke lambda from internet as you would a regular server.
Also lambda runs for max 15 minutes, thus its use as a server would be very limited.
Whenever I try to generate a Presign URL using AWS API I get something like this:
https://{OBJECT_PATH}?X-Amz-Algorithm=AWS4-HMAC-SHA256\\u0026X-Amz-Credential=AKIAJMALZY6GKVGFVOCQ%2F20191003%2Fus-east-1%2Fs3%2Faws4_request\\u0026X-Amz-Date=20191003T111419Z\\u0026X-Amz-Expires=300\\u0026X-Amz-SignedHeaders=host\\u0026X-Amz-Signature=c72a66c249fe2dada4c3925388efc62cbc734c84e01ffbf4748a5f6be9a99026\
When I try to enter it, I get the following message:
<Error>
<Code>AuthorizationQueryParametersError</Code>
<Message>X-Amz-Algorithm only supports "AWS4-HMAC-SHA256"</Message
<RequestId>7236615A79C34A4B</RequestId>
<HostId>PHoX1otuX6apJrXRTOMM/8GvgiajrNPdElrnfSzAZCzpLzMG8NFu9RIJEiyEYHWAls91n882QNE=</HostId>
</Error>
I tried to replace \\u0026 with & because I saw on documentation (https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-query-string-auth.html) that this is how presign URL should be structured, I get:
<Error>
<Code>SignatureDoesNotMatch</Code>
<Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message>
<AWSAccessKeyId>AKIAJMALZY6GKVGFVOCQ</AWSAccessKeyId>
<StringToSign>AWS4-HMAC-SHA256
20191003T112220Z
20191003/us-east-1/s3/aws4_request
a1d8f89ff0472249afb8f1320314eef950bcef423f05fe98420f53cc7f7d8e8f</StringToSign>
<SignatureProvided>a1b2b7080c67e6456cbd2ab678565e5f3857483378e69ca03e35cc83232b2844\</SignatureProvided>
<StringToSignBytes>{SEQUENCE_OF_BYTES}</StringToSignBytes>
<CanonicalRequest>GET/{OBJECT_KEY}?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAJMALZY6GKVGFVOCQ%2F20191003%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20191003T112220Z&X-Amz-Expires=300&X-Amz-SignedHeaders=host host:{BUCKET_NAME} host UNSIGNED-PAYLOAD</CanonicalRequest>
<CanonicalRequestBytes>{SEQUENCE_OF_BYTES}</CanonicalRequestBytes>
<RequestId>F3A0303ADD62D8FD</RequestId
<HostId>yU7FdXLoEO+j+KpZtQH1YjF6l3RnVugBi6rzCRJOgVYk7FEQqreobMuFdSiYtzIKUgl0Qr2GcAQ=</HostId>
</Error>
Finally, here is the code I use in order to generate these URLs:
var accessableURLs []string
for _, fileName := range fileNames {
objectKey := filepath.Join(fileDir, fileName)
req, _ := s3Obj.Client.GetObjectRequest(&s3.GetObjectInput{
Bucket: aws.String(s3Obj.Bucket),
Key: aws.String(objectKey),
})
urlStr, err := req.Presign(expireAfter)
if err != nil {
glog.Errorf("Failed during generating pre-signed S3 url, reason -%v ", err)
return nil, err
}
accessableURLs = append(accessableURLs, urlStr)
}
The following works with the v1 aws sdk. I'm not sure why your solution fails:
package main
import (
"fmt"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
func main() {
MyBucketName := "myAwesomeBucket"
MyKeyName := "/path/filename.txt"
days := 5
session, err := session.NewSession(&aws.Config{
Region: aws.String("us-east-1")},
)
s := s3.New(session)
r, _ := s.GetObjectRequest(&s3.GetObjectInput{
Bucket: aws.String(MyBucketName),
Key: aws.String(MyKeyName),
})
url, err := r.Presign(time.Duration(days) * 24 * time.Hour)
if err != nil {
fmt.Println("failure")
}
fmt.Println(url)
}
One thing to note is that the IAM creds used to sign the url must be valid for as long as the signed url is intended to be valid for. This ends up being an issue with ec2 machines that use temporary creds. A workaround is for the application to have a permanent IAM cred that it uses to sign the url with.
I'd like to upload files to my s3 bucket via the aws golang sdk. I have a web server listening to POST requests and I'm expecting to receive multiple files of any type.
Using the sdk, the s3 struct PutObjectInput expects Body to be of type io.ReadSeeker and I'm not sure how to extract the content from the files uploaded and in turn satisfy the io.ReadSeeker interface.
images := r.MultipartForm.File
for _, files := range images {
for _, f := range files {
# In my handler, I can loop over the files
# and see the content
fmt.Println(f.Header)
_, err = svc.PutObjectWithContext(ctx, &s3.PutObjectInput{
Bucket: aws.String("bucket"),
Key: aws.String("key"),
Body: FILE_CONTENT_HERE,
})
}
}
Use the FileHeader.Open method to get an io.ReadSeeker.
f, err := f.Open()
if err != nil {
// handle error
}
_, err = svc.PutObjectWithContext(ctx, &s3.PutObjectInput{
Bucket: aws.String("bucket"),
Key: aws.String("key"),
Body: f,
})
Open returns a File. This type satisfies the io.ReadSeeker interface.
Use the S3 Manager's Uploader.Upload method, http://docs.aws.amazon.com/sdk-for-go/api/service/s3/s3manager/#Uploader.Upload. We have an example at http://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/s3-example-basic-bucket-operations.html#s3-examples-bucket-ops-upload-file-to-bucket.
I am working in Golang,now I am attempting to upload an image to AWS S3, but I get:
NoCredentialProviders: no valid providers in chain. Deprecated.
For verbose messaging see aws.Config.CredentialsChainVerboseErrors
My code is like this:
func firstFunction(){
//Connect to S3
AWSsession, err := ConnectAWS()
if err != nil {
fmt.Println("Error Connecting to AWS S3")
}
GetSingleMedia(AWSsession)
}
func ConnectAWS()(*session.Session, error){
//Create S3 Session
AWSsession, err := session.NewSession(&aws.Config{
Region: aws.String("us-west-2")},
)
if err != nil {
fmt.Println("Error AWS:", err.Error())
}
return AWSsession,err
}
func GetSingleMedia(...someparams,AWSsession *session.Session){
//o.Blob is correct, this is valid
data, err := ioutil.ReadAll(bytes.NewReader(o.Blob))
//Store: bytes.NewReader(o.Blob)
UploadImage(AWSsession,bytes.NewReader(o.Blob),bucket,"SomeID")
}
func UploadImage(AWSsession *session.Session,reader *bytes.Reader,bucket string, key string) (*s3manager.UploadOutput,error){
uploader := s3manager.NewUploader(AWSsession)
result, err := uploader.Upload(&s3manager.UploadInput{
Body : reader,
Bucket: aws.String(bucket),
Key : aws.String(key),
})
if err != nil {
fmt.Println("Error uploagin img: ",err.Error())
}
return result,err
}
Also, I have placed the creentials under /home/myuser/.aws/ there's a credential file, I don't get any error on creating the session, then, what could be the problem?
The error is triggered in UploadImage
EDIT:
Currently in the credentials file I have:
[default]
awsBucket = "someBucket"
awsAccessKey = "SOME_ACCESS_KEY"
awsSecretKey = "SOME_AWS_SECRET_KEY"
Sould I change any permission or something?
I would suggest you follow the guide here: http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html
This command will ask you for access/secret key and write them in a correct format:
aws configure
It would appear you have a wrong format of credentials file. The correct format would be something like this:
[default]
aws_access_key_id = SOME_ACCESS_KEY
aws_secret_access_key = SOME_AWS_SECRET_KEY