Esempio n. 1
0
func (s *S3) UploadFileD(S3 *s3.S3, key string, file *os.File, contentType, perm string) (*s3.PutObjectInput, *s3.PutObjectOutput, error) {

	md5hex, _, _, err := Md5Sum(file)
	if err != nil {
		return nil, nil, err
	}
	//s.Log.Debugf("key:%s md5=%s", key, md5hex)
	req := &s3.PutObjectInput{
		ACL:    &perm,
		Body:   file,
		Bucket: &s.Bucket,
		//ContentLength: &size,
		ContentType: &contentType,
		Key:         &key,
	}
	res, err := S3.PutObject(req)
	if err != nil {
		return req, res, err
	}
	if res == nil {
		return req, res, fmt.Errorf("res is nil pointer")
	}
	if res.ETag == nil {
		return req, res, fmt.Errorf("res.ETag is nil pointer")
	}
	if len(*res.ETag) < 2 {
		return req, res, fmt.Errorf("*res.ETag is too short. It should have 2 characters or more")
	}
	etag := (*res.ETag)[1 : len(*res.ETag)-1]
	if md5hex != etag {
		return req, res, fmt.Errorf("md5 and ETag does not match. md5:%s ETag:%s", md5hex, etag)
	}
	return req, res, err
}
Esempio n. 2
0
func recvUpload(s3serv *s3.S3, c config, files chan string) {

	for {
		path, closed := <-files

		if !closed {
			break
		}

		if f, err := os.Open(path); err == nil {
			counter := &CountReader{input: f, seeker: f, co: c}
			counter.Init()
			fmt.Print("uploading:", path)
			if _, err := s3serv.PutObject(&s3.PutObjectInput{Bucket: &c.Bucket, Key: &path, Body: counter}); err == nil {
				fmt.Println("done", counter.count)

			} else {
				fmt.Println("error uploading file:", err)
			}

		} else {
			fmt.Printf("error opening file:", err)
		}
	}
}
Esempio n. 3
0
func uploadFile(file string, conf config, svc *s3.S3, results chan<- result) {
	obj, err := newObjectInfo(file)
	if err != nil {
		results <- result{err: fmt.Errorf("unable to read file: %v", err)}
		return
	}

	bname := conf.Buckets[bucketName].Name
	params := &s3.PutObjectInput{
		Bucket:      &bname,
		Key:         &obj.key,
		Body:        obj.body,
		ContentType: &obj.contentType,
	}

	if !private {
		params.ACL = aws.String("public-read")
	}

	_, err = svc.PutObject(params)

	if err != nil {
		results <- result{err: err}
	} else {
		results <- result{url: buildOutputURL(obj, conf)}
	}
}
Esempio n. 4
0
func UploadObject(svc *s3.S3, bucketName, objKey string, reader io.Reader) error {
	params := &s3.PutObjectInput{
		Body:   aws.ReadSeekCloser(reader),
		Bucket: aws.String(bucketName),
		Key:    aws.String(objKey),
		ACL:    ACLPublicRead,
	}
	_, err := svc.PutObject(params)
	return err
}
Esempio n. 5
0
func put(service *s3.S3, bucketName string, key, path string) {
	f, err := os.Open(path)
	if err != nil {
		panic(err)
	}
	defer f.Close()

	_, err = service.PutObject(&s3.PutObjectInput{
		Bucket: stringPtr(bucketName),
		Key:    stringPtr(key),
		Body:   f,
	})
	if err != nil {
		panic(err)
	}
}
Esempio n. 6
0
func s3Upload(svc *s3.S3, bucket string, filename string, data []byte, date string) (*s3.PutObjectOutput, error) {
	params := &s3.PutObjectInput{
		Bucket: aws.String(bucket),
		Key:    aws.String(filename),
		Body:   bytes.NewReader(data),
		ACL:    aws.String("public-read"),
		Metadata: map[string]*string{
			"Date": aws.String(date), //required
		},
	}
	resp, err := svc.PutObject(params)
	if err != nil {
		log.Printf("Error uploading to s3: %v", err)
		log.Printf("bucket: %s filename: %s date: %s", bucket, filename, date)
	}

	return resp, err
}
Esempio n. 7
0
File: s3.go Progetto: ymomoi/vuls
func putObject(svc *s3.S3, k string, b []byte) error {
	var err error
	if c.Conf.GZIP {
		if b, err = gz(b); err != nil {
			return err
		}
		k = k + ".gz"
	}

	if _, err := svc.PutObject(&s3.PutObjectInput{
		Bucket: &c.Conf.S3Bucket,
		Key:    &k,
		Body:   bytes.NewReader(b),
	}); err != nil {
		return fmt.Errorf("Failed to upload data to %s/%s, %s",
			c.Conf.S3Bucket, k, err)
	}
	return nil
}
Esempio n. 8
0
func putObject(client *s3.S3, bucket string, key string, body []byte, contentType string) (err error) {
	contentLength := int64(len(body))

	request := &s3.PutObjectInput{
		Body:          bytesReadCloser{bytes.NewReader(body)},
		Bucket:        &bucket,
		ContentLength: &contentLength,
		ContentType:   &contentType,
		Key:           &key,
	}

	if client == nil {
		return
	}

	if _, err = client.PutObject(request); err != nil {
		err = fmt.Errorf("S3 PutObject: %s", err)
	}
	return
}
Esempio n. 9
0
func (s *S3) ZipUploadReaderD(S3 *s3.S3, key string, data io.Reader, contentType, perm string) (*s3.PutObjectInput, *s3.PutObjectOutput, error) {

	b := &bytes.Buffer{}
	gz := gzip.NewWriter(b)
	_, err := io.Copy(gz, data)
	if err != nil {
		return nil, nil, err
	}
	gz.Close()

	req := &s3.PutObjectInput{
		ACL:         &perm,                      // aws.StringValue   `xml:"-"` private | public-read | public-read-write | authenticated-read | bucket-owner-read | bucket-owner-full-control
		Body:        bytes.NewReader(b.Bytes()), // io.ReadSeeker     `xml:"-"`
		Bucket:      &s.Bucket,                  // aws.StringValue   `xml:"-"`
		ContentType: &contentType,               // aws.StringValue   `xml:"-"`
		Key:         &key,                       // aws.StringValue   `xml:"-"`
	}
	res, err := S3.PutObject(req)
	return req, res, err
}