如何以正确的方式处理多个文件上传请求?

时间:2018-05-03 10:55:08

标签: nginx go file-upload digital-ocean aws-sdk-go

处理有大量图片上传的项目。每张图片都会调整大小和缩略图。

起初,我尝试使用单个请求处理所有图像的上传,当我在服务器中测试时,它会给出响应超时错误。

现在我发送要上传的每个文件的请求,这似乎会减慢服务器的速度。

你对我有什么建议吗?

我的代码:

func SavePhotoWithBuffer(bigBuf, thumbBuf bytes.Buffer, bigSize, thumbSize int64, fileName, contentType string) {
    params := &s3.PutObjectInput{
        Bucket:        aws.String("bucket"),
        Key:           aws.String(fileName),
        Body:          bytes.NewReader(bigBuf.Bytes()),
        ACL:           aws.String("public-read"),
        ContentLength: aws.Int64(bigSize),
        ContentType:   aws.String(contentType),
    }
    thumbParams := &s3.PutObjectInput{
        Bucket:        aws.String("bucket"),
        Key:           aws.String("t_" + fileName),
        Body:          bytes.NewReader(thumbBuf.Bytes()),
        ACL:           aws.String("public-read"),
        ContentLength: aws.Int64(thumbSize),
        ContentType:   aws.String(contentType),
    }
    UploadWithParams(params, thumbParams)
}

//I've used github.com/patrickmn/go-cache package to cache the session.
func Open() *s3.S3 {
    var cachedData = cacheservice.GetCache("s3session")
    if cachedData == nil {
        var sess *s3.S3
        accessKey := "accessKey"
        secretKey := "secretKey"
        token := ""
        cred := credentials.NewStaticCredentials(accessKey, secretKey, token)
        _, err := cred.Get()
        if err != nil {
            fmt.Printf("bad credentials: %s", err)
        }
        conf := aws.NewConfig().WithRegion("eu-west-1").WithCredentials(cred)
        conf.Endpoint = aws.String("ams3.digitaloceanspaces.com")
        sess = s3.New(session.New(), conf)
        cacheservice.SetCache("s3session", sess)
        return sess
    }
    return cachedData.(*s3.S3)
}


func UploadWithParams(params, thumbParams *s3.PutObjectInput) {
    var se = Open()
    var err error
    _, err = se.PutObject(params)
    if err != nil {
        fmt.Printf("bad response: %s", err)
    }
    _, err = se.PutObject(thumbParams)
    if err != nil {
        fmt.Printf("bad response: %s", err)
    }
}

0 个答案:

没有答案