用golang gin框架 写一个分片上传阿里云OSS功能,注意 上传的文件很大 5G左右
时间: 2023-10-15 10:06:51 浏览: 244
golang阿里云oss图片上传
好的,下面是一个示例代码,使用 `gin` 框架实现分片上传阿里云OSS功能:
```go
package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strconv"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"github.com/gin-gonic/gin"
)
const (
MaxUploadSize = 5 * 1024 * 1024 * 1024 // 5 GB
ChunkSize = 10 * 1024 * 1024 // 10 MB
)
func main() {
r := gin.Default()
r.POST("/upload", UploadHandler)
if err := r.Run(":8080"); err != nil {
log.Fatal(err)
}
}
func UploadHandler(c *gin.Context) {
file, header, err := c.Request.FormFile("file")
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
defer file.Close()
fileSize, err := strconv.Atoi(c.Request.FormValue("size"))
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
fileName := header.Filename
filePath := filepath.Join(os.TempDir(), fileName)
dst, err := os.Create(filePath)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
defer dst.Close()
if _, err := io.Copy(dst, file); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
if int64(fileSize) > MaxUploadSize {
c.JSON(http.StatusBadRequest, gin.H{"error": "file size exceeds the maximum limit"})
return
}
client, err := oss.New("oss-cn-hangzhou.aliyuncs.com", "<access_key>", "<access_secret>")
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
bucket, err := client.Bucket("<bucket_name>")
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
chunkSize := ChunkSize
totalPartsNum := (fileSize + chunkSize - 1) / chunkSize
objectName := fileName
// Initiate the multipart upload
imur, err := bucket.InitiateMultipartUpload(objectName)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
var uploadedParts []oss.UploadPart
var partNumber int
// Upload each part
for i := 0; i < totalPartsNum; i++ {
partSize := chunkSize
if i == totalPartsNum-1 {
partSize = fileSize - i*chunkSize
}
partNumber = i + 1
// Open the file and read the bytes
file, err := os.Open(filePath)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
defer file.Close()
// Seek to the start of the part
offset := int64(i * chunkSize)
if _, err := file.Seek(offset, io.SeekStart); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
// Read the part into memory
partBuffer := make([]byte, partSize)
if _, err := file.Read(partBuffer); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
// Upload the part to OSS
uploadPart, err := bucket.UploadPart(imur, partBuffer, partNumber)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
uploadedParts = append(uploadedParts, uploadPart)
}
// Complete the multipart upload
cmur, err := bucket.CompleteMultipartUpload(imur, uploadedParts)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
fmt.Println(cmur)
c.JSON(http.StatusOK, gin.H{"message": "upload success"})
}
```
以上代码中,实现了以下功能:
1. 从请求中获取文件和大小
2. 将文件存储到本地磁盘中
3. 初始化分片上传,并上传每个分片
4. 完成分片上传,将分片合并成一个对象
需要注意的是,代码中使用了阿里云OSS的 SDK 进行操作,因此需要先在阿里云控制台上创建 OSS Bucket,并在代码中填写正确的 access_key、access_secret 和 bucket_name。
另外,由于上传的文件可能很大,因此需要设置分片大小,本例中设置为每个分片的大小为10MB。同时,为了避免服务器崩溃,还需要设置文件大小的最大限制,本例中设置为5GB。
阅读全文