I have been able to upload files to my bucket using the below code from the AWS PHP SDK. The problem is when I try to upload files that are larger than 15 mb, the script throws an error. Otherwise it works as expected. Any Ideas what I'm doing wrong? Thanks in advance.
require $dir . 'aws/aws-autoloader.php';
$s3Client = new Aws\S3\S3Client(array(
'version' => 'latest',
'region' => 'us-west-2',
'credentials' => array(
'key' => 'KEY',
'secret' => 'SECRET',
)
));
$result = $s3Client->putObject(array(
'Bucket' => 'eot-resources',
'Key' => $org_id."_".$_FILES["fileToUpload"]["name"],
'SourceFile' => $_FILES["fileToUpload"]["tmp_name"],
'Body' => new GuzzleHttp\Psr7\Stream(fopen($_FILES["fileToUpload"]["tmp_name"], 'r')),
'ACL' => 'public-read',
'StorageClass' => 'REDUCED_REDUNDANCY',
'Metadata' => array(
'Foo' => 'abc',
'Baz' => '123'
)
));
echo "URL: ".$result['ObjectURL'] . "<br>";
I get the following error when trying to upload a large file bigger than 15mb. Other than that it works.
Warning: fopen(): Filename cannot be empty in /Users/xxx/Code/xxx/wp-content/plugins/xxx/parts/part-upload_file.php on line 37.
Line 37 reads..
'Body' => new GuzzleHttp\Psr7\Stream(fopen($_FILES["fileToUpload"]["tmp_name"], 'r')),
Any help/advice/tips would be appreciated.
I suspect your server has a limit on the size of file that can be uploaded. Check using phpinfo() and look for post_max_size and upload_max_filesize. Assuming that's the problem then you can increase the limit.
Related
What could be the possible solution for this? I'm trying to upload an Object (Image file) to my Linode Object Storage, I have already set up the configuration and credentials but I'm still having trouble doing it. Can someone help me?
Code:
$config = [
'version' => 'latest',
'region' => config('filesystems.disks.s3.region'),
'scheme' => 'http',
'endpoint' => config('filesystems.disks.s3.endpoint'),
'credentials' => [
'key' => config('filesystems.disks.s3.key'),
'secret' => config('filesystems.disks.s3.secret'),
],
'debug' => true
];
$client = new S3Client($config);
return $client->putObject([
'Bucket' => config('filesystems.disks.s3.bucket'),
'Key' => $filename,
'SourceFile' => $file
]);
Here's the exception error:
check me please
Hoping to have some feedback here, thank you!
My error message:
Uncaught Aws\S3\Exception\InvalidRequestException:
AWS Error Code: InvalidRequest,
Status Code: 400, AWS Request ID: xxx,
AWS Error Type: client,
AWS Error Message: The authorization mechanism you have provided is not supported.
Please use AWS4-HMAC-SHA256., User-Agent: aws-sdk-php2/2.7.27 Guzzle/3.9.3 curl/7.47.0 PHP/7.0.15-0ubuntu0.16.04.4
My Code:
<?php
use Aws\S3\S3Client;
require 'vendor/autoload.php';
$config = array(
'key' => 'xxx',
'secret' => 'xxx',
'bucket' => 'myBucket'
);
$filepath = '/var/www/html/aws3/test.txt';
//s3
// Instantiate the client.
$s3 = S3Client::factory();
// Upload a file.
$result = $s3->putObject(array(
'Bucket' => $config['bucket'],
'Key' => $config['key'],
'SourceFile' => $filepath,
'Endpoint' => 's3-eu-central-1.amazonaws.com',
'Signature'=> 'v4',
'Region' => 'eu-central-1',
//'ContentType' => 'text/plain',
//'ACL' => 'public-read',
//'StorageClass' => 'REDUCED_REDUNDANCY',
//'Metadata' => array(
// 'param1' => 'value 1',
// 'param2' => 'value 2'
)
);
echo $result['ObjectURL'];
Cant figure out why I cant get through. I have the right parameters in my call. My code is mostly copied from their aws-sdk-php example page. So it shouldnt be to much fault there.
I am using aws cli and have set upp my configure and credentials file under ~/.aws/
EDIT:
Got it to work! This is my code now:
<?php
// Get dependencies
use Aws\S3\S3Client;
require 'vendor/autoload.php';
// File to upload
$filepath = '/var/www/html/aws3/test.txt';
// instantiate the Client
$s3Client = S3Client::factory(array(
'credentials' => array(
'key' => 'AKIAJVKBYTTADILGRTVQ',
'secret' => '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'
),
'region' => 'eu-central-1',
'version' => 'latest',
'signature_version' => 'v4'
));
// Upload a file.
$result = $s3Client->putObject(array(
'Bucket' => "myBucket",//some filebucket name
'Key' => "some_file_name.txt",//name of the object with which it is created on s3
'SourceFile' => $filepath,
)
);
// Echo results
if ($result){
echo $result['ObjectURL'];
} else{
echo "fail";
}
Try this, it will surely work, the problem in your code is you haven't supplied credentials to factory function.
<?php
ini_set('display_errors', 1);
use Aws\S3\S3Client;
require 'vendor/autoload.php';
//here we are creating client object
$s3Client = S3Client::factory(array(
'credentials' => array(
'key' => 'YOUR_AWS_ACCESS_KEY_ID',
'secret' => 'YOUR_AWS_SECRET_ACCESS_KEY',
)
));
// Upload a file.
$filepath = '/var/www/html/aws3/test.txt';
$result = $s3Client->putObject(array(
'Bucket' => "myBucket",//some filebucket name
'Key' => "some_file_name.txt",//name of the object with which it is created on s3
'SourceFile' => $filepath,
'Endpoint' => 's3-eu-central-1.amazonaws.com',
'Signature' => 'v4',
'Region' => 'eu-central-1',
)
);
echo $result['ObjectURL'];
I am trying to create a php script which will be able to upload a text file to my ASW S3 bucket.
I have tried the method which is there on AWS site but sadly that ain't proper, I mean it's not end to end.
I have installed the AWS PHP SDK on my instance.
Then I did what's written in the sample code i.e.
<?php
use Aws\S3\S3Client;
$bucket = 'cst';
$keyname = 'sampleUpload';
// $filepath should be absolute path to a file on disk
$filepath = '/var/www/html/po/si/mag/sahara.txt';
// Instantiate the client.
$s3 = S3Client::factory();
// Upload a file.
$result = $s3->putObject(array(
'Bucket' => $bucket,
'Key' => $keyname,
'SourceFile' => $filepath,
'ContentType' => 'text/plain',
'ACL' => 'public-read',
'StorageClass' => 'REDUCED_REDUNDANCY',
'Metadata' => array(
'param1' => 'value 1',
'param2' => 'value 2'
)
));
echo $result['ObjectURL'];
?>
Obviously, I haven't added the aws key nor the aws secret key so it won't work. But then nothing is specified in the tutorial either. So am kinda lost.
Secondly, I tried using this code :
It's also not working.
Thirdly, I tried this article.
It's working when am using it with html but I am not really able to create a php only script where I can just specify the file location, and the file get's uploaded to the server.
Any help is highly appreciated. I searched a lot, but couldn't find anything useful.
Just a guess, but did you add your credentials inside your HTML code using hidden inputs? Cause I just had a very quick look at this page: https://aws.amazon.com/articles/1434/ and it seems like you can set your credentials using HTML. And my guess is the class will automatically take care of that.
If my guess is right, you do need to add the credentials to your instance:
// Instantiate the client.
$s3 = S3Client::factory();
like
// Instantiate the client.
$s3 = S3Client::factory(array(
'version' => 'latest',
'region' => 'us-west-2', //add correct region
'credentials' => array(
'key' => <YOUR_AWS_KEY>,
'secret' => <YOUR_AWS_SECRET>
)
));
It probably depends on the version of the sdk you're using, whether you need above mentioned code or this one (notice the missing credentials array):
// Instantiate the client.
$s3 = S3Client::factory(array(
'version' => 'latest',
'region' => 'us-west-2', //add correct region
'key' => <YOUR_AWS_KEY>,
'secret' => <YOUR_AWS_SECRET>
));
EDIT:
Just to show what exactly worked in my case, this is my complete code. The path I executed:
http://myurl.com/index.php?path=./test.txt
code:
require __DIR__ . '/vendor/autoload.php';
use Aws\S3\S3Client;
$bucket = 'sdl-images';
$keyname = '*** Your Object Key ***';
// $filepath should be absolute path to a file on disk
$filepath = $_GET['path'];
// Instantiate the client.
$s3 = S3Client::factory(array(
'version' => 'latest',
'region' => <YOUR_REGION E.G. eu-west-1>,
'credentials' => array(
'key' => <YOUR_AWS_KEY>,
'secret' => <YOUR_AWS_SECRET>
)
));
// Upload a file.
$result = $s3->putObject(array(
'Bucket' => $bucket,
'Key' => $keyname,
'SourceFile' => $filepath,
'ContentType' => 'text/plain',
'ACL' => 'public-read',
'StorageClass' => 'REDUCED_REDUNDANCY'
));
echo $result['ObjectURL'];
I'm learning how to use Amazon for storing images and have gotten to the point I can upload a file with PHP. The problem is when I upload an image it shows as a blank image like it didn't upload. Here's what I've got:
$result = $s3->putObject(array(
'Bucket' => $bucket,
'Key' => $keyname,
'Body' => $filepath,
'ContentType' => 'image/jpeg',
'ACL' => 'public-read',
'StorageClass' => 'STANDARD',
'Metadata' => array(
'param1' => 'value 1',
'param2' => 'value 2'
)
));
Here's the image it uploaded (which doesn't help much): https://motorcyclealbum.s3.amazonaws.com/test.jpg
Solved
So, 'Body' needed to be 'SourceFile' if uploading an image. I'm a dork.
So, 'Body' needed to be 'SourceFile' if uploading an image. I'm a dork.
I need to upload my files inside specific directories that I created on my amazon s3 storage. I always uploaded the files on the "absolute path" of my bucket doing something like so:
$s3->putObject(array(
'Bucket' => $bucket,
'ContentType' => $mime,
'Key' => $localImage,
'ACL' => 'public-read',
'SourceFile' => $localImage,
'CacheControl' => 'max-age=172800',
"Expires" => gmdate("D, d M Y H:i:s T", strtotime("+5 years")),
'Metadata' => array(
'profile' => $localImage,
),
));
How can I define where this file should be uploaded on a given directory?
You must include that information in the "Key" parameter. S3 isn't actually a filesystem, it's more like a big (hash table) associative array. The "Bucket" is the name of the hash table, and the "Key" is the key (e.g., $bucket[$key] = $content). So all path/directory information must be a part of the "Key".
$localImage = '/Users/jim/Photos/summer-vacation/DP00342654.jpg';
$s3->putObject(array(
'Bucket' => 'my-uniquely-named-bucket',
'SourceFile' => $localImage,
'Key' => 'photos/summer/' . basename($localImage)
));
thank you Jeremy Lindblom, this is my python example that worked for me.
import boto3
s3 = boto3.resource('s3')
data = open('/home/briansanchez/www/red-hat.jpg', 'rb')
s3.Bucket('briansanchez').put_object(Key='www/red-hat.jpg', Body=data)
Updated code according to the latest SDK of AWS:-
$result = $s3->putObject(array(
'Bucket' => 'bucket name of S3',
'Key' => 'pawan-trying',
'SourceFile' => 'local image path or document root image path ',
'ContentType' => 'image',
'ACL' => 'public-read',
'StorageClass' => 'REDUCED_REDUNDANCY',
'Metadata' => array(
'param1' => 'value 1',
'param2' => 'value 2'
)
));