Exemplo n.º 1
0
//Bucket bucket cmd
func Bucket(args []string) (err error) {
	if err = readCfg(); err != nil {
		fmt.Println("You May Havn't Init . Use osscmd init First!")
		os.Exit(0)
	}
	ossapi.Init(accessKeyID, accessKeySecret)
	var e *ossapi.Error
	if "-l" == args[2] {
		info, e := service.QueryBucketsDefault()
		if e != nil {
			Exit(e.Error())
		}
		fmt.Println("Owner:", info.Owner.DisplayName)
		for idx, bkt := range info.Buckets.Bucket {
			fmt.Printf("	Bucket[%v]:", idx)
			fmt.Println(bkt)
		}
	} else if "-c" == args[2] {
		flag.CommandLine.Parse(args[3:])
		var loc, per string
		if BucketFlag.Location == "hangzhou" {
			loc = bucket.LHangzhou
		} else if BucketFlag.Location == "beijin" {
			loc = bucket.LBeijing
		} else if BucketFlag.Location == "shenzhen" {
			loc = bucket.LShenzhen
		} else if BucketFlag.Location == "hongkong" {
			loc = bucket.LHongKong
		} else if BucketFlag.Location == "qingdao" {
			loc = bucket.LQingdao
		} else if BucketFlag.Location == "shanghai" {
			loc = bucket.LShanghai
		} else {
			loc = bucket.LHangzhou
		}
		if BucketFlag.Permission == "RW" {
			per = bucket.PPublicRW
		} else if BucketFlag.Permission == "PT" {
			per = bucket.PPrivate
		} else if BucketFlag.Permission == "RO" {
			per = bucket.PPublicReadOnly
		} else {
			per = bucket.PPrivate
		}
		e = bucket.Create(BucketFlag.Bucket, loc, per)
		if e != nil {
			Exit(e.Error())
		}
		fmt.Println("Create Bucket " + BucketFlag.Bucket + " Success !")
	} else if "-q" == args[2] {
		flag.CommandLine.Parse(args[3:])
		var loc string
		if BucketFlag.Location == "hangzhou" {
			loc = bucket.LHangzhou
		} else if BucketFlag.Location == "beijin" {
			loc = bucket.LBeijing
		} else if BucketFlag.Location == "shenzhen" {
			loc = bucket.LShenzhen
		} else if BucketFlag.Location == "hongkong" {
			loc = bucket.LHongKong
		} else if BucketFlag.Location == "qingdao" {
			loc = bucket.LQingdao
		} else if BucketFlag.Location == "shanghai" {
			loc = bucket.LShanghai
		} else {
			loc = bucket.LHangzhou
		}
		if BucketFlag.IsACL {
			info, e := bucket.QueryACL(BucketFlag.Bucket, loc)
			if e != nil {
				Exit(e.Error())
			}
			fmt.Println("Owner is :", info.Owner)
			fmt.Println("Grant is :", info.AccessControlList.Grant)
		} else if BucketFlag.IsLocation {
			info, e := bucket.QueryLocation(BucketFlag.Bucket)
			if e != nil {
				Exit(e.Error())
			}
			fmt.Println("Location is ", info)
		} else if BucketFlag.IsLifecycle {
			info, e := bucket.QueryLifecycle(BucketFlag.Bucket, loc)
			if e != nil {
				Exit(e.Error())
			}
			for idx, rule := range info {
				fmt.Printf("Rule [%d]:", idx)
				fmt.Println(rule)
			}
		} else if BucketFlag.IsLog {
			info, e := bucket.QueryLogging(BucketFlag.Bucket, loc)
			if e != nil {
				Exit(e.Error())
			}
			if info == nil {
				fmt.Println("Bucket has not config logging")

			} else {
				fmt.Println("Target Bucket", info.TargetBucket)
				fmt.Println("Target Prefix", info.TargetPrefix)
			}
		} else if BucketFlag.IsReferer {
			info, e := bucket.QueryReferer(BucketFlag.Bucket, loc)
			if e != nil {
				Exit(e.Error())
			}
			fmt.Println("If Allow Empyt", info.AllowEmptyReferer)
			fmt.Println("White List", info.RefererList.Referer)
		} else if BucketFlag.IsWebsite {
			info, e := bucket.QueryWebsite(BucketFlag.Bucket, loc)
			if e != nil {
				Exit(e.Error())
			}
			fmt.Println("Index is :", info.IndexDocument.Suffix)
			fmt.Println("404page is :", info.ErrorDocument.Key)
		} else {
			info, e := bucket.QueryObjects(BucketFlag.Bucket, loc, "", "", "", "", 0)
			if e != nil {
				Exit(e.Error())
			}
			for idx, obj := range info.Contents {
				fmt.Printf("Objects [%d]", idx)
				fmt.Println(" are:", obj)
			}
		}
	} else if "-d" == args[2] {
		flag.CommandLine.Parse(args[3:])
		var loc string
		if BucketFlag.Location == "hangzhou" {
			loc = bucket.LHangzhou
		} else if BucketFlag.Location == "beijin" {
			loc = bucket.LBeijing
		} else if BucketFlag.Location == "shenzhen" {
			loc = bucket.LShenzhen
		} else if BucketFlag.Location == "hongkong" {
			loc = bucket.LHongKong
		} else if BucketFlag.Location == "qingdao" {
			loc = bucket.LQingdao
		} else if BucketFlag.Location == "shanghai" {
			loc = bucket.LShanghai
		} else {
			loc = bucket.LHangzhou
		}
		if BucketFlag.IsLog {
			e = bucket.DeleteLogging(BucketFlag.Bucket, loc)
			if e != nil {
				Exit(e.Error())
			}
			fmt.Println("Delete Bucket " + BucketFlag.Bucket + "  Logging Success !")
		} else if BucketFlag.IsWebsite {
			e = bucket.DeleteWebsite(BucketFlag.Bucket, loc)
			if e != nil {
				Exit(e.Error())
			}
			fmt.Println("Delete Bucket " + BucketFlag.Bucket + "  Website Success !")
		} else if BucketFlag.IsLifecycle {
			e = bucket.DeleteLifecycle(BucketFlag.Bucket, loc)
			if e != nil {
				Exit(e.Error())
			}
			fmt.Println("Delete Bucket " + BucketFlag.Bucket + "  LifeCycle Success !")
		} else {
			e = bucket.Delete(BucketFlag.Bucket, loc)
			if e != nil {
				Exit(e.Error())
			}
			fmt.Println("Delete Bucket " + BucketFlag.Bucket + " Success !")
		}
	} else if "-s" == args[2] {
		flag.CommandLine.Parse(args[3:])
		var loc, per string
		if BucketFlag.Location == "hangzhou" {
			loc = bucket.LHangzhou
		} else if BucketFlag.Location == "beijin" {
			loc = bucket.LBeijing
		} else if BucketFlag.Location == "shenzhen" {
			loc = bucket.LShenzhen
		} else if BucketFlag.Location == "hongkong" {
			loc = bucket.LHongKong
		} else if BucketFlag.Location == "qingdao" {
			loc = bucket.LQingdao
		} else if BucketFlag.Location == "shanghai" {
			loc = bucket.LShanghai
		} else {
			loc = bucket.LHangzhou
		}
		if BucketFlag.Permission == "RW" {
			per = bucket.PPublicRW
		} else if BucketFlag.Permission == "PT" {
			per = bucket.PPrivate
		} else if BucketFlag.Permission == "RO" {
			per = bucket.PPublicReadOnly
		} else {
			per = bucket.PPrivate
		}
		if BucketFlag.Permission != "" {
			e = bucket.SetACL(BucketFlag.Bucket, loc, per)
			if e != nil {
				Exit(e.Error())
			}
			fmt.Println("Set Bucket " + BucketFlag.Bucket + ":" + BucketFlag.Permission + " Success !")
		} else if BucketFlag.Log {

			if BucketFlag.LogPossition == "" {
				e = bucket.CloseLogging(BucketFlag.Bucket, loc)
			} else {
				e = bucket.OpenLogging(BucketFlag.Bucket, loc, BucketFlag.LogPossition, BucketFlag.LogPrefix)
			}
			if e != nil {
				Exit(e.Error())
			}
			fmt.Println("Set Bucket " + BucketFlag.Bucket + "log  Success !")
		} else if BucketFlag.Website {
			e = bucket.SetWebsite(BucketFlag.Bucket, loc, BucketFlag.WebIndex, BucketFlag.WebError)
			if e != nil {
				Exit(e.Error())
			}
			fmt.Println("Set Bucket " + BucketFlag.Bucket + " website  Success !")
		} else if BucketFlag.Referer {
			if len(flag.Args()) > 0 {
				e = bucket.SetReferer(BucketFlag.Bucket, loc, true, flag.Args())
			} else {
				e = bucket.SetReferer(BucketFlag.Bucket, loc, true, flag.Args())
			}
			if e != nil {
				Exit(e.Error())
			}
			fmt.Println("Set Bucket " + BucketFlag.Bucket + " refer Success !")
		} else {
			fmt.Println(bucketHelp)
			os.Exit(-1)
		}

	} else {
		fmt.Println(bucketHelp)
		os.Exit(-1)
	}
	return
}
Exemplo n.º 2
0
// Object is object cmd
func Object(args []string) (err error) {
	if err = readCfg(); err != nil {
		fmt.Println("You May Havn't Init . Use osscmd init First!")
		os.Exit(0)
	}
	ossapi.Init(accessKeyID, accessKeySecret)
	var e *ossapi.Error
	flag.CommandLine.Parse(args[3:])
	var loc, per string
	if BucketFlag.Location == "hangzhou" {
		loc = bucket.LHangzhou
	} else if BucketFlag.Location == "beijin" {
		loc = bucket.LBeijing
	} else if BucketFlag.Location == "shenzhen" {
		loc = bucket.LShenzhen
	} else if BucketFlag.Location == "hongkong" {
		loc = bucket.LHongKong
	} else if BucketFlag.Location == "qingdao" {
		loc = bucket.LQingdao
	} else if BucketFlag.Location == "shanghai" {
		loc = bucket.LShanghai
	} else {
		loc = bucket.LHangzhou
	}
	if BucketFlag.Permission == "RW" {
		per = bucket.PPublicRW
	} else if BucketFlag.Permission == "PT" {
		per = bucket.PPrivate
	} else if BucketFlag.Permission == "RO" {
		per = bucket.PPublicReadOnly
	} else {
		per = bucket.PPrivate
	}
	if "-n" == args[2] {
		fd, err := os.Open(ObjectFlag.File)
		if err != nil {
			Exit(err.Error())
		}
		body, err := ioutil.ReadAll(fd)
		if err != nil {
			Exit(err.Error())
		}
		objInfo := &object.Info{
			CacheControl:       "no-cache",
			ContentDisposition: "attachment;",
			ContentEncoding:    ObjectFlag.Encoding,
			Expires:            ObjectFlag.Expire,
			Encryption:         "AES256",
			ACL:                per,
			Body:               body,
			Type:               ObjectFlag.Type}
		e = object.Create(filepath.Base(ObjectFlag.File), BucketFlag.Bucket, loc, objInfo)
		if e != nil {
			Exit(e.Error())
		}
		fmt.Println("Create Object" + ObjectFlag.File + " Success !")
	} else if "-c" == args[2] {
		copyInfo := &object.CopyInfo{
			ObjectName: CORSFlag.Object,
			BucketName: BucketFlag.Bucket,
			Location:   loc,
			Source:     ObjectFlag.Source,
			Directive:  ObjectFlag.Directive,
			ACL:        per,
			Encryption: "AES256"}
		info, e := object.Copy(copyInfo, nil)
		if e != nil {
			Exit(e.Error())
		}
		fmt.Println("Copy Object Success , New Object is ", info)
	} else if "-a" == args[2] {
		fd, err := os.Open(ObjectFlag.File)
		if err != nil {
			Exit(e.Error())
		}
		body, err := ioutil.ReadAll(fd)
		if err != nil {
			Exit(e.Error())
		}
		objInfo := &object.AppendObjInfo{Info: object.Info{
			CacheControl:       "no-cache",
			ContentDisposition: "attachment;",
			ContentEncoding:    ObjectFlag.Encoding,
			Expires:            ObjectFlag.Expire,
			Encryption:         "AES256",
			Body:               body,
			ACL:                per,
			Type:               ObjectFlag.Type},
			Position: uint64(ObjectFlag.Position)}
		info, e := object.Append(CORSFlag.Object, BucketFlag.Bucket, loc, objInfo)
		if e != nil {
			Exit(e.Error())
		}
		fmt.Println("Append Success. resuult:", info)
	} else if "-d" == args[2] {
		e = object.Delete(CORSFlag.Object, BucketFlag.Bucket, loc)
		if e != nil {
			Exit(e.Error())
		}
		fmt.Println("Delete " + CORSFlag.Object + " Success ")
	} else if "-m" == args[2] {
		info, e := object.QueryMeta(CORSFlag.Object, BucketFlag.Bucket, loc, nil)
		if e != nil {
			Exit(e.Error())
		}
		fmt.Println("Meta "+CORSFlag.Object+"  is ", info)
	} else if "-s" == args[2] {
		e := object.SetACL(CORSFlag.Object, BucketFlag.Bucket, loc, per)
		if e != nil {
			Exit(e.Error())
		}
		fmt.Println("Set Object's ACL Success")
	} else if "-q" == args[2] {
		if BucketFlag.IsACL {
			info, e := object.QueryACL(CORSFlag.Object, BucketFlag.Bucket, loc)
			if e != nil {
				Exit(e.Error())
			}
			fmt.Println("Object's ACL Owner:", info.Owner)
			fmt.Println("Object's ACL:", info.AccessControlList)
		} else {
			data, e := object.Query(CORSFlag.Object, BucketFlag.Bucket, loc, nil, nil)
			if e != nil {
				Exit(e.Error())
			}
			fmt.Println("Data:", string(data))
		}
	} else {
		fmt.Println(objectHelp)
		os.Exit(-1)
	}
	return
}