1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package utils
- import (
- "gd_management_gateway/common.in/config"
- "github.com/aliyun/aliyun-oss-go-sdk/oss"
- )
- const (
- ossId = "LTAI5tAAvdM9p9h3eqRkCMW6"
- ossSecret = "NybirnmP0rGmkCDWkA6dEIpioShOGr"
- ossEndpoint = "oss-cn-hangzhou.aliyuncs.com"
- ossBucket = "dev-gdata-image"
- )
- func isOssConfigured() bool {
- if config.Conf.Oss.Endpoint == "" ||
- config.Conf.Oss.Bucket == "" ||
- config.Conf.Oss.Id == "" ||
- config.Conf.Oss.Secret == "" {
- return false
- }
- return true
- }
- // OssGetUrl 获取oss下载地址url前缀
- func OssGetUrl() string {
- if isOssConfigured() == false {
- return "https://" + ossBucket + "." + ossEndpoint
- }
- return "https://" + config.Conf.Oss.Bucket + "." + config.Conf.Oss.Endpoint
- }
- func ossGetInfo() (id, secret, bucket, endpoint string) {
- id = ossId
- secret = ossSecret
- bucket = ossBucket
- endpoint = ossEndpoint
- if isOssConfigured() {
- id = config.Conf.Oss.Id
- secret = config.Conf.Oss.Secret
- bucket = config.Conf.Oss.Bucket
- endpoint = config.Conf.Oss.Endpoint
- }
- return id, secret, bucket, endpoint
- }
- // OssUploadFile 上传文件到oss服务器
- func OssUploadFile(path string, fileName string) error {
- id, secret, buck, endpoint := ossGetInfo()
- client, err := oss.New(endpoint, id, secret)
- if err != nil {
- return err
- }
- // 获取存储空间。
- bucket, err := client.Bucket(buck)
- if err != nil {
- return err
- }
- err = bucket.PutObjectFromFile(fileName, path)
- return err
- }
- // OssDeleteFile 在oss服务器上删除文件
- func OssDeleteFile(fileName string) error {
- id, secret, buck, endpoint := ossGetInfo()
- client, err := oss.New(endpoint, id, secret)
- if err != nil {
- return err
- }
- // 获取存储空间。
- bucket, err := client.Bucket(buck)
- if err != nil {
- return err
- }
- err = bucket.DeleteObject(fileName)
- return err
- }
|