I installed the SDK via composer and have my creds in .aws/credentials. When running 'php PutObject.php' I get no output nor error/exception but the objects are not uploaded either. I'm running Ubuntu 16.04. What's odd is I have the exact same config installed in my Mac and I can put objects.
<?php
require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\Exception\AwsException;
// Create an S3 client
$client = new Aws\S3\S3Client([
'region' => 'us-east-1',
'version' => 'latest',
'debug' => true
]);
// Where the files will be source from
$source = 'TestUnit';
// Where the files will be transferred to
$dest = 's3://mybucket/TestUnit';
// Create a transfer object.
$manager = new \Aws\S3\Transfer($client, $source, $dest);
// Perform the transfer synchronously.
$manager->transfer();
Related
I am trying to upload an image to my S3 Bucket via AWS PHP SDK. For my EC2 instance, I have attached a role that allows PutObject and GetObject for my S3 Bucket. Therefore, I do not need to attach credentials when creating the S3Client, supposedly. I'm still learning.
Here is my PHP script:
<?php
require './aws/aws-autoloader.php';
use Aws\S3\S3Client;
use Aws\Exception\AwsException;
use Aws\Credentials\Credentials;
$s3 = new Aws\S3\S3Client([
'version' => 'latest',
'region' => 'us-east-1'
]);
$filename = $_FILES['file']['name'];
try {
$result = $s3->putObject([
'Bucket' => 'bucket name',
'Key' => 'testimage1',
'Body' => $filename
]);
echo 'DONE';
} catch (Exception $e) {
echo $e;
}
I keep getting a 500 internal error in this form. Apparently the error occurs at S3Client creation, I'm not sure why.
Further probing confirms, that when I try to instantiate the S3Client class, the 500 internal error is thrown. I am able to access the S3 bucket from the CLI but unable to do so using the AWS PHP SDK.
Here is the image of the error:
Can anyone advice in this? Thank you for reading.
According to the docs, the use of the S3Client Class is like this: https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_configuration.html
use Aws\S3\S3Client;
$options = [
'region' => 'us-east-1',
'version' => '2006-03-01',
'signature_version' => 'v4'
];
$s3Client = new S3Client($options);
It also says, "We do not recommend Using latest in a production application because pulling in a new minor version of the SDK that includes an API update could break your production application."
I solved it but I used composer. So here's a step by step guide.
This is under the assumption that you have created an IAM role with full S3 access or at least with the PutObject policy attached, and the IAM role is attached to the EC2 instance. Also that your EC2 instance has installed a web server.
In the EC2 instance /var/www/html (folder where your web server hosts your webpage), install composer as guided through here in your project folder. I chose to install composer globally for convenience sake.
Install AWS PHP SDK as seen in this guide.
If you an encounter an error asking for simplexml extension, just yum install php-xml and rerun the installation of AWS PHP SDK.
Now that you have the SDK installed, you can now upload the file you want to the S3 bucket.
Here is my code for uploading, my vendor folder is root level with the php file used for uploading files to the S3 bucket.
require 'vendor/autoload.php';
use Aws\S3\S3Client;
try{
$sharedConfig = [
'region' => 'us-east-1',
'version' => 'latest'
];
$sdk = new Aws\Sdk($sharedConfig); /*Instantiate SDK class with configs for API use*/
$s3Client = $sdk->createS3(); /*creates the S3 Client for API use*/
$file_name = $_FILES['file']['name']; /*file name e.g. name.jpg */
$file_tmp_name = $_FILES['file']['tmp_name']; /*!!! IMPORTANT - this is what you need to supply the SourceFile to properly upload the file*/
$file_type = $_FILES['file']['type']; /*file type*/
/*print_r($_FILES['file']);*/
$result = $s3Client->putObject([
'Bucket' => 'bucket-name',
'Key' => 'testimage.jpg',
'SourceFile' => $file_tmp_name,
'ContentType'=>$file_type,
'ContentDisposition'=>'attachment'
]);
echo "FILE SENT";
}catch(Exception $e){
echo $e;
}
I am working on a project where users can upload objects in their own directory in server and this is my approach that works on localhost to create user directories:
$s3Client = new Aws\S3\S3Client([
'version' => 'latest',
'region' => 'ca-central-1'
]);
$s3Client->registerStreamWrapper();
$s3Dir = 's3://' . $bucketName . '/' . $folderName;
file_put_contents($s3Dir, '');
Since the code is working on local, I think it's not the problem of s3 permissions or bucket policies. My guess is php setting on AWS server is causing the issue.
Any help would be great!
I am trying to check bucket existence on Amazon S3 using below code:
$credentials = new Aws\Common\Credentials\Credentials($creds['access_key_id'], $creds['secret_access_key']);
$client = Aws\S3\S3Client::factory(array( 'credentials' => $credentials ) );
if( ! $client->doesBucketExist($creds['bucket']) ) {
throw new Exception("Bucket (" . $creds['bucket'] . ") does not exist.");
}
It is working on localhost (wamp) but when I tried this on server it is not working. I am getting following error:
Missing required client configuration options: region: (string) A "region" configuration value is required for the "s3" service (e.g., "us-west-2"). A list of available public regions and endpoints can be found at http://docs.aws.amazon.com/general/latest/gr/rande.html. version: (string) A "version" configuration value is required. Specifying a version constraint ensures that your code will not be affected by a breaking change made to the service. For example, when using Amazon S3, you can lock your API version to "2006-03-01". Your build of the SDK has the following version(s) of "s3": * "2006-03-01" You may provide "latest" to the "version" configuration value to utilize the most recent available API version that your client's API provider can find. Note: Using 'latest' in a production application is not recommended. A list of available API versions can be found on each client's API documentation page: http://docs.aws.amazon.com/aws-sdk-php/v3/api/index.html. If you are unable to load a specific API version, then you may need to update your copy of the SDK.
I don't know why it is not working on server but same code is working on localhost.
I had the same problem and I needed to clear my config cache to fix it.
$ artisan config:clear
Set region explicitly when creating s3 client instead of relying on defaults.
use Aws\Credentials\Credentials;
use Aws\S3\S3Client;
$result = $stsClient->getSessionToken();
$credentials = new Credentials(
$result['Credentials']['AccessKeyId'],
$result['Credentials']['SecretAccessKey'],
$result['Credentials']['SessionToken']
);
$s3Client = new S3Client([
'version' => '2006-03-01',
'region' => 'us-west-2',
'credentials' => $credentials
]);
Check .env file variables are matching with filesystems.php
's3' => [
'driver' => 's3',
'key' => env('S3_KEY'),
'secret' => env('S3_SECRET'),
'region' => env('S3_REGION'),
'bucket' => env('S3_BUCKET'),
],
1) Ensure you have S3_KEY, S3_SECRET, S3_REGION, S3_BUCKET etc configured in your .env file
2) Your environment file might have changed after the autoload/caches were generated. Run:
php artisan config:cache
I have been working on a project where we will have a website process on EC2 server. In my project, I was able to write a php code that allow users to upload file from web server to AWS S3 budget. However, in order to access that file from EC2, i think we need to transfer file from S3 budget to EC2 instance. I saw that there is a unix command line way to do that, but the project i'm working on is based on PHP/HTML/JSON. I wonder is there a way or tutorial to access or transfer file from S3 to EC2 instance using PHP?
Thanks
You do not need to transfer the file from S3 to EC2, in fact, this would be a waste of resources. Instead, you can generate a signed url pointing to the file directly on S3 (even if the bucket/file is private). This link can also be set to expire after a certain period. Here's an example using the AWS PHP SDK:
<?php
require 'aws-autoloader.php'; //SDK autoloader file
//*** Initiate an S3 client
$client = S3Client::factory(array(
'key' => "public_key",
'secret' => "private_key",
'region' => "your s3 region"
));
$url = $client->getObjectUrl("bucket_name", "full_object_name", "+1 minutes"[optional]);
echo $url; //Print the direct S3 link to the file, which will expire in 1 minute.
//This will work even if the bucket is private.
[update]
If you still need to download the file from S3 to EC2, do this:
<?php
require 'aws-autoloader.php'; //SDK autoloader file
//*** Initiate an S3 client
$client = S3Client::factory(array(
'key' => "public_key",
'secret' => "private_key",
'region' => "your s3 region"
));
$result = $client->getObject(array(
'Bucket' => "bucket_name",
'Key' => "name of file",
'SaveAs' => "ec2 path to save the file at"
));
echo $result; //make sure download worked.
Further reading:
http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.S3.S3Client.html
I'm trying to use Amazon S3 to store my images.
What I've done so far is installing the AWS SDK via PEAR (link: http://docs.aws.amazon.com/aws-sdk-php-2/guide/latest/installation.html)
Then I've gone to the second step, creating a client:
<?php
// Include the SDK using the Composer autoloader
require 'vendor/autoload.php';
use Aws\S3\S3Client;
// Instantiate the S3 client with your AWS credentials and desired AWS region
$client = S3Client::factory(array(
'key' => 'your-aws-access-key-id',
'secret' => 'your-aws-secret-access-key',
));
My keys are set correctly ... .
Then I want to test all this by creating a bucket like this:
$bucket = 'my-bucket';
try{
$result = $client->createBucket(array(
'Bucket' => $bucket
));
// Wait until the bucket is created
$client->waitUntil('BucketExists', array('Bucket' => $bucket));
}
catch(Exception $e){
var_dump($e->getMessage());
}
But I always get this error:
The requested bucket name is not available. The bucket namespace is shared by all users of the system. Please select a different name and try again.
I think my SDK isn't properly installed ... Can anyone help me with this? I'm using vagrant so I installed the AWS SDK in my root folder with this command:
sudo pear -D auto_discover=1 install pear.amazonwebservices.com/sdk
I got the message that is was installed ok.
I've also did a var_dump of my $client. The link to my response: http://pastebin.com/KqkEiKGs
Don't now if you're something with it ... (My keys are hidden)
Your error message says it - you are trying to use the name that is not unique for your bucket, you should use some more unique names. And, yes, bucket names must be unique across all of Amazon S3 (not only your account).
You can read more about it here.