2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

golangでPre-Signed URL(署名付きURL)を生成する

Last updated at Posted at 2020-09-26

GolangでPreSignedUrlを発行するコードを書いたので残しておきます。

package main

import (
	"context"
	"fmt"
	"time"
    "os"


	"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"
)

	// S3の接続情報
	accessKey := os.Getenv("AWS_ACCSESS_KEY")
	secretKey := os.Getenv("AWS_SECRET_KEY")
	region := aws.String(os.Getenv("CUEE_DB_REGION"))

	creds := credentials.NewStaticCredentials(accsessKey, secretKey, "")
	session := session.Must(session.NewSession(&aws.Config{
		Credentials: creds,
		Region:      region,
	}))
	svc := s3.New(session)
	c, _ := svc.PutObjectRequest(&s3.PutObjectInput{
		Bucket: aws.String("バケット名"),
		Key:    aws.String("/diectory/filename"),
	})

	url, err := c.Presign(15 * time.Minute)
	if err != nil {
		fmt.Println("error presigning request", err)
		return nil, err
	}

	fmt.Println(url)
}

ここで有効期限を設定できます

url, err := c.Presign(15 * time.Minute)

公式
https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/s3-example-presigned-urls.html

Bodyにファイルを含め、発行されたURLにアクセスすればアップロードできます

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?