func TestGetService(t *testing.T) {
	if nil != ossapi.Init("v8P430U3UcILP6KA", "EB9v8yL2aM07YOgtO1BdfrXtdxa4A1") {
		t.Fail()
	}
	if buckets, err := QueryBucketsDefault(); err != nil {
		fmt.Println(err.ErrNo, err.HTTPStatus, err.ErrMsg, err.ErrDetailMsg)
	} else {
		fmt.Println(buckets)
		t.Log("[SUCC]:GetService")
	}

	fmt.Println("+++++++++++++++Get Service With+++++++++++")
	if buckets, err := QueryBuckets("aa", "b&afds=safsd?asfsab", 10); err != nil {
		fmt.Println(err.ErrNo, err.HTTPStatus, err.ErrMsg, err.ErrDetailMsg)
	} else {
		fmt.Println(buckets)
		t.Log("[SUCC]:GetService")
	}

	fmt.Println("+++++++++++++++With Init Error+++++++++++")
	if nil != ossapi.Init("v8P430U3UcILP6KA", "EB9v8yL2aM07YOgtO1BdfrXtdxa4A1sfasdfs") {
		t.Fail()
	}
	if buckets, err := QueryBucketsDefault(); err != nil {
		fmt.Println(err.ErrNo, err.HTTPStatus, err.ErrMsg, err.ErrDetailMsg)
	} else {
		fmt.Println(buckets)
		t.Log("[SUCC]:GetService")
	}
}
func TestCopyObject(t *testing.T) {
	if nil != ossapi.Init("v8P430U3UcILP6KA", "EB9v8yL2aM07YOgtO1BdfrXtdxa4A1") {
		t.Fail()
	}

	copyInfo := &CopyInfo{
		ObjectName: "test2.html",
		BucketName: "test-object-hz",
		Location:   bucket.LHangzhou,
		Source:     "/test-object-hz/test"}

	if info, err := Copy(copyInfo, nil); err != nil {
		fmt.Println(err.ErrNo, err.HTTPStatus, err.ErrMsg, err.ErrDetailMsg)
	} else {
		t.Log("CopyObject Success")
		fmt.Println(info)
	}
	conInfo := &CopyConditionInfo{}
	if info, err := Copy(copyInfo, conInfo); err != nil {
		fmt.Println(err.ErrNo, err.HTTPStatus, err.ErrMsg, err.ErrDetailMsg)
	} else {
		t.Log("CopyObject Success")
		fmt.Println(info)
	}
}
func TestCreateObject(t *testing.T) {
	if nil != ossapi.Init("v8P430U3UcILP6KA", "EB9v8yL2aM07YOgtO1BdfrXtdxa4A1") {
		t.Fail()
	}
	objInfo := &Info{
		CacheControl:       "no-cache",
		ContentDisposition: "attachment;filename=oss_download.jpg",
		ContentEncoding:    "utf-8",
		Expires:            "Fri, 28 Feb 2012 05:38:42 GMT",
		Encryption:         "AES256",
		ACL:                bucket.PPrivate,
		Body:               []byte("<html><head></head><body>test</body></html>"),
		Type:               "text/html"}
	if err := Create("test", "test-object-hz", bucket.LHangzhou, objInfo); err != nil {
		fmt.Println(err.ErrNo, err.HTTPStatus, err.ErrMsg, err.ErrDetailMsg)
	} else {
		t.Log("PutObject Success!")
	}

	if err := Create("test2", "test-object-hz", bucket.LHangzhou, objInfo); err != nil {
		fmt.Println(err.ErrNo, err.HTTPStatus, err.ErrMsg, err.ErrDetailMsg)
	} else {
		t.Log("PutObject Success!")
	}
}
func TestCompleteMultipartUpload(t *testing.T) {
	if nil != ossapi.Init("v8P430U3UcILP6KA", "EB9v8yL2aM07YOgtO1BdfrXtdxa4A1") {
		t.Fail()
	}
	initInfo := &InitInfo{
		CacheControl:       "no-cache",
		ContentDisposition: "attachment;filename=oss_download.jpg",
		ContentEncoding:    "utf-8",
		Expires:            "Fri, 28 Feb 2012 05:38:42 GMT",
		Encryption:         "AES256"}
	var info *InitRstInfo
	var err *ossapi.Error
	if info, err = Init("a.c", "test-mupload", bucket.LHangzhou, initInfo); err != nil {
		fmt.Println(err.ErrNo, err.HTTPStatus, err.ErrMsg, err.ErrDetailMsg)
	} else {
		t.Log("Init Multiple Upload Success!")
		fmt.Println(info)
	}
	var partData []byte
	for i := 0; i < 10250; i++ {
		partData = append(partData, "1234567890"...)
	}

	partInfo := &UploadPartInfo{
		ObjectName: "a.c",
		BucketName: "test-mupload",
		Location:   bucket.LHangzhou,
		UploadID:   info.UploadId,
		PartNumber: 1,
		Data:       partData[:100*1024],
		CntType:    "text/html"}

	var i1 PartInfo
	if info, err := Append(partInfo); err != nil {
		fmt.Println(err.ErrNo, err.HTTPStatus, err.ErrMsg, err.ErrDetailMsg)
	} else {
		i1.ETag = info.Etag
		i1.PartNumber = 1
		t.Log("UploadPart Success!")
	}

	partInfo.PartNumber = 2
	var i2 PartInfo
	if info, err := Append(partInfo); err != nil {
		fmt.Println(err.ErrNo, err.HTTPStatus, err.ErrMsg, err.ErrDetailMsg)
	} else {
		i2.ETag = info.Etag
		i2.PartNumber = 2
		t.Log("UploadPart Success!")
	}

	partsInfo := &PartsInfo{Part: []PartInfo{i1, i2}}
	if info, err := Complete("a.c", "test-mupload", bucket.LHangzhou, info.UploadId, partsInfo); err != nil {
		fmt.Println(err.ErrNo, err.HTTPStatus, err.ErrMsg, err.ErrDetailMsg)
	} else {
		t.Log(" CompleteMultipartUpload Success!")
		fmt.Println(info)
	}

}
func TestGetBucketACL(t *testing.T) {
	if nil != ossapi.Init("v8P430U3UcILP6KA", "EB9v8yL2aM07YOgtO1BdfrXtdxa4A1") {
		t.Fail()
	}
	if info, err := QueryACL("test-put-bucket2", LHangzhou); err != nil {
		fmt.Println(err.ErrNo, err.HTTPStatus, err.ErrMsg, err.ErrDetailMsg)
	} else {
		fmt.Println(info)
	}
}
func TestSetBucketWebsite(t *testing.T) {
	if nil != ossapi.Init("v8P430U3UcILP6KA", "EB9v8yL2aM07YOgtO1BdfrXtdxa4A1") {
		t.Fail()
	}
	if err := SetWebsite("test-put-bucket4", LBeijing, "index.html", ""); err != nil {
		fmt.Println(err.ErrNo, err.HTTPStatus, err.ErrMsg, err.ErrDetailMsg)
	} else {
		t.Log("SetBucketWebSite Success")
	}
}
func TestDeleteBucketCORS(t *testing.T) {
	if nil != ossapi.Init("v8P430U3UcILP6KA", "EB9v8yL2aM07YOgtO1BdfrXtdxa4A1") {
		t.Fail()
	}
	if err := Delete("test-cors", bucket.LHangzhou); err != nil {
		fmt.Println(err.ErrNo, err.HTTPStatus, err.ErrMsg, err.ErrDetailMsg)
	} else {
		t.Log("DelteBucketCORS Success")
	}
}
func TestDeleteBucketLifecycle(t *testing.T) {
	if nil != ossapi.Init("v8P430U3UcILP6KA", "EB9v8yL2aM07YOgtO1BdfrXtdxa4A1") {
		t.Fail()
	}
	if err := DeleteLifecycle("test-put-bucket3", LBeijing); err != nil {
		fmt.Println(err.ErrNo, err.HTTPStatus, err.ErrMsg, err.ErrDetailMsg)
	} else {
		t.Log("DeleteBucketLifecycle Success")
	}
}
func TestOpenLogging(t *testing.T) {
	if nil != ossapi.Init("v8P430U3UcILP6KA", "EB9v8yL2aM07YOgtO1BdfrXtdxa4A1") {
		t.Fail()
	}
	if err := OpenLogging("test-put-bucket", LHangzhou, "test-put-bucket", "test-2-"); err != nil {
		fmt.Println(err.ErrNo, err.HTTPStatus, err.ErrMsg, err.ErrDetailMsg)
	} else {
		t.Log("Open Bucket Log Success")
	}
}
func TestListMultipartUploads(t *testing.T) {
	if nil != ossapi.Init("v8P430U3UcILP6KA", "EB9v8yL2aM07YOgtO1BdfrXtdxa4A1") {
		t.Fail()
	}
	if info, err := QueryObjects("test-object-hz", bucket.LHangzhou, nil); err != nil {
		fmt.Println(err.ErrNo, err.HTTPStatus, err.ErrMsg, err.ErrDetailMsg)
	} else {
		t.Log("Init Multiple Upload Success!")
		fmt.Println(info)
	}
}
func TestSetObjectACL(t *testing.T) {
	if nil != ossapi.Init("v8P430U3UcILP6KA", "EB9v8yL2aM07YOgtO1BdfrXtdxa4A1") {
		t.Fail()
	}
	if err := SetACL("acl", "test-object-hz", bucket.LHangzhou, bucket.PPublicRW); err != nil {
		fmt.Println(err.ErrNo, err.HTTPStatus, err.ErrMsg, err.ErrDetailMsg)
	} else {
		t.Log("PutObjectACL Success!")
	}

}
func TestGetBucket(t *testing.T) {
	if nil != ossapi.Init("v8P430U3UcILP6KA", "EB9v8yL2aM07YOgtO1BdfrXtdxa4A1") {
		t.Fail()
	}
	if info, err := QueryObjects("test-put-bucket3", LBeijing, "", "", "", "", 0); err != nil {
		fmt.Println(err.ErrNo, err.HTTPStatus, err.ErrMsg, err.ErrDetailMsg)
	} else {
		t.Log("GetBucket Success")
		fmt.Println(info)
	}
}
func TestSetBucketLifecycle(t *testing.T) {
	if nil != ossapi.Init("v8P430U3UcILP6KA", "EB9v8yL2aM07YOgtO1BdfrXtdxa4A1") {
		t.Fail()
	}
	rules := []RuleInfo{RuleInfo{Prefix: "nimei", Status: LifecycleStatsEnable, Expiration: ExpirationDaysInfo{2}}}
	if err := SetLifecycle("test-put-bucket3", LBeijing, rules); err != nil {
		fmt.Println(err.ErrNo, err.HTTPStatus, err.ErrMsg, err.ErrDetailMsg)
	} else {
		t.Log("SetBucketLiecycle Success")
	}
}
func TestOptionObject(t *testing.T) {
	if nil != ossapi.Init("v8P430U3UcILP6KA", "EB9v8yL2aM07YOgtO1BdfrXtdxa4A1") {
		t.Fail()
	}
	optionInfo := &OptionReqInfo{Origin: "www.qq.com", Method: "GET", Headers: "authorization"}
	if info, err := Option("app.py", "test-cors", bucket.LHangzhou, optionInfo); err != nil {
		fmt.Println(err.ErrNo, err.HTTPStatus, err.ErrMsg, err.ErrDetailMsg)
	} else {
		t.Log("OptionObject Success")
		fmt.Println(info)
	}
}
func TestQueryObject(t *testing.T) {
	if nil != ossapi.Init("v8P430U3UcILP6KA", "EB9v8yL2aM07YOgtO1BdfrXtdxa4A1") {
		t.Fail()
	}
	if info, err := Query("acl", "test-object-hz", bucket.LHangzhou, nil, nil); err != nil {
		fmt.Println(err.ErrNo, err.HTTPStatus, err.ErrMsg, err.ErrDetailMsg)
	} else {
		t.Log("GetObjectACL Success!")
		fmt.Println(info)
	}

}
func TestDeleteObject(t *testing.T) {
	if nil != ossapi.Init("v8P430U3UcILP6KA", "EB9v8yL2aM07YOgtO1BdfrXtdxa4A1") {
		t.Fail()
	}

	if err := Delete("append", "test-object-hz", bucket.LHangzhou); err != nil {
		fmt.Println(err.ErrNo, err.HTTPStatus, err.ErrMsg, err.ErrDetailMsg)
	} else {
		t.Log("AppendObject Success!")
	}

}
func TestSetBucketReferer(t *testing.T) {
	if nil != ossapi.Init("v8P430U3UcILP6KA", "EB9v8yL2aM07YOgtO1BdfrXtdxa4A1") {
		t.Fail()
	}
	if err := SetReferer("test-put-bucket3", LBeijing, false, []string{"http://www.baidu.com", "http://www.qq.com"}); err != nil {
		fmt.Println(err.ErrNo, err.HTTPStatus, err.ErrMsg, err.ErrDetailMsg)
	} else {
		t.Log("SetBucketReferer Success")
	}
	if err := SetReferer("test-put-bucket4", LBeijing, false, nil); err != nil {
		fmt.Println(err.ErrNo, err.HTTPStatus, err.ErrMsg, err.ErrDetailMsg)
	} else {
		t.Log("SetBucketReferer Success")
	}
}
func TestCreate(t *testing.T) {
	if nil != ossapi.Init("v8P430U3UcILP6KA", "EB9v8yL2aM07YOgtO1BdfrXtdxa4A1") {
		t.Fail()
	}
	if err := CreateDefault("test-put-bucket2"); err != nil {
		fmt.Println(err.ErrNo, err.HTTPStatus, err.ErrMsg, err.ErrDetailMsg)
	} else {
		fmt.Println("Create Default Success!")
	}
	if err := Create("test-put-bucket3", LBeijing, PPrivate); err != nil {
		fmt.Println(err.ErrNo, err.HTTPStatus, err.ErrMsg, err.ErrDetailMsg)
	} else {
		fmt.Println("Create Success")
	}
}
func TestDeleteObjects(t *testing.T) {
	if nil != ossapi.Init("v8P430U3UcILP6KA", "EB9v8yL2aM07YOgtO1BdfrXtdxa4A1") {
		t.Fail()
	}
	info := &DeleteObjInfo{
		Quiet:  false,
		Object: []KeyInfo{KeyInfo{Key: "test"}, KeyInfo{Key: "test2"}},
	}
	if info, err := DeleteObjects("test-object-hz", bucket.LHangzhou, info); err != nil {
		fmt.Println(err.ErrNo, err.HTTPStatus, err.ErrMsg, err.ErrDetailMsg)
	} else {
		t.Log("Delte Multiplie Objects Success!")
		fmt.Println(info)
	}

}
func TestBriefObject(t *testing.T) {
	if nil != ossapi.Init("v8P430U3UcILP6KA", "EB9v8yL2aM07YOgtO1BdfrXtdxa4A1") {
		t.Fail()
	}
	if info, err := QueryMeta("append2", "test-object-hz", bucket.LHangzhou, nil); err != nil {
		fmt.Println(err.ErrNo, err.HTTPStatus, err.ErrMsg, err.ErrDetailMsg)
	} else {
		t.Log("CopyObject Success")
		fmt.Println(info)
	}
	if info, err := QueryMeta("append2", "test-object-hz", bucket.LHangzhou, &BriefConnInfo{MatchEtag: "append2"}); err != nil {
		fmt.Println(err.ErrNo, err.HTTPStatus, err.ErrMsg, err.ErrDetailMsg)
	} else {
		t.Log("CopyObject Success")
		fmt.Println(info)
	}
}
func TestCreateBucketCORS(t *testing.T) {
	if nil != ossapi.Init("v8P430U3UcILP6KA", "EB9v8yL2aM07YOgtO1BdfrXtdxa4A1") {
		t.Fail()
	}
	corsInfo := []RuleInfo{RuleInfo{
		AllowedOrigin: []string{"www.qq.com", "www.baidu.com", "*", "/"},
		AllowedMethod: []string{"GET"},
		AllowedHeader: []string{"Access-Control-Requet-Headers", "Authorization", "Access-Control-Request-Method"},
		ExposeHeader:  []string{"x-oss-test", "x-oss-test2"},
		MaxAgeSeconds: 100,
	}}
	if err := Create("test-cors", bucket.LHangzhou, corsInfo); err != nil {
		fmt.Println(err.ErrNo, err.HTTPStatus, err.ErrMsg, err.ErrDetailMsg)
	} else {
		fmt.Println("Create Bucket CORS SUCCESS")
	}
}
func TestInit(t *testing.T) {
	if nil != ossapi.Init("v8P430U3UcILP6KA", "EB9v8yL2aM07YOgtO1BdfrXtdxa4A1") {
		t.Fail()
	}
	initInfo := &InitInfo{
		CacheControl:       "no-cache",
		ContentDisposition: "attachment;filename=oss_download.jpg",
		ContentEncoding:    "utf-8",
		Expires:            "Fri, 28 Feb 2012 05:38:42 GMT",
		Encryption:         "AES256"}
	if info, err := Init("a.c", "test-mupload", bucket.LHangzhou, initInfo); err != nil {
		fmt.Println(err.ErrNo, err.HTTPStatus, err.ErrMsg, err.ErrDetailMsg)
	} else {
		t.Log("Init Multiple Upload Success!")
		fmt.Println(info)
	}
}
func TestUploadPartCopy(t *testing.T) {
	if nil != ossapi.Init("v8P430U3UcILP6KA", "EB9v8yL2aM07YOgtO1BdfrXtdxa4A1") {
		t.Fail()
	}
	initInfo := &InitInfo{
		CacheControl:       "no-cache",
		ContentDisposition: "attachment;filename=oss_download.jpg",
		ContentEncoding:    "utf-8",
		Expires:            "Fri, 28 Feb 2012 05:38:42 GMT",
		Encryption:         "AES256"}
	var info *InitRstInfo
	var err *ossapi.Error
	if info, err = Init("a.c", "test-mupload", bucket.LHangzhou, initInfo); err != nil {
		fmt.Println(err.ErrNo, err.HTTPStatus, err.ErrMsg, err.ErrDetailMsg)
	} else {
		t.Log("Init Multiple Upload Success!")
		fmt.Println(info)
	}
	var partData []byte
	for i := 0; i < 10250; i++ {
		partData = append(partData, "1234567890"...)
	}

	partInfo := &UploadPartCopyInfo{
		ObjectName:    "a.cpp",
		BucketName:    "test-mupload",
		Location:      bucket.LHangzhou,
		UploadID:      info.UploadId,
		PartNumber:    1,
		SrcObjectName: "a.c",
		SrcBucketName: "test-mupload",
		SrcRangeBegin: 1,
		SrcRangeEnd:   102400,
	}

	if copyInfo, err := Copy(partInfo, nil); err != nil {
		fmt.Println(err.ErrNo, err.HTTPStatus, err.ErrMsg, err.ErrDetailMsg)
	} else {
		t.Log("UploadPartCopy Success!")
		fmt.Println(copyInfo)
	}
}
Example #24
0
// CORS is cors cmd
func CORS(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 "-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
		}
		info, e := cors.Query(BucketFlag.Bucket, loc)
		if e != nil {
			Exit(e.Error())
		}
		for idx, rule := range info {
			fmt.Printf("Rule [%d]: \n", idx)
			fmt.Println("    AllowedOrigin:", rule.AllowedOrigin)
			fmt.Println("    AllowedMethod:", rule.AllowedMethod)
			fmt.Println("    AllowedHeader:", rule.AllowedHeader)
			fmt.Println("    ExposeHeader:", rule.ExposeHeader)
			fmt.Println("    MaxAgeSeconds:", rule.MaxAgeSeconds)
		}
	} 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
		}
		e := cors.Delete(BucketFlag.Bucket, loc)
		if e != nil {
			Exit(e.Error())
		}
		fmt.Println("Delete CORS Success.")
	} else if "-o" == 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
		}
		reqInfo := &cors.OptionReqInfo{
			Origin:  CORSFlag.Origin,
			Headers: CORSFlag.Headers,
			Method:  CORSFlag.Method}
		info, e := cors.Option(CORSFlag.Object, BucketFlag.Bucket, loc, reqInfo)
		if e != nil {
			Exit(e.Error())
		}
		fmt.Println("Option Info:", info)
	} else {
		fmt.Println(corsHelp)
		os.Exit(-1)
	}
	return
}
Example #25
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
}
Example #26
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
}
Example #27
0
// Mupload is mupload cmd
func Mupload(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 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 "-i" == args[2] {
		initInfo := &mupload.InitInfo{
			CacheControl:       "no-cache",
			ContentDisposition: "attachment",
			ContentEncoding:    ObjectFlag.Encoding,
			Expires:            ObjectFlag.Expire,
			Encryption:         "AES256"}
		info, e := mupload.Init(CORSFlag.Object, BucketFlag.Bucket, loc, initInfo)
		if e != nil {
			Exit(e.Error())
		}
		fmt.Println("Init Mupload Success:")
		fmt.Println("Key is:", info.Key)
		fmt.Println("Id is: ", info.UploadId)
		rstData := &UploadInfo{
			ID:  info.UploadId,
			Key: info.Key}
		body, err := xml.MarshalIndent(rstData, "", "  ")
		if err != nil {
			Exit(err.Error())
		}
		fd, err := os.Create(muploadCfgFile)
		defer fd.Close()
		if err != nil {
			Exit(err.Error())
		}
		fd.Write(append([]byte(xml.Header), body...))
	} else if "-l" == args[2] {
		cfgFd, err := os.Open(muploadCfgFile)
		if err != nil {
			Exit(err.Error())
		}
		cfgData, err := ioutil.ReadAll(cfgFd)
		defer cfgFd.Close()
		if err != nil {
			Exit(err.Error())
		}
		cfgInfo := new(UploadInfo)
		err = xml.Unmarshal(cfgData, cfgInfo)
		if err != nil {
			Exit(err.Error())
		}
		cfgFd.Close()
		info, e := mupload.QueryParts(CORSFlag.Object, BucketFlag.Bucket, loc, cfgInfo.ID, nil)
		if e != nil {
			Exit(e.Error())
		}
		fmt.Println("Uploaded Parts:")
		for idx, p := range info.Part {
			fmt.Printf("Part [%d]", idx)
			fmt.Println(p)
		}
	} else if "-m" == args[2] {
		info, e := mupload.QueryObjects(BucketFlag.Bucket, loc, nil)
		if e != nil {
			Exit(e.Error())
		}
		fmt.Println("Uploading Slices:")
		for idx, p := range info.Upload {
			fmt.Printf("Part [%d]", idx)
			fmt.Println(p)
		}
	} else if "-d" == args[2] {
		cfgFd, err := os.Open(muploadCfgFile)
		if err != nil {
			Exit(err.Error())
		}
		cfgData, err := ioutil.ReadAll(cfgFd)
		defer cfgFd.Close()
		if err != nil {
			Exit(err.Error())
		}
		cfgInfo := new(UploadInfo)
		err = xml.Unmarshal(cfgData, cfgInfo)
		if err != nil {
			Exit(err.Error())
		}
		cfgFd.Close()
		e := mupload.Abort(CORSFlag.Object, BucketFlag.Bucket, loc, cfgInfo.ID)
		if e != nil {
			Exit(e.Error())
		}
		fmt.Println("Success Cacnel Upload Task")
	} else if "-c" == args[2] {
		cfgFd, err := os.Open(muploadCfgFile)
		if err != nil {
			Exit(err.Error())
		}
		cfgData, err := ioutil.ReadAll(cfgFd)
		defer cfgFd.Close()
		if err != nil {
			Exit(err.Error())
		}
		cfgInfo := new(UploadInfo)
		err = xml.Unmarshal(cfgData, cfgInfo)
		if err != nil {
			Exit(err.Error())
		}
		cfgFd.Close()
		var parts []mupload.PartInfo
		for _, i := range cfgInfo.PartInfo {
			parts = append(parts, mupload.PartInfo{ETag: i.ETag, PartNumber: i.Number})
		}
		partsInfo := &mupload.PartsInfo{Part: parts}
		rstInfo, e := mupload.Complete(CORSFlag.Object, BucketFlag.Bucket, loc, cfgInfo.ID, partsInfo)
		if e != nil {
			Exit(e.Error())
		}
		fmt.Println("Success Done Complete ETag:", rstInfo.ETag, "  Key:", rstInfo.Key)
	} else if "-a" == args[2] {
		fd, err := os.Open(ObjectFlag.File)
		defer fd.Close()
		if err != nil {
			Exit(err.Error())
		}
		body, err := ioutil.ReadAll(fd)
		if err != nil {
			Exit(err.Error())
		}
		cfgFd, err := os.Open(muploadCfgFile)
		if err != nil {
			Exit(err.Error())
		}
		cfgData, err := ioutil.ReadAll(cfgFd)
		defer cfgFd.Close()
		if err != nil {
			Exit(err.Error())
		}
		cfgInfo := new(UploadInfo)
		err = xml.Unmarshal(cfgData, cfgInfo)
		if err != nil {
			Exit(err.Error())
		}
		cfgFd.Close()
		partInfo := &mupload.UploadPartInfo{
			ObjectName: CORSFlag.Object,
			BucketName: BucketFlag.Bucket,
			Location:   loc,
			UploadID:   cfgInfo.ID,
			PartNumber: MuploadFlag.Number,
			Data:       body,
			CntType:    ObjectFlag.Type}
		info, e := mupload.Append(partInfo)
		if e != nil {
			Exit(e.Error())
		}
		fmt.Println("Add Part Success, Tag is ", info.Etag)
		cfgFd, err = os.Create(muploadCfgFile)
		defer cfgFd.Close()
		if err != nil {
			Exit(err.Error())
		}
		cfgInfo.PartInfo = append(cfgInfo.PartInfo, PartInfo{Number: MuploadFlag.Number, ETag: strings.Trim(info.Etag, "\"")})
		cfgData, err = xml.MarshalIndent(cfgInfo, "", "  ")
		if err != nil {
			Exit(err.Error())
		}
		cfgFd.Write(append([]byte(xml.Header), cfgData...))
	} else {
		fmt.Println(corsHelp)
		os.Exit(-1)
	}
	return
}