MCPcopy Index your code
hub / github.com/TruthHun/BookStack / MoveToOss

Method MoveToOss

models/store/oss.go:82–123  ·  view source on GitHub ↗

文件移动到OSS进行存储[如果是图片文件,不要使用gzip压缩,否则在使用阿里云OSS自带的图片处理功能无法处理图片] @param local 本地文件 @param save 存储到OSS的文件 @param IsPreview 是否是预览,是预览的话,则上传到预览的OSS,否则上传到存储的OSS。存储的OSS,只作为文档的存储,以供下载,但不提供预览等访问,为私有 @param IsDel 文件上传后,是否删除本地文件

(local, save string, IsDel bool, IsGzip ...bool)

Source from the content-addressed store, hash-verified

80//@param IsDel 文件上传后,是否删除本地文件
81//@param IsGzip 是否做gzip压缩,做gzip压缩的话,需要修改oss中对象的响应头,设置gzip响应
82func (o *Oss) MoveToOss(local, save string, IsDel bool, IsGzip ...bool) error {
83 isgzip := false
84 //如果是开启了gzip,则需要设置文件对象的响应头
85 if len(IsGzip) > 0 && IsGzip[0] == true {
86 isgzip = true
87 }
88
89 bucket, err := o.GetBucket()
90 if err != nil {
91 beego.Error("OSS Bucket初始化错误:%v", err.Error())
92 return err
93 }
94 //在移动文件到OSS之前,先压缩文件
95 if isgzip {
96 if bs, err := ioutil.ReadFile(local); err != nil {
97 beego.Error(err.Error())
98 isgzip = false //设置为false
99 } else {
100 var by bytes.Buffer
101 w := gzip.NewWriter(&by)
102 defer w.Close()
103 w.Write(bs)
104 w.Flush()
105 ioutil.WriteFile(local, by.Bytes(), 0777)
106 }
107 }
108 err = bucket.PutObjectFromFile(save, local)
109 if err != nil {
110 beego.Error("文件移动到OSS失败:", err.Error())
111 return err
112 }
113 //如果是开启了gzip,则需要设置文件对象的响应头
114 if isgzip {
115 bucket.SetObjectMeta(save, oss.ContentEncoding("gzip")) //设置gzip响应头
116 }
117
118 if err == nil && IsDel {
119 err = os.Remove(local)
120 }
121
122 return err
123}
124
125//从OSS中删除文件
126//@param object 文件对象

Callers 15

UploadMethod · 0.80
QrcodeMethod · 0.80
UploadMethod · 0.80
UploadCoverMethod · 0.80
unzipToDataMethod · 0.80
loadByFolderMethod · 0.80
UpdateCateIconMethod · 0.80
ChangeAvatarMethod · 0.80
CrawlHtml2MarkdownFunction · 0.80
HandleSVGFunction · 0.80
UploadFileFunction · 0.80
ReleaseContentMethod · 0.80

Calls 2

GetBucketMethod · 0.95
SetObjectMetaMethod · 0.80

Tested by

no test coverage detected