示例#1
0
func main() {
	var accessKeyID, accessKeySecret string
	flag.StringVar(&accessKeyID, "id", "", "access key ID")
	flag.StringVar(&accessKeySecret, "secret", "", "access key Secret")
	flag.Parse()
	if accessKeyID == "" || accessKeySecret == "" {
		fmt.Println("go run main.go -id <your id> -secret <your secret>")
		flag.PrintDefaults()
		return
	}

	endPoint := "oss-cn-hangzhou.aliyuncs.com"

	// Create an API object
	api := oss.New(endPoint, accessKeyID, accessKeySecret)

	// Create a bucket
	if err := api.PutBucket("bucket-name", oss.PrivateACL); err != nil {
		log.Fatal(err)
	}
	log.Println("bucket created or existed")

	// Post an object
	policy := `{ "expiration": "2020-12-01T12:00:00.000Z", "conditions": [{"success_action_status": "200"}]}`
	postResult, err := api.PostObject("bucket-name", "posted/object", "testdata/test", policy, oss.PostSuccessActionStatus("200"))
	if err != nil {
		log.Fatal(err)
	}
	log.Println("object uploaded")
	fmt.Println(postResult)

	// Get the object
	buf := new(bytes.Buffer)
	headers, err := api.GetObject("bucket-name", "posted/object", buf)
	if err != nil {
		log.Fatal(err)
	}
	log.Println("object downloaded")
	log.Println(headers)

	testStr := "sfweruewpinbeewa"
	if buf.String() != testStr {
		log.Fatalf("expectd %s but got %s", testStr, buf.String())
	}
	log.Println("object contents match")

	if err := api.DeleteObject("bucket-name", "posted/object"); err != nil {
		log.Fatal(err)
	}
	log.Println("object deleted")
}
示例#2
0
func main() {
	var accessKeyID, accessKeySecret string
	flag.StringVar(&accessKeyID, "id", "", "access key ID")
	flag.StringVar(&accessKeySecret, "secret", "", "access key Secret")
	flag.Parse()
	if accessKeyID == "" || accessKeySecret == "" {
		fmt.Println("go run main.go -id <your id> -secret <your secret>")
		flag.PrintDefaults()
		return
	}

	endPoint := "oss-cn-hangzhou.aliyuncs.com"

	// Create an API object
	api := oss.New(endPoint, accessKeyID, accessKeySecret)

	// List all buckets
	bucketList, err := api.GetService()
	if err != nil {
		log.Fatal(err)
	}
	log.Println("OK accessing OSS")
	log.Println(bucketList)

	// Create a bucket
	if err := api.PutBucket("bucket-name", oss.PrivateACL); err != nil {
		log.Fatal(err)
	}
	log.Println("bucket created or existed")

	// Upload an object
	testStr := "your test string"
	if err := api.PutObject("bucket-name", "object/name", strings.NewReader(testStr)); err != nil {
		log.Fatal(err)
	}
	log.Println("object uploaded")

	// Get the object
	buf := new(bytes.Buffer)
	headers, err := api.GetObject("bucket-name", "object/name", buf)
	if err != nil {
		log.Fatal(err)
	}
	log.Println("object downloaded")
	log.Println(headers)

	if buf.String() != testStr {
		log.Fatalf("expectd %s but got %s", testStr, buf.String())
	}
	log.Println("object contents match")
}
示例#3
0
func main() {
	if len(os.Args) < 2 {
		log.Fatal("Arguement is required. See -h for help.")
	}
	if os.Args[1] == "--help" || os.Args[1] == "-h" {
		fmt.Println("oss <upload_file_path_regexp>")
		return
	}

	fmt.Println("The file to be uploaded: ", getFiles(os.Args[1]))
	fmt.Print("Confirm? [y/n] ")

	k, err := bufio.NewReader(os.Stdin).ReadString('\n')
	if strings.ToLower(strings.Trim(k, "\r\n ")) != "y" {
		return
	}

	access_key_id := os.Getenv("ALIYUN_ACCESS_KEY_ID")
	access_key_secret := os.Getenv("ALIYUN_ACCESS_SECRET_KEY")

	client, err := oss.New("http://oss-cn-shanghai.aliyuncs.com", access_key_id, access_key_secret)
	if err != nil {
		log.Fatalln(err)
	}
	bucket, err := client.Bucket("dragonsreach")
	if err != nil {
		log.Fatalln(err)
	}
	objects, err := bucket.ListObjects()
	if err != nil {
		log.Fatalln(err)
	}
	log.Println(objects)

	for _, object := range objects.Objects {
		fmt.Println("Objects: ", object.Key)
	}

	err = bucket.UploadFile("<remote_object_id>", "<local_file_path>",
		10*1024*1024,                                 // partSize
		oss.Routines(4),                              // routine numbers
		oss.Checkpoint(true, "<local_file_path>.cp")) // resumable, and context would be stored in a file <local_file_path> with "cp" suffixed
	if err != nil {
		log.Println(err)
	}

	// TODO: Add progress bar
	// TODO: Resolve timeout issue

}
示例#4
0
// NewAliyunOSS 新建阿里云对象存储服务
func NewAliyunOSS(config AliyunOSSConfig) *AliyunOSS {

	client, err := oss.New(config.EndPoint, config.AccessKeyID, config.AccessKeySecret)
	if err != nil {
		log.Fatal(err)
	}

	bucket, err := client.Bucket(config.Bucket)
	if err != nil {
		log.Fatal(err)
	}

	return &AliyunOSS{config: config, bucket: bucket}
}
示例#5
0
func (a *aliyundesc) New() (factory.DrvInterface, error) {
	client, err := oss.New(setting.Endpoint, setting.AccessKeyID, setting.AccessKeysecret)
	if err != nil {
		return nil, err
	}

	bucket, err := client.Bucket(setting.Bucket)
	if err != nil {
		return nil, err
	}

	return &aliyundesc{
		Cli:    client,
		Bucket: bucket,
	}, nil
}
示例#6
0
func NewOssClient(endpoint string) (*OssClient, error) {
	if !strings.HasPrefix(
		"http://",
		strings.ToLower(endpoint),
	) {
		endpoint = fmt.Sprintf("http://%s", endpoint)
	}

	var err error
	o := new(OssClient)
	o.Client, err = oss.New(
		endpoint,
		beego.AppConfig.String("aliapi::apikey"),
		beego.AppConfig.String("aliapi::secret"),
	)
	return o, err
}
示例#7
0
func NewBucket() (bucket *oss.Bucket, err error) {
	endpoint := beego.AppConfig.String("oss.endpoint")
	accesskeyid := beego.AppConfig.String("oss.accesskeyid")
	accesskeysecret := beego.AppConfig.String("oss.accesskeysecret")

	client, err := oss.New(endpoint, accesskeyid, accesskeysecret)
	if err != nil {
		err = fmt.Errorf("init storage fail: " + err.Error())
		return
	}
	bucketname := beego.AppConfig.String("oss.bucket")

	bucket, err = client.Bucket(bucketname)
	if err != nil {
		err = fmt.Errorf("init bucket fail: " + err.Error())
	}
	return
}
示例#8
0
func aliyunsave(file string) (url string, err error) {

	client, err := oss.New(setting.Endpoint, setting.AccessKeyID, setting.AccessKeysecret)
	if err != nil {
		return "", err
	}

	bucket, err := client.Bucket(setting.Bucket)
	if err != nil {
		return "", err
	}

	var key string
	//get the filename from the file , eg,get "1.txt" from /home/liugenping/1.txt
	for _, key = range strings.Split(file, "/") {

	}
	opath := "/" + setting.Bucket + "/" + key
	url = "http://" + setting.Endpoint + opath

	fd, err := os.Open(file)
	if err != nil {
		return "", err
	}
	defer fd.Close()
	fi, err := fd.Stat()
	if err != nil {
		return "", err
	}
	fsize := fi.Size()

	if fsize < (1 << 27) {
		err = bucket.PutObject(key, fd)
	} else {
		err = bucket.UploadFile(key, file, 2<<22)
	}

	if nil != err {
		return "", err
	} else {
		return url, nil
	}
}
示例#9
0
func NewDownloadInfo(file string) (*DownloadInfo, error) {
	info := new(DownloadInfo)
	err := util.ReadJsonFile(file, info)
	if err != nil {
		return nil, err
	}
	if info.AppKey == "" || info.AppSecret == "" || info.BucketName == "" || info.EndPoint == "" || info.EncryptKey == "" {
		return nil, errors.New("missing required download information")
	}

	client, err := oss.New(info.EndPoint, info.AppKey, info.AppSecret)
	if err != nil {
		return nil, errors.New("can't connect to aliyun oss")
	}

	bucket, err := client.Bucket(info.BucketName)
	if err != nil {
		return nil, errors.New("can't get specified bucket")
	}
	info.Bucket = bucket

	return info, err
}