// ossclient
package util

import (
	"fmt"
	"io/ioutil"
	"os"
	"qfw/util"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

var (
	ossEndpoint        = "oss-cn-beijing-internal.aliyuncs.com" //http://oss-cn-beijing.aliyuncs.com"
	//ossEndpointTest    = "topjy.oss-cn-beijing.aliyuncs.com"
	ossEndpointTest    = "oss-cn-beijing.aliyuncs.com"
	ossAccessKeyId     = "LTAI4G5x9aoZx8dDamQ7vfZi"
	ossAccessKeySecret = "Bk98FsbPYXcJe72n1bG3Ssf73acuNh"
	ossBucketName      = "topjy"
	ossclient          *oss.Client
)

func InitOss(isTest bool) {
	if isTest{
		ossEndpoint = ossEndpointTest
	}
	client, err := oss.New(ossEndpoint, ossAccessKeyId, ossAccessKeySecret)
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	ossclient = client
}

func OssGetObject(objectName string) string {
	util.Catch()
	// 获取存储空间。
	bucket, err := ossclient.Bucket(ossBucketName)
	if err != nil {
		fmt.Println("Error:", err)
		return ""
	}

	// 下载文件到流。
	body, err := bucket.GetObject(objectName)
	if err != nil {
		fmt.Println("Error:", err)
		return ""
	}
	defer body.Close()
	data, err := ioutil.ReadAll(body)
	if err != nil {
		fmt.Println("Error:", err)
		return ""
	}
	return string(data)
}
