Implémenter upload de document par API

`POST /bucket/:bucket` permet d'upload un FormFile 'file' dans le
':bucket' spécifié. Le nom de fichier sera utilisé comme descriptif alors il est de bonne pratique de donner un nom descriptif.

Comme le reste du API, doit être authentifié par key-auth
This commit is contained in:
Victor Lacasse-Beaudoin 2023-04-24 18:29:05 -04:00
parent 902e57cb1f
commit e21fe3be11

View file

@ -116,7 +116,7 @@ func RunServer() {
groupV1.GET("/bucket/:bucket", handleV1BucketRead) groupV1.GET("/bucket/:bucket", handleV1BucketRead)
groupV1.POST("/bucket/:bucket/:document", handleV1DocumentCreate) groupV1.POST("/bucket/:bucket", handleV1DocumentCreate)
groupV1.GET("/bucket/:bucket/:document", handleV1DocumentRead) groupV1.GET("/bucket/:bucket/:document", handleV1DocumentRead)
@ -305,8 +305,59 @@ func handleV1BucketRead(c echo.Context) error {
} }
func handleV1DocumentCreate(c echo.Context) error { func handleV1DocumentCreate(c echo.Context) error {
return c.JSON(http.StatusNotImplemented, map[string]string{ documents_endpoint := viper.GetString("server.documents.endpoint")
"message": "Not Implemented", documents_access_key_id := viper.GetString("server.documents.access_key_id")
documents_secret_access_key := viper.GetString("server.documents.secret_access_key")
documents_use_ssl := viper.GetBool("server.documents.use_ssl")
//bucket := c.FormValue("bucket")
bucket := c.Param("bucket")
form_file, err := c.FormFile("file")
if err != nil {
return err
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Initialize minio client object
client, err := minio.New(documents_endpoint, &minio.Options{
Creds: credentials.NewStaticV4(documents_access_key_id, documents_secret_access_key, ""),
Secure: documents_use_ssl,
})
if err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{
"message": "Error during minio#New",
})
}
src, err := form_file.Open()
if err != nil {
return err
}
defer src.Close()
info, err := client.PutObject(ctx, bucket, form_file.Filename, src, form_file.Size, minio.PutObjectOptions{
ContentType: form_file.Header.Get("Content-Type"),
})
if err != nil {
//TODO
log.Println(err)
return c.JSON(http.StatusInternalServerError, map[string]string{
"message": "Error during minio#PutObject",
})
}
return c.JSON(http.StatusOK, map[string]interface{}{
"message": "ok",
"info": map[string]interface{}{
"bucket": info.Bucket,
"key": info.Key,
"size": info.Size,
},
}) })
} }