Skip to content

Instantly share code, notes, and snippets.

@PyYoshi
Last active October 9, 2015 03:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PyYoshi/6fde8f4764fea5012955 to your computer and use it in GitHub Desktop.
Save PyYoshi/6fde8f4764fea5012955 to your computer and use it in GitHub Desktop.

GZip圧縮したものをうまいことGoogle Cloud Storageにアップロードできなかったので調べた結果,

ContentTyperインタフェースのメソッドを実装することによって解決できることがわかったので そのためのコードを残しておく.

以下サンプル

import (
...
  "google.golang.org/api/storage/v1"
...
)

object := &storage.Object{
	...
}
pngGziped := doGzip("./sample.png")
storageService.Objects.Insert(...).Media(reader.NewContentTyperBytesReader(pngGziped, object.ContentType)).Do()

https://github.com/google/google-api-go-client/tree/7f3774db0aa426a2390fa9abcccbc272dbb93111/storage/v1beta1

package reader
import "bytes"
// ContentTyperBytesReader bytes.ReaderにcontentTypeを付加したもの
type ContentTyperBytesReader struct {
*bytes.Reader
contentType string
}
// ContentType ContentTypeを返す
func (r *ContentTyperBytesReader) ContentType() string {
return r.contentType
}
// NewContentTyperBytesReader ContentTyperBytesReaderを作成する
func NewContentTyperBytesReader(b []byte, contentType string) *ContentTyperBytesReader {
return &ContentTyperBytesReader{bytes.NewReader(b), contentType}
}
package reader
import "testing"
var (
imagePath = "./sample.jpg"
)
func TestNewContentTyperBytesReader(t *testing.T) {
b, err := ioutil.ReadFile(imagePath)
if err != nil {
t.Fatal(err)
}
reader := NewContentTyperBytesReader(b, "image/jpeg")
if reader.ContentType() != "image/jpeg" {
t.Fatalf("contentType is invalid: %s", reader.ContentType())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment