1
0

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.

【Go】CloudFrontの署名付きURLでプライベートコンテンツを配信

Posted at

Golangで署名付きURLを発行します。


import 	sign "github.com/aws/aws-sdk-go/service/cloudfront/sign"

func main() {

type CFConfig struct {
	KeyID   string
	Domain  string
	KeyPath string
}

// cloud front config
cfConf := &model.CFConfig{
	KeyID:   "xxxxxxxxxxxxCQ"
	Domain:  "http://xxx.cloudfront.net",
	KeyPath: "key/xxxxxxxxxxxxCQ",
}

	privKey, err := sign.LoadPEMPrivKeyFile(r.cfConf.KeyPath)
	if err != nil {
		log.Println("failed to load pem file:", err)
		return "", err
	}

	keyID := r.cfConf.KeyID
	signer := sign.NewURLSigner(keyID, privKey)

	path := "s3アクセスを許可したいコンテンツのパス"

	signedURL, err := signer.Sign(r.cfConf.Domain+path, time.Now().Add(1*time.Hour))
	if err != nil {
		log.Fatalf("Failed to sign url, err: %s\n", err.Error())
	}
	return signedURL, err
}
1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?