我用 OPTIONS 方法得到一个 204,但似乎没有达到终点
只需像这样构建一个简单的文件上传端点:
package main
import (
"cloud.google.com/go/storage"
"github.com/gin-gonic/gin"
"github.com/gin-contrib/cors"
"google.golang.org/api/option"
"io"
"log"
)
const uploadBucket = "some-cool-bucket-name"
const uploadApiKey = "some-advanced-key"
func main() {
router := gin.Default()
rg := router.Group("api/v1/photo")
{
rg.PATCH("/", uploadFile)
}
router.Use(cors.Default())
router.Run()
}
func uploadFile(c *gin.Context) {
mr, e := c.Request.MultipartReader()
if e != nil {
panic("Error reading request")
}
client, e := storage.NewClient(c, option.WithAPIKey(uploadApiKey))
bucket := client.Bucket(uploadBucket)
for {
p, e := mr.NextPart()
if e == io.EOF {
break
} else if e != nil {
panic("Error processing file")
}
w := bucket.Object(p.FileName()).NewWriter(c)
if _, e := io.Copy(w, p); e != nil {
panic("Error during chunk upload")
} else if e := w.Close(); e != nil {
panic("Could not finalize chunk writing")
}
}
}
任何想法为什么?
呼啦一阵风
函数式编程
相关分类