func (minion *Minion) save() { out := minion.current.serialize() path := fmt.Sprintf("builds/%s/%s", minion.current.Job.JobFamily, minion.current.Job.JobId) err := minion.app.s3.Put(path, out, "application/json", s3.ACL("public-read")) if err != nil { log.Printf("Could not upload file %v", err) // panic(err) } else { log.Printf("uploaded file to %s", minion.current.Job.JobId) } minion.app.setAuth("minion:" + minion.app.SimpleCiSecret) err = minion.app.patch("/minions/jobs/"+minion.current.Job.JobId, map[string]interface{}{ "complete": true, "cancelled": minion.current.Status.Cancelled, "failed": minion.current.Status.Failed, "failure": minion.current.Status.Failure, "total_time": minion.current.Status.TotalTime, "stored_output_url": fmt.Sprintf("https://s3-%s.amazonaws.com/%s/%s", minion.app.S3Region, minion.app.S3Bucket, path), }, nil) if err != nil { panic(err) } }
func Put(c Config) gonzo.Stage { return func(ctx context.Context, files <-chan gonzo.File, out chan<- gonzo.File) error { err := checkconfig(c) if err != nil { return err } auth := aws.Auth{ AccessKey: c.AccessKey, SecretKey: c.SecretKey, } con := s3.New(auth, aws.Region(c.Region)) bucket := con.Bucket(c.Name) for { select { case file, ok := <-files: if !ok { return nil } if file.FileInfo().IsDir() { continue } content, err := ioutil.ReadAll(file) if err != nil { return err } name := file.FileInfo().Name() contentType := mime.TypeByExtension(filepath.Ext(name)) if contentType == "" { contentType = http.DetectContentType(content) } ctx = context.WithValue(ctx, "Content-Type", contentType) ctx.Infof("Uploading %s", name) err = bucket.Put(name, content, contentType, s3.ACL(c.Perm)) if err != nil { return err } out <- gonzo.NewFile(ioutil.NopCloser(bytes.NewReader(content)), file.FileInfo()) case <-ctx.Done(): return ctx.Err() } } } }
// PUT on a bucket creates the bucket. // http://docs.amazonwebservices.com/AmazonS3/latest/API/RESTBucketPUT.html func (r bucketResource) put(a *action) interface{} { var created bool if r.bucket == nil { if !validBucketName(r.name) { fatalf(400, "InvalidBucketName", "The specified bucket is not valid") } if loc := locationConstraint(a); loc == "" { fatalf(400, "InvalidRequets", "The unspecified location constraint is incompatible for the region specific endpoint this request was sent to.") } // TODO validate acl r.bucket = &bucket{ name: r.name, // TODO default acl objects: make(map[string]*object), } a.srv.buckets[r.name] = r.bucket created = true } if !created && a.srv.config.send409Conflict() { fatalf(409, "BucketAlreadyOwnedByYou", "Your previous request to create the named bucket succeeded and you already own it.") } r.bucket.acl = s3.ACL(a.req.Header.Get("x-amz-acl")) return nil }