Esempio n. 1
0
func (dstObject *Object) Copy(srcObject *Object, copyMeta bool) (err error) {
	url, err := dstObject.Client.SignedUrl("PUT", dstObject.Bucket.Name, dstObject.Name, "", "", "")
	if err != nil {
		return
	}

	req, err := http.NewRequest("PUT", url, nil)
	if err != nil {
		return
	}

	req.Header.Set(dstObject.Client.Provider.ObjectCopy(), dstObject.Client.ObjectUrl(srcObject))
	if !copyMeta {
		req.Header.Set(dstObject.Client.Provider.ObjectCopyDrt(), dstObject.Client.Provider.ObjectCopyDrtForReplace())
	}

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return
	}

	err = errors.GetError(resp, dstObject.Client.Provider)

	if err == nil {
		md5 := resp.Header.Get("Content-MD5")
		if md5 == "" {
			err = errors.Error("copy error! content md5 empty")
		} else {
			dstObject.MD5 = md5
		}
	}

	return

}
Esempio n. 2
0
func (object *Object) Put() (err error) {
	ok, err := object.available()
	if !ok {
		return
	}

	url, err := object.Client.SignedUrl("PUT", object.Bucket.Name, object.Name, "", "", "")
	if err != nil {
		return
	}

	req, err := http.NewRequest("PUT", url, bytes.NewReader(object.Data))

	if err != nil {
		return
	}

	if req.Header.Get("Content-Length") == "" {
		req.Header.Set("Content-Length", strconv.FormatUint(object.Size, 10))
	}

	if req.Header.Get("Content-Type") == "" {
		req.Header.Set("Content-Type", object.Type)
	}

	if req.Header.Get("Content-Disposition") == "" {
		req.Header.Set("Content-Disposition", object.Alias)
	}

	if req.Header.Get(object.Client.Provider.Acl()) == "" {
		req.Header.Set(object.Client.Provider.Acl(), object.Acl)
	}

	metaPrefix := object.Client.Provider.MetaPrefix()
	if len(object.Meta) > 0 {
		for k, v := range object.Meta {
			req.Header.Set(metaPrefix+k, v)
		}
	}

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return
	}

	err = errors.GetError(resp, object.Client.Provider)

	if err == nil {
		md5 := resp.Header.Get("Content-MD5")
		if md5 == "" {
			err = errors.Error("put error! content md5 empty")
		} else {
			object.MD5 = md5
		}
	}

	return
}
Esempio n. 3
0
func (bucket *Bucket) Delete() (err error) {
	url, err := bucket.Client.SignedUrl("DELETE", bucket.Name, "/", "", "", "")
	if err != nil {
		return
	}

	req, err := http.NewRequest("DELETE", url, nil)
	if err != nil {
		return
	}

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return
	}

	err = errors.GetError(resp, bucket.Client.Provider)

	return
}
Esempio n. 4
0
func (client Client) ListBucket(parser Parser) (buckets []Bucket, err error) {

	url, err := client.SignedUrl("GET", "", "/", "", "", "")
	if err != nil {
		return
	}

	resp, err := http.Get(url)
	if err != nil {
		return
	}

	defer resp.Body.Close()

	err = errors.GetError(resp, client.Provider)
	if err == nil {
		err = parser.Parse(resp.Body, &buckets)
	}

	return
}
Esempio n. 5
0
func (object *Object) Head() (err error) {
	url, err := object.Client.SignedUrl("HEAD", object.Bucket.Name, object.Name, "", "", "")
	if err != nil {
		return
	}

	req, err := http.NewRequest("HEAD", url, nil)
	if err != nil {
		return
	}

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return
	}

	err = errors.GetError(resp, object.Client.Provider)

	if err == nil {
		object.MD5 = resp.Header.Get("Content-MD5")
		object.Size, _ = strconv.ParseUint(resp.Header.Get("Content-Length"), 10, 64)
		object.Type = resp.Header.Get("Content-Type")

		//modify time
		object.ModifyTime, _ = time.Parse(http.TimeFormat, resp.Header.Get("Last-Modified"))

		//meta headers
		metaPrefix := object.Client.Provider.MetaPrefix()
		for k, v := range resp.Header {
			if idx := strings.Index(k, metaPrefix); idx != -1 {
				if object.Meta == nil {
					object.Meta = make(map[string]string)
				}
				//just keep name without prefix
				object.Meta[k[idx+len(metaPrefix):len(k)]] = strings.Join(v, ";")
			}
		}
	}
	return
}
Esempio n. 6
0
func (bucket *Bucket) List(parser Parser) (err error) {
	url, err := bucket.Client.SignedUrl("GET", bucket.Name, "/", "", "", "")

	if err != nil {
		return
	}

	resp, err := http.Get(url)

	if err != nil {
		return
	}

	defer resp.Body.Close()

	err = errors.GetError(resp, bucket.Client.Provider)

	if err == nil {
		err = parser.Parse(resp.Body, &bucket.Objects)
	}
	return
}
Esempio n. 7
0
func (bucket *Bucket) Create() (err error) {
	url, err := bucket.Client.SignedUrl("PUT", bucket.Name, "/", "", "", "")
	if err != nil {
		return
	}

	req, err := http.NewRequest("PUT", url, nil)
	if err != nil {
		return
	}

	if bucket.Acl != "" {
		req.Header.Set(bucket.Client.Provider.Acl(), bucket.Acl)
	}

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return
	}

	err = errors.GetError(resp, bucket.Client.Provider)

	return
}