func (s *sampleService) streamVoiceSample(request *restful.Request, response *restful.Response) { sessionId, _ := strconv.Atoi(request.PathParameter("session-id")) sampleId, _ := strconv.Atoi(request.PathParameter("sample-index")) sample, _ := s.repository.Get(sessionId, sampleId) if sample.Id == 0 { response.WriteError(http.StatusNotFound, nil) return } ak, sk := os.Getenv("S3_ACCESS_KEY"), os.Getenv("S3_SECRET_KEY") if ak == "" || sk == "" { response.WriteError(http.StatusInternalServerError, fmt.Errorf("Missing S3_ACCESS_KEY and/or S3_SECRET_KEY")) return } s3util.DefaultConfig.AccessKey = ak s3util.DefaultConfig.SecretKey = sk audio, err := s3util.Open(sample.AudioURL, nil) if err != nil { response.WriteError(http.StatusInternalServerError, err) return } defer audio.Close() response.AddHeader("Content-Type", sample.MimeType) io.Copy(response, audio) }
func ExampleOpen() { s3util.DefaultConfig.AccessKey = "...access key..." s3util.DefaultConfig.SecretKey = "...secret key..." r, _ := s3util.Open("https://mybucket.s3.amazonaws.com/log.txt", nil) w, _ := os.Create("out.txt") io.Copy(w, r) w.Close() }
func (backend *S3Backend) Get(hash string) ([]byte, error) { r, err := s3util.Open(backend.key(hash), nil) if err != nil { return nil, err } defer r.Close() var buf bytes.Buffer w := bufio.NewWriter(&buf) io.Copy(w, r) return buf.Bytes(), nil }
func GetUrl(url string) (resource io.Reader, err error) { r, err := s3util.Open(url, nil) if err != nil { return } // now, we have the file; // let's read it into memory buf := new(bytes.Buffer) if _, err = io.Copy(buf, r); err != nil { return } // close the remote resource r.Close() return buf, nil }
func open(s string) (io.ReadCloser, error) { if isUrl(s) { return s3util.Open(s, nil) } return os.Open(s) }