123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package utils
- import (
- "adm-ods/common.in/config"
- "github.com/aliyun/aliyun-oss-go-sdk/oss"
- )
- const (
- ossId = "LTAI4Fjs1bNMd8k8LT41Fxxn"
- ossSecret = "WlQJwVbxHIY4TIZwMIFA5QCye6n0Pl"
- ossEndpoint = "oss-cn-shanghai.aliyuncs.com"
- ossBucket = "gd-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
- }*/
- func OssDownloadFile(objectName, downloadedFileName string) error {
- client, err := oss.New(config.Conf.Oss.EndPoint, config.Conf.Oss.AccessKey, config.Conf.Oss.AccessSecret)
- if err != nil {
- return err
- }
- // 获取存储空间。
- bucket, err := client.Bucket(config.Conf.Oss.Bucket)
- if err != nil {
- return err
- }
- // 下载文件。
- err = bucket.GetObjectToFile(objectName, downloadedFileName)
- if err != nil {
- return err
- }
- return nil
- }
|