only do multipart upload for large files

Signed-off-by: Robin Appelman <robin@icewind.nl>
This commit is contained in:
Robin Appelman 2017-09-21 14:06:59 +02:00
parent e4e5e735db
commit 4ae46d8876
No known key found for this signature in database
GPG key ID: CBCA68FBAEBF98C9

View file

@ -26,6 +26,8 @@ use Aws\S3\MultipartUploader;
use Aws\S3\S3Client; use Aws\S3\S3Client;
use Psr\Http\Message\StreamInterface; use Psr\Http\Message\StreamInterface;
const S3_UPLOAD_PART_SIZE = 5368709120;
trait S3ObjectTrait { trait S3ObjectTrait {
/** /**
* Returns the connection * Returns the connection
@ -62,17 +64,39 @@ trait S3ObjectTrait {
* @since 7.0.0 * @since 7.0.0
*/ */
function writeObject($urn, $stream) { function writeObject($urn, $stream) {
$stat = fstat($stream);
if ($stat['size'] && $stat['size'] < S3_UPLOAD_PART_SIZE) {
$this->singlePartUpload($urn, $stream);
} else {
$this->multiPartUpload($urn, $stream);
}
}
private function singlePartUpload($urn, $stream) {
$this->getConnection()->putObject([
'Bucket' => $this->bucket,
'Key' => $urn,
'Body' => $stream
]);
}
private function multiPartUpload($urn, $stream) {
$uploader = new MultipartUploader($this->getConnection(), $stream, [ $uploader = new MultipartUploader($this->getConnection(), $stream, [
'bucket' => $this->bucket, 'bucket' => $this->bucket,
'key' => $urn, 'key' => $urn,
]); ]);
$tries = 0; $tries = 0;
do { do {
try { try {
$result = $uploader->upload(); $result = $uploader->upload();
} catch (MultipartUploadException $e) { } catch (MultipartUploadException $e) {
rewind($stream); rewind($stream);
$tries++; $tries++;
if ($tries < 5) { if ($tries < 5) {
$uploader = new MultipartUploader($this->getConnection(), $stream, [ $uploader = new MultipartUploader($this->getConnection(), $stream, [
'state' => $e->getState() 'state' => $e->getState()