1

I have tried to create a REST API to upload images to digital ocean space and try it with postman, but I get an error:

"NotImplemented: Server does not support one or more requested headers. Please see https://developers.digitalocean.com/documentation/spaces/#aws-s3-compatibility\n\tstatus code: 501, request id: , host id: "
func UploadImage() gin.HandlerFunc {
    return func(c *gin.Context) {
        _, cancel := context.WithTimeout(context.Background(), 10*time.Second)
        defer cancel()

        file, header, err := c.Request.FormFile("image")
        defer func(file multipart.File) {
            err := file.Close()
            if err != nil {
                fmt.Print(err.Error())
                return
            }
        }(file)
        if err != nil {
            fmt.Print(err.Error())
            return
        }

        fileExt := filepath.Ext(header.Filename)
        originalFileName := strings.TrimSuffix(filepath.Base(header.Filename), filepath.Ext(header.Filename))
        now := time.Now()
        filename := strings.ReplaceAll(strings.ToLower(originalFileName), " ", "-") + "-" + fmt.Sprintf("%v", now.Unix()) + fileExt

        fmt.Print(filename)

        spaceName := os.Getenv("DO_SPACE_NAME")
        spaceRegion := os.Getenv("DO_SPACE_REGION")
        spaceEndPoint := os.Getenv("SPACE_ENDPOINT")
        accessKey := os.Getenv("ACCESS_KEY")
        secretKey := os.Getenv("SECRET_KEY")
        token := os.Getenv("DO_SPACE_TOKEN")

        s3Config := &aws.Config{
            Credentials:      credentials.NewStaticCredentials(accessKey, secretKey, token),
            Endpoint:         aws.String(spaceEndPoint),
            Region:           aws.String(spaceRegion),
            S3ForcePathStyle: aws.Bool(false),
        }

        newSession := session.New(s3Config)
        s3Client := s3.New(newSession)

        object := s3.PutObjectInput{
            Bucket: aws.String(spaceName),
            Key:    aws.String("photo"),
            Body:   file,                 
            ACL:    aws.String("public"),  
            Metadata: map[string]*string{
                "x-amz-meta-my-key": aws.String("your-value"),
            },
        }

        response, err := s3Client.PutObject(&object)
        if err != nil {
            fmt.Print(err.Error())
            return
        }

        fmt.Print(response.String())
    }
}

Does anyone have an idea of what could go wrong? Is something wrong with my digital ocean settings?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Afdal
  • 501
  • 9
  • 19

0 Answers0