Beispiel #1
0
func ExampleCreate() {
	s3util.DefaultConfig.AccessKey = "...access key..."
	s3util.DefaultConfig.SecretKey = "...secret key..."
	r, _ := os.Open("/dev/stdin")
	w, _ := s3util.Create("https://mybucket.s3.amazonaws.com/log.txt", nil, nil)
	io.Copy(w, r)
	w.Close()
}
Beispiel #2
0
func (s *sampleService) uploadVoiceSample(request *restful.Request, response *restful.Response) {
	sessionId, _ := strconv.Atoi(request.PathParameter("session-id"))
	sampleId, _ := strconv.Atoi(request.PathParameter("sample-index"))

	sample, err := s.repository.Get(sessionId, sampleId)
	if err != nil {
		response.WriteError(http.StatusInternalServerError, err)
		return
	}
	session, err := s.repository.Group().Sessions().Get(sessionId)
	if err != nil {
		response.WriteError(http.StatusInternalServerError, err)
		return
	}

	ak, sk := os.Getenv("S3_ACCESS_KEY"), os.Getenv("S3_SECRET_KEY")
	bucket := os.Getenv("S3_BUCKET")

	if ak == "" || sk == "" {
		response.WriteError(http.StatusInternalServerError, fmt.Errorf("Missing S3_ACCESS_KEY and/or S3_SECRET_KEY"))
		return
	} else if bucket == "" {
		response.WriteError(http.StatusInternalServerError, fmt.Errorf("No S3_BUCKET env var present."))
		return
	} else if bucket[len(bucket)-1] != '/' {
		response.WriteError(http.StatusInternalServerError, fmt.Errorf("S3_BUCKET must have trailing slash."))
		return
	}
	s3util.DefaultConfig.AccessKey = ak
	s3util.DefaultConfig.SecretKey = sk

	destURI := fmt.Sprintf("%s%d/%d/%d", bucket, session.ProjectId, sessionId, sampleId)
	w, err := s3util.Create(destURI, nil, nil)
	if err != nil {
		response.WriteError(http.StatusInternalServerError, err)
		return
	}

	io.Copy(w, request.Request.Body)
	w.Close()

	sample.AudioURL = destURI

	if sample, err = s.repository.Put(sample); err == nil {
		response.WriteEntity(sample)
	} else {
		response.WriteError(http.StatusBadRequest, err)
	}
}
Beispiel #3
0
func (backend *S3Backend) upload(hash string, data []byte) error {
	backend.Lock()
	defer backend.Unlock()
	r := bytes.NewBuffer(data)
	w, err := s3util.Create(backend.key(hash), nil, nil)
	if err != nil {
		return err
	}
	_, err = io.Copy(w, r)
	if err != nil {
		return err
	}
	if err := w.Close(); err != nil {
		return err
	}
	return nil
}
Beispiel #4
0
func (u *S3Uploader) Upload(imageDiskPath string) (string, error) {

	s3util.DefaultConfig.AccessKey = u.accessKey
	s3util.DefaultConfig.SecretKey = u.secretKey

	header := make(http.Header)
	header.Add("x-amz-acl", "public-read")
	header.Add("Content-Type", "image/png")

	var imageUrl *url.URL
	var err error

	if imageUrl, err = url.Parse(u.bucket); err != nil {
		return "", err
	}

	// add image to url
	imageUrl.Path = path.Join(imageUrl.Path, util.GetRandomString(20)+".png")
	imageUrlString := imageUrl.String()
	log.Debug("Uploading image to s3", "url", imageUrlString)

	writer, err := s3util.Create(imageUrlString, header, nil)
	if err != nil {
		return "", err
	}

	defer writer.Close()

	imgData, err := ioutil.ReadFile(imageDiskPath)
	if err != nil {
		return "", err
	}

	_, err = writer.Write(imgData)
	if err != nil {
		return "", err
	}

	return imageUrlString, nil
}
Beispiel #5
0
func upload(localFile, url string) error {
	reader, err := os.Open(localFile)
	defer reader.Close()
	if err != nil {
		return err
	}

	header := make(http.Header)
	header.Add("x-amz-acl", "public-read")
	writer, err := s3util.Create(url, header, nil)
	defer writer.Close()
	if err != nil {
		return err
	}

	fmt.Printf("Uploading %s...\n", url)
	_, err = io.Copy(writer, reader)
	if err != nil {
		return err
	}

	return nil
}
Beispiel #6
0
func create(s string) (io.WriteCloser, error) {
	if isUrl(s) {
		return s3util.Create(s, nil, nil)
	}
	return os.Create(s)
}
Beispiel #7
0
/**
 * Upload a file straight to s3 via the multipartreader stream
 */
func Upload(r *http.Request, settings AwsSettings) (string, string, string, string, error) {
	var (
		isBase64     = false
		base64Prefix = ";base64,"
		file         string
		origFile     string
		ext          string
		mime         string
	)

	reader, readerErr := r.MultipartReader()
	if readerErr != nil {
		return "", "", "", "", readerErr
	}

	// Copy each part to an s3 file (assumes only 1 part)
	for {
		part, err := reader.NextPart()
		if err == io.EOF {
			break
		}

		// Check the mime type
		cBuff := make([]byte, 512)
		part.Read(cBuff)

		// Check if the file part is base 64 encoded.
		// If so, strip the type prefix string and re-check
		// it's mime type.
		if strings.Contains(string(cBuff), base64Prefix) {
			isBase64 = true

			s := strings.Split(string(cBuff), base64Prefix)

			// Remove the prefix slice element
			s = append(s[:0], s[1:]...)
			sBuff := strings.Join(s, "")

			// Add back to buffer with removed prefix
			cBuff = []byte(sBuff)

			// base 64 decode
			decode, _ := base64.StdEncoding.DecodeString(sBuff)

			// Check the mime type
			kind, _ := filetype.Match([]byte(decode))
			mime = kind.MIME.Value
			origFile = "file." + kind.Extension
		} else {
			kind, _ := filetype.Match(cBuff)
			mime = kind.MIME.Value
			origFile = part.FileName()
		}

		ext = getFileExtension(origFile)

		// Generate a unique file name
		file = fmt.Sprintf("%s", uuid.NewV4())

		joinedBody := io.MultiReader(bytes.NewReader(cBuff), part)

		if isBase64 {
			// base64 decode the stream
			joinedBody = base64.NewDecoder(base64.StdEncoding, joinedBody)
		}

		s3util.DefaultConfig.AccessKey = settings.AccessKey
		s3util.DefaultConfig.SecretKey = settings.SecretKey

		s3File, err := s3util.Create(fmt.Sprintf("http://%s.s3.amazonaws.com/%s.%s", settings.Bucket, file, ext), nil, nil)
		if err != nil {
			return "", "", "", "", err
		}
		defer s3File.Close()

		io.Copy(s3File, joinedBody)

		return file, origFile, ext, mime, nil
	}

	return file, origFile, ext, mime, nil
}