2025-02-20 08:24:49 +00:00
|
|
|
|
package auth
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2025-02-20 08:38:16 +00:00
|
|
|
|
|
2025-02-20 08:24:49 +00:00
|
|
|
|
"io"
|
|
|
|
|
"net/http"
|
|
|
|
|
"os"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type OOInf struct {
|
|
|
|
|
Point string
|
|
|
|
|
Img string
|
|
|
|
|
File string
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-20 08:38:16 +00:00
|
|
|
|
// DownImg 重定向到指定位置
|
2025-02-20 08:24:49 +00:00
|
|
|
|
func DownImg(c *gin.Context) {
|
|
|
|
|
|
|
|
|
|
var req OOInf
|
|
|
|
|
|
|
|
|
|
if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
|
|
|
|
|
service.Error(c, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
client, err := oss.New(req.Point, "LTAI5tHfjSmWXHqfWgaL7Uo5", "kOPctFZ3DHsbdSSym1fLyDK39hkzPI")
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println("Error:", err)
|
|
|
|
|
service.Error(c, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 填写Bucket名称,例如examplebucket。
|
|
|
|
|
bucketName := "erp-k8s-store"
|
|
|
|
|
// 填写文件完整路径,例如exampledir/exampleobject.txt。文件完整路径中不能包含Bucket名称。
|
|
|
|
|
objectName := req.Img
|
|
|
|
|
// 下载OSS文件到本地文件,并保存到指定的本地路径中。如果指定的本地文件存在会覆盖,不存在则新建。
|
|
|
|
|
// 如果未指定本地路径,则下载后的文件默认保存到示例程序所属项目对应本地路径中。
|
|
|
|
|
|
|
|
|
|
// 获取存储空间。
|
|
|
|
|
bucket, err := client.Bucket(bucketName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 指定签名URL过期时间,单位为秒,本示例以设置签名ULR过期时间为30天为例。您可以根据实际业务场景,设置合理的过期时间。
|
|
|
|
|
signedURL, err := bucket.SignURL(objectName, oss.HTTPGet, 100)
|
|
|
|
|
fmt.Println(signedURL, err)
|
|
|
|
|
|
|
|
|
|
// 目标文件路径
|
|
|
|
|
filePath := req.File
|
|
|
|
|
|
|
|
|
|
// 发起GET请求
|
|
|
|
|
resp, err := http.Get(signedURL)
|
|
|
|
|
if err != nil {
|
|
|
|
|
// 处理错误
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
|
|
// 确保HTTP响应状态码为200
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
|
// 处理非200状态码
|
|
|
|
|
panic("bad status: " + resp.Status)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 创建或截断目标文件
|
|
|
|
|
out, err := os.Create(filePath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
// 处理错误
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
defer out.Close()
|
|
|
|
|
|
|
|
|
|
// 将响应体复制到文件
|
|
|
|
|
_, err = io.Copy(out, resp.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
// 处理错误
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 输出成功信息
|
|
|
|
|
service.Success(c, "")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func DownImgV2(c *gin.Context) {
|
|
|
|
|
|
|
|
|
|
var req OOInf
|
|
|
|
|
|
|
|
|
|
if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
|
|
|
|
|
service.Error(c, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
client, err := oss.New(req.Point, "LTAI5tHfjSmWXHqfWgaL7Uo5", "kOPctFZ3DHsbdSSym1fLyDK39hkzPI")
|
|
|
|
|
//client, err := oss.New(req.Point, "xxxxx", "xxxx")
|
|
|
|
|
bucketName := "erp-k8s-store"
|
|
|
|
|
objectName := req.Img
|
|
|
|
|
filePath := req.File //本地下载的地址
|
|
|
|
|
|
|
|
|
|
// 获取存储空间。
|
|
|
|
|
bucket, err := client.Bucket(bucketName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rd, err := bucket.GetObject(objectName)
|
|
|
|
|
|
|
|
|
|
// 读取所有数据
|
|
|
|
|
_, err = io.ReadAll(rd)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println("Error reading data:", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the local file does not exist, create a new one. If it exists, overwrite it.
|
|
|
|
|
fd, err := os.OpenFile(filePath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, os.FileMode(0664))
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
defer fd.Close()
|
|
|
|
|
|
|
|
|
|
// Copy the data to the local file path.
|
|
|
|
|
_, err = io.Copy(fd, rd)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 输出成功信息
|
|
|
|
|
service.Success(c, "")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func DownImgV3(c *gin.Context) {
|
|
|
|
|
|
|
|
|
|
var req OOInf
|
|
|
|
|
|
|
|
|
|
if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
|
|
|
|
|
service.Error(c, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
client, err := oss.New(req.Point, "LTAI5tHfjSmWXHqfWgaL7Uo5", "kOPctFZ3DHsbdSSym1fLyDK39hkzPI")
|
|
|
|
|
//client, err := oss.New(req.Point, "xxxxx", "xxxx")
|
|
|
|
|
bucketName := "erp-k8s-store"
|
|
|
|
|
objectName := req.Img
|
|
|
|
|
filePath := req.File //本地下载的地址
|
|
|
|
|
|
|
|
|
|
// 获取存储空间。
|
|
|
|
|
bucket, err := client.Bucket(bucketName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bucket.GetObjectToFile(objectName, filePath)
|
|
|
|
|
|
|
|
|
|
//body, err := bucket.GetObject(objectName)
|
|
|
|
|
|
|
|
|
|
//data, err := ioutil.ReadAll(body)
|
|
|
|
|
|
|
|
|
|
// 指定签名URL过期时间,单位为秒,本示例以设置签名ULR过期时间为30天为例。您可以根据实际业务场景,设置合理的过期时间。
|
|
|
|
|
|
|
|
|
|
// 输出成功信息
|
|
|
|
|
service.Success(c, "")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
}
|