Exemplo n.º 1
0
func (cp *cpOpts) Execute(args []string) (err error) {

	k, err := getAWSKeys()
	if err != nil {
		return
	}

	conf := new(s3gof3r.Config)
	*conf = *s3gof3r.DefaultConfig
	s3 := s3gof3r.New(cp.EndPoint, k)
	conf.Concurrency = cp.Concurrency
	if cp.NoSSL {
		conf.Scheme = "http"
	}
	conf.PartSize = cp.PartSize
	conf.Md5Check = !cp.NoMd5
	conf.NTry = cp.NTry
	s3gof3r.SetLogger(os.Stderr, "", log.LstdFlags, cp.Debug)

	src, err := func(src string) (io.ReadCloser, error) {
		if !strings.HasPrefix(strings.ToLower(src), "s3") {
			return os.Open(src)
		}
		u, err := url.ParseRequestURI(src)
		if err != nil {
			return nil, fmt.Errorf("parse error: %s", err)
		}

		r, _, err := s3.Bucket(u.Host).GetReader(u.Path, conf)
		return r, err
	}(cp.Source)
	if err != nil {
		return
	}
	defer checkClose(src, err)

	dst, err := func(dst string) (io.WriteCloser, error) {
		if !strings.HasPrefix(strings.ToLower(dst), "s3") {
			return os.Create(dst)
		}
		u, err := url.ParseRequestURI(dst)
		if err != nil {
			return nil, fmt.Errorf("parse error: %s", err)
		}

		return s3.Bucket(u.Host).PutWriter(u.Path, ACL(cp.Header, cp.ACL), conf)
	}(cp.Dest)
	if err != nil {
		return
	}

	defer checkClose(dst, err)
	_, err = io.Copy(dst, src)
	return
}
Exemplo n.º 2
0
func (get *getOpts) Execute(args []string) (err error) {
	conf := new(s3gof3r.Config)
	*conf = *s3gof3r.DefaultConfig
	k, err := getAWSKeys()
	if err != nil {
		return
	}
	s3 := s3gof3r.New(get.EndPoint, k)
	b := s3.Bucket(get.Bucket)
	conf.Concurrency = get.Concurrency
	if get.NoSSL {
		conf.Scheme = "http"
	}
	conf.PartSize = get.PartSize
	conf.Md5Check = !get.NoMd5
	conf.NTry = get.NTry

	s3gof3r.SetLogger(os.Stderr, "", log.LstdFlags, get.Debug)

	if get.VersionID != "" {
		get.Key = fmt.Sprintf("%s?versionId=%s", get.Key, get.VersionID)
	}

	w, err := os.Create(get.Path)
	if err != nil {
		if get.Path == "" {
			w = os.Stdout
		} else {
			return
		}
	}
	defer checkClose(w, err)
	r, header, err := b.GetReader(get.Key, conf)
	if err != nil {
		return
	}
	defer checkClose(r, err)
	if _, err = io.Copy(w, r); err != nil {
		return
	}
	if get.Debug {
		log.Println("Headers: ", header)
	}
	return
}
Exemplo n.º 3
0
func (put *putOpts) Execute(args []string) (err error) {
	conf := new(s3gof3r.Config)
	*conf = *s3gof3r.DefaultConfig
	k, err := getAWSKeys()
	if err != nil {
		return
	}
	s3 := s3gof3r.New(put.EndPoint, k)
	b := s3.Bucket(put.Bucket)
	conf.Concurrency = put.Concurrency
	if put.NoSSL {
		conf.Scheme = "http"
	}
	conf.PartSize = put.PartSize
	conf.Md5Check = !put.NoMd5
	conf.NTry = put.NTry
	s3gof3r.SetLogger(os.Stderr, "", log.LstdFlags, put.Debug)

	if put.Header == nil {
		put.Header = make(http.Header)
	}

	r, err := os.Open(put.Path)
	if err != nil {
		if put.Path == "" {
			r = os.Stdin
		} else {
			return
		}
	}
	defer checkClose(r, err)
	w, err := b.PutWriter(put.Key, ACL(put.Header, put.ACL), conf)
	if err != nil {
		return
	}
	defer checkClose(w, err)
	if _, err = io.Copy(w, r); err != nil {
		return
	}
	return
}