以下是使用 Go 语言操作亚马逊 S3 对象云存储的详细步骤和示例代码:
解决思路:
- 安装必要的 Go 语言包,这里我们将使用
aws-sdk-go
包来与 Amazon S3 进行交互。 - 配置 AWS 凭证,包括访问密钥和秘密访问密钥,以及 AWS 区域。
- 使用
aws-sdk-go
创建 S3 客户端。 - 通过 S3 客户端执行各种操作,如上传文件、下载文件、列出存储桶中的对象等。
示例代码:
package s3Uploader
import (
"bytes"
"errors"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/chai2010/webp" // WebP 库
"github.com/nfnt/resize"
"html"
"image"
"image/jpeg"
"image/png"
"net/http"
"os"
"strings"
"upos/src/config"
)
type S3Uploader struct {
Name string
Scale config.Scale
Cfg config.Sharktech
uploader *s3manager.Uploader
ContentType string
}
var (
ErrorUnsupportedImageFormat = errors.New("不支持的图片格式")
)
func NewS3Uploader(cfg config.Sharktech) (*S3Uploader, error) {
sess, err := session.NewSession(&aws.Config{
Region: aws.String(cfg.S3.Region),
Endpoint: aws.String(cfg.S3.Endpoint),
/*
Set this to `true` to force the request to use path-style addressing,
i.e., `http://s3.amazonaws.com/BUCKET/KEY`. By default, the S3 client
will use virtual hosted bucket addressing when possible
(`http://BUCKET.s3.amazonaws.com/KEY`).
*/
S3ForcePathStyle: aws.Bool(true),
Credentials: credentials.NewStaticCredentials(cfg.S3.AccessId, cfg.S3.AccessKey, ""),
})
if err != nil {
return nil, err
}
uploader := s3manager.NewUploader(sess)
return &S3Uploader{
Cfg: cfg,
Scale: config.YC.Scale,
uploader: uploader,
}, nil
}
func (u *S3Uploader) ConvertWebPToJPG(file *os.File) (image.Image, error) {
// 解码 WebP 文件为 image.Image
img, err := webp.Decode(file)
if err != nil {
return nil, fmt.Errorf("解码 WebP 文件失败: %w", err)
}
return img, nil
}
func (u *S3Uploader) ScaleImage(img image.Image, maxWidth, maxHeight uint) image.Image {
// 获取原始图片的宽度和高度
originalWidth := img.Bounds().Dx()
originalHeight := img.Bounds().Dy()
// 计算缩放比例
ratio := float64(originalWidth) / float64(originalHeight)
var newWidth, newHeight uint
if ratio > 1 { // 图片宽 > 高
newWidth = maxWidth
newHeight = uint(float64(maxWidth) / ratio)
} else { // 图片高 >= 宽
newHeight = maxHeight
newWidth = uint(float64(maxHeight) * ratio)
}
return resize.Resize(newWidth, newHeight, img, resize.Lanczos3)
}
// 根据图片格式生成 Content-Type
func (u *S3Uploader) getContentType(format string) string {
switch strings.ToLower(format) {
case ".jpg", ".jpeg":
return "image/jpeg"
case ".png":
return "image/png"
case ".webp":
return "image/webp"
default:
return "application/octet-stream" // 默认值
}
}
// 根据文件扩展名选择编码格式
func (u *S3Uploader) encodeImage(buf *bytes.Buffer, img image.Image, format string) error {
switch strings.ToLower(format) {
case ".jpg", ".jpeg":
return jpeg.Encode(buf, img, nil)
case ".png":
return png.Encode(buf, img)
case ".webp":
return webp.Encode(buf, img, &webp.Options{Lossless: true})
default:
return ErrorUnsupportedImageFormat
}
}
// DetectFormatAndDecode 根据文件头判断图片格式并解码
func (u *S3Uploader) DetectFormatAndDecode(filePath string) (image.Image, string, error) {
// 打开文件
file, err := os.Open(filePath)
if err != nil {
return nil, "", err
}
defer file.Close()
// 读取文件头前 12 个字节
header := make([]byte, 12)
_, err = file.Read(header)
if err != nil {
return nil, "", err
}
// 重置文件读取位置
_, err = file.Seek(0, 0)
if err != nil {
return nil, "", err
}
// 根据文件头判断格式
var img image.Image
var ext string
switch {
case bytes.HasPrefix(header, []byte("\xFF\xD8\xFF")):
// JPEG 文件头
img, err = jpeg.Decode(file)
ext = ".jpg"
case bytes.HasPrefix(header, []byte("\x89PNG\r\n\x1a\n")):
// PNG 文件头
img, err = png.Decode(file)
ext = ".png"
case bytes.HasPrefix(header, []byte("RIFF")) && bytes.Contains(header[8:], []byte("WEBP")):
// WebP 文件头
img, err = webp.Decode(file)
ext = ".webp"
default:
err = errors.New("unsupported image format")
}
return img, ext, err
}
func (u *S3Uploader) Resize(filePath string) (buf bytes.Buffer, err error) {
newImg, ext, err := u.DetectFormatAndDecode(filePath)
if err != nil {
return bytes.Buffer{}, err
}
u.ContentType = u.getContentType(ext)
// 将缩放后的图片编码为 JPEG 格式
err = u.encodeImage(&buf, newImg, ext)
if err != nil {
return
}
return buf, nil
}
func (u *S3Uploader) Upload(filePath string, key string) error {
buf, err := u.Resize(filePath)
if err != nil {
return err
}
_, err = u.uploader.Upload(&s3manager.UploadInput{
ACL: aws.String("public-read"), // 设置为 public-read
Bucket: aws.String(u.Cfg.S3.Bucket),
Key: aws.String(key),
// TODO:: 不传Content-Type的好处是WP站点不好外链
// ContentType: aws.String(u.ContentType),
Body: bytes.NewReader(buf.Bytes()),
})
if err != nil {
return err
}
return nil
}
func (u *S3Uploader) DownloadAndUpload(imageUrl string, key string) error {
buf, err := u.DownloadAndResizeImage(imageUrl)
if err != nil {
return err
}
_, err = u.uploader.Upload(&s3manager.UploadInput{
ACL: aws.String("public-read"), // 设置为 public-read
Bucket: aws.String(u.Cfg.S3.Bucket),
Key: aws.String(key),
// TODO:: 不传Content-Type的好处是WP站点不好外链
// ContentType: aws.String(u.ContentType),
Body: bytes.NewReader(buf.Bytes()),
})
if err != nil {
return err
}
return nil
}
func (u *S3Uploader) DownloadAndResizeImage(imgURL string) (buf bytes.Buffer, err error) {
imgURL = html.UnescapeString(imgURL)
// 创建请求
req, err := http.NewRequest("GET", imgURL, nil)
if err != nil {
return
}
// 设置 Referer 或 User-Agent,模拟合法请求
req.Header.Set("Referer", "https://www.google.com")
req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36")
// 发起请求
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
// 检查响应的Content-Type
contentType := resp.Header.Get("Content-Type")
// log.Println("Content-Type:", contentType)
if !strings.HasPrefix(contentType, "image/") {
err = fmt.Errorf("invalid content type: %s", contentType)
return
}
// 读取图片
img, _, err := image.Decode(resp.Body)
if err != nil {
return
}
newImg := u.ScaleImage(img, u.Scale.Width, u.Scale.Height)
// 将缩放后的图片编码为 JPG 格式
err = u.encodeImage(&buf, newImg, ".jpg")
if err != nil {
return
}
return buf, nil
}