Пример #1
0
func Start() error {
	t := log.Start()
	defer log.End(t)

	o, err := option.New(os.Args[1:])
	if err != nil {
		return err
	}
	handler, err := NewHandler(o)
	if err != nil {
		return err
	}
	s := http.Server{
		Handler:        &handler,
		ReadTimeout:    10 * time.Second,
		MaxHeaderBytes: 1 << 20,
	}
	l, err := net.Listen("tcp", addr)
	if err != nil {
		return err
	}
	if err := s.Serve(netutil.LimitListener(l, o.MaxConn)); err != nil {
		log.Fatalf("fail: err=%v", err)
	}
	log.Println("listening: addr=%s", addr)
	return nil
}
Пример #2
0
// New はアップローダーを作成する。
func New(o option.Options) (*Uploader, error) {
	jsonFile, err := ioutil.ReadFile(o.JSON)
	if err != nil {
		log.Fatalf("Could not open json: %v", err)
	}
	config, err := google.JWTConfigFromJSON(jsonFile, scope)
	if err != nil {
		log.Fatalf("Could not parse json: %v", err)
	}
	client := config.Client(context.Background())
	service, err := gcs.New(client)
	if err != nil {
		log.Fatalf("Unable to create storage service: %v", err)
	}
	return &Uploader{service, o.ProjectID, o.Bucket}, nil
}
Пример #3
0
func (self *Uploader) Upload(buf *bytes.Buffer, f storage.Image) (string, error) {
	t := log.Start()
	defer log.End(t)

	object := &gcs.Object{Name: f.Filename, CacheControl: fmt.Sprintf("max-age=%d", sixMonths)}
	if res, err := self.service.Objects.Insert(self.bucket, object).Media(buf).Do(); err == nil {
		fmt.Printf("Created object %v at location %v\n\n", res.Name, res.SelfLink)
	} else {
		log.Fatalf("Objects.Insert failed: %v", err)
	}

	url := self.CreateURL(f.Filename)
	log.Printf("ok: url=%s", url)

	return url, nil
}