I'm having problems when I want to upload an image to my amazon s3 bucket.
I'm trying to upload a jpg image with the size of 238 KB. I've put a try/catch in my code to check what the error was. I always get this error:
Your proposed upload is smaller than the minimum allowed size
I've also tried this with images from 1MB and 2MB, same error ... .
Here's my code:
<?php
// Include the SDK using the Composer autoloader
require 'AWSSDKforPHP/aws.phar';
use Aws\S3\S3Client;
use Aws\Common\Enum\Size;
$bucket = 'mybucketname';
$keyname = 'images';
$filename = 'thelinktomyimage';
// Instantiate the S3 client with your AWS credentials and desired AWS region
$client = S3Client::factory(array(
'key' => 'key',
'secret' => 'secretkey',
));
// Create a new multipart upload and get the upload ID.
$response = $client->createMultipartUpload(array(
'Bucket' => $bucket,
'Key' => $keyname
));
$uploadId = $response['UploadId'];
// 3. Upload the file in parts.
$file = fopen($filename, 'r');
$parts = array();
$partNumber = 1;
while (!feof($file)) {
$result = $client->uploadPart(array(
'Bucket' => $bucket,
'Key' => $keyname,
'UploadId' => $uploadId,
'PartNumber' => $partNumber,
'Body' => fread($file, 5 * 1024 * 1024),
));
$parts[] = array(
'PartNumber' => $partNumber++,
'ETag' => $result['ETag'],
);
}
// Complete multipart upload.
try{
$result = $client->completeMultipartUpload(array(
'Bucket' => $bucket,
'Key' => $keyname,
'UploadId' => $uploadId,
'Parts' => $parts,
));
$url = $result['Location'];
fclose($file);
}
catch(Exception $e){
var_dump($e->getMessage());
}
(I've changed the bucket, keys and image link.)
Has anyone had this before? Searching on the internet didn't help me much.
Also searching to change the minimum upload size didn't provide much help.
UPDATE:
When I tried this with a local image (changed the filename), it worked! How can I make this work with an image that's online? Now I save it in my temp file and then upload it from there. But isn't there a way to store it directly without saving it locally?