@@ -179,8 +179,12 @@ type Uploader struct {
179179// u.PartSize = 64 * 1024 * 1024 // 64MB per part
180180// })
181181func NewUploader (cfg aws.Config , options ... func (* Uploader )) * Uploader {
182+ return newUploader (s3 .New (cfg ), options ... )
183+ }
184+
185+ func newUploader (client s3iface.ClientAPI , options ... func (* Uploader )) * Uploader {
182186 u := & Uploader {
183- S3 : s3 . New ( cfg ) ,
187+ S3 : client ,
184188 PartSize : DefaultUploadPartSize ,
185189 Concurrency : DefaultUploadConcurrency ,
186190 LeavePartsOnError : false ,
@@ -214,20 +218,7 @@ func NewUploader(cfg aws.Config, options ...func(*Uploader)) *Uploader {
214218// u.PartSize = 64 * 1024 * 1024 // 64MB per part
215219// })
216220func NewUploaderWithClient (svc s3iface.ClientAPI , options ... func (* Uploader )) * Uploader {
217- u := & Uploader {
218- S3 : svc ,
219- PartSize : DefaultUploadPartSize ,
220- Concurrency : DefaultUploadConcurrency ,
221- LeavePartsOnError : false ,
222- MaxUploadParts : MaxUploadParts ,
223- BufferProvider : defaultUploadBufferProvider (),
224- }
225-
226- for _ , option := range options {
227- option (u )
228- }
229-
230- return u
221+ return newUploader (svc , options ... )
231222}
232223
233224// Upload uploads an object to S3, intelligently buffering large files into
@@ -356,6 +347,8 @@ type uploader struct {
356347
357348 readerPos int64 // current reader position
358349 totalSize int64 // set to -1 if the size is not known
350+
351+ bufferPool sync.Pool
359352}
360353
361354// internal logic for deciding whether to upload a single part or use a
@@ -392,6 +385,10 @@ func (u *uploader) init() error {
392385 u .cfg .PartSize = DefaultUploadPartSize
393386 }
394387
388+ u .bufferPool = sync.Pool {
389+ New : func () interface {} { return make ([]byte , u .cfg .PartSize ) },
390+ }
391+
395392 // Try to get the total size for some optimizations
396393 return u .initSize ()
397394}
@@ -460,11 +457,13 @@ func (u *uploader) nextReader() (io.ReadSeeker, int, func(), error) {
460457 return reader , int (n ), cleanup , err
461458
462459 default :
463- part := make ( []byte , u . cfg . PartSize )
460+ part := u . bufferPool . Get ().( []byte )
464461 n , err := readFillBuf (r , part )
465462 u .readerPos += int64 (n )
466463
467- cleanup := func () {}
464+ cleanup := func () {
465+ u .bufferPool .Put (part )
466+ }
468467
469468 return bytes .NewReader (part [0 :n ]), n , cleanup , err
470469 }
0 commit comments