package aliyun

import (
	"github.com/aliyun/aliyun-oss-go-sdk/oss"
	"io"
	"openapi/pkg/constant"
)

/*
AK  AccessKey 完全访问权限
STS (Security Token Service)  临时访问控制
*/
type ossConfig struct {
	AccessID    string
	AccessKey   string
	RoleAcs     string
	EndPoint    string
	BuckName    string
	CallbackUrl string
}

func DefaultBucket() (*oss.Bucket, error) {
	client, err := oss.New(constant.OssEndPoint, constant.AccessKeyID, constant.AccessKeySecret)
	if err != nil {
		return nil, err
	}

	// Get bucket
	bucket, err := client.Bucket(constant.BuckName)
	if err != nil {
		return nil, err
	}

	return bucket, nil
}

type Object struct {
	Key   string
	Value io.Reader
}

// CreateObjects creates some objects
func CreateObjects(bucket *oss.Bucket, objects ...Object) error {
	for _, object := range objects {
		err := bucket.PutObject(object.Key, object.Value)
		if err != nil {
			return err
		}
	}
	return nil
}

// DeleteObjects deletes some objects.
// object = key
func DeleteObjects(bucket *oss.Bucket, objects ...string) error {
	for _, object := range objects {
		err := bucket.DeleteObject(object)
		if err != nil {
			return err
		}
	}
	return nil
}