Last active
July 13, 2016 07:59
-
-
Save yamamoto-febc/af6c432129775694a53cb9121e3927be to your computer and use it in GitHub Desktop.
さくらのクラウドのオブジェクトストレージにGo言語からアクセスしたい ref: http://qiita.com/yamamoto-febc/items/1ea7c559a6bd04f597b1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "os" | |
| "log" | |
| "github.com/mitchellh/goamz/aws" | |
| "github.com/mitchellh/goamz/s3" | |
| ) | |
| func main() { | |
| //アップロードするファイル | |
| file, err := os.Open("./test.txt") | |
| if err != nil { | |
| log.Println(err.Error()) | |
| } | |
| defer file.Close() | |
| //アップロードするファイルのサイズ取得用 | |
| fi , err := file.Stat() | |
| if err != nil { | |
| log.Println(err.Error()) | |
| } | |
| // s3クライアントを環境変数から作成 | |
| // 事前に以下の値を環境変数に設定しておく | |
| // AWS_ACCESS_KEY_ID : アクセスキーID | |
| // AWS_SECRET_ACCESS_KEY : シークレットアクセスキー | |
| auth, err := aws.EnvAuth() | |
| if err != nil { | |
| panic(err.Error()) | |
| } | |
| cli := s3.New(auth , aws.Region{ | |
| Name : "us-west-2", //なんでもいいらしい | |
| S3Endpoint : "https://b.sakurastorage.jp", //さくらのオブジェクトストレージ用URL | |
| }) | |
| // バケット | |
| bucket := cli.Bucket("wanwano") | |
| // バケットに対しアップロード | |
| err = bucket.PutReader("test.txt" , file , fi.Size(), "text/plain" , s3.PublicRead) | |
| if err != nil { | |
| panic(err.Error()) | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment