Browse Source

Merge pull request #548 from neo-6/NeoSunDevelop

[修复阿里云OSS图片上传路径不一致错误]
main
奇淼(piexlmax 3 years ago
committed by GitHub
parent
commit
57c592f0da
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      server/config.yaml
  2. 1
      server/config/oss.go
  3. 4
      server/utils/upload/aliyun_oss.go
  4. 2
      web/src/components/upload/image.vue

1
server/config.yaml

@ -104,6 +104,7 @@ aliyun-oss:
access-key-secret: 'yourAccessKeySecret' access-key-secret: 'yourAccessKeySecret'
bucket-name: 'yourBucketName' bucket-name: 'yourBucketName'
bucket-url: 'yourBucketUrl' bucket-url: 'yourBucketUrl'
base-path: 'yourBasePath'
# tencent cos configuration # tencent cos configuration
tencent-cos: tencent-cos:

1
server/config/oss.go

@ -20,6 +20,7 @@ type AliyunOSS struct {
AccessKeySecret string `mapstructure:"access-key-secret" json:"accessKeySecret" yaml:"access-key-secret"` AccessKeySecret string `mapstructure:"access-key-secret" json:"accessKeySecret" yaml:"access-key-secret"`
BucketName string `mapstructure:"bucket-name" json:"bucketName" yaml:"bucket-name"` BucketName string `mapstructure:"bucket-name" json:"bucketName" yaml:"bucket-name"`
BucketUrl string `mapstructure:"bucket-url" json:"bucketUrl" yaml:"bucket-url"` BucketUrl string `mapstructure:"bucket-url" json:"bucketUrl" yaml:"bucket-url"`
BasePath string `mapstructure:"base-path" json:"basePath" yaml:"base-path"`
} }
type TencentCOS struct { type TencentCOS struct {
Bucket string `mapstructure:"bucket" json:"bucket" yaml:"bucket"` Bucket string `mapstructure:"bucket" json:"bucket" yaml:"bucket"`

4
server/utils/upload/aliyun_oss.go

@ -6,7 +6,6 @@ import (
"github.com/aliyun/aliyun-oss-go-sdk/oss" "github.com/aliyun/aliyun-oss-go-sdk/oss"
"go.uber.org/zap" "go.uber.org/zap"
"mime/multipart" "mime/multipart"
"path/filepath"
"time" "time"
) )
@ -27,7 +26,8 @@ func (*AliyunOSS) UploadFile(file *multipart.FileHeader) (string, string, error)
} }
defer f.Close() // 创建文件 defer 关闭 defer f.Close() // 创建文件 defer 关闭
// 上传阿里云路径 文件名格式 自己可以改 建议保证唯一性 // 上传阿里云路径 文件名格式 自己可以改 建议保证唯一性
yunFileTmpPath := filepath.Join("uploads", time.Now().Format("2006-01-02")) + "/" + file.Filename
//yunFileTmpPath := filepath.Join("uploads", time.Now().Format("2006-01-02")) + "/" + file.Filename
yunFileTmpPath := global.GVA_CONFIG.AliyunOSS.BasePath + "/" + "uploads" + "/" + time.Now().Format("2006-01-02") + "/" + file.Filename
// 上传文件流。 // 上传文件流。
err = bucket.PutObject(yunFileTmpPath, f) err = bucket.PutObject(yunFileTmpPath, f)

2
web/src/components/upload/image.vue

@ -17,7 +17,7 @@
:before-upload="beforeImageUpload" :before-upload="beforeImageUpload"
:multiple="false" :multiple="false"
> >
<img v-if="imageUrl" :src="path + imageUrl" class="image">
<img v-if="imageUrl" :src="imageUrl" class="image">
<i v-else class="el-icon-plus image-uploader-icon" /> <i v-else class="el-icon-plus image-uploader-icon" />
</el-upload> </el-upload>
</div> </div>

Loading…
Cancel
Save