'Basic AWS S3 PHP setup
I've been trying for a while to setup just a basic PHP implementation of an upload form to upload to Amazon's S3 service, but I can't get anything to work.
Reading through their docs, their examples all seem different. What is the correct way to provide credentials and upload a file?
On their github repo, it says:
// Require the Composer autoloader.
require 'vendor/autoload.php';
use Aws\S3\S3Client;
// Instantiate an Amazon S3 client.
$s3 = new S3Client([
'version' => 'latest',
'region' => 'us-west-2'
]);
try {
$s3->putObject([
'Bucket' => 'my-bucket',
'Key' => 'my-object',
'Body' => fopen('/path/to/file', 'r'),
'ACL' => 'public-read',
]);
} catch (Aws\Exception\S3Exception $e) {
echo "There was an error uploading the file.\n";
}
On http://docs.aws.amazon.com/aws-sdk-php/v2/guide/service-s3.html they say:
use Aws\S3\S3Client;
$client = S3Client::factory(array(
'profile' => '<profile in your aws credentials file>'
));
// Upload an object by streaming the contents of a file
// $pathToFile should be absolute path to a file on disk
$result = $client->putObject(array(
'Bucket' => $bucket,
'Key' => 'data_from_file.txt',
'SourceFile' => $pathToFile,
'Metadata' => array(
'Foo' => 'abc',
'Baz' => '123'
)
));
// We can poll the object until it is accessible
$client->waitUntil('ObjectExists', array(
'Bucket' => $this->bucket,
'Key' => 'data_from_file.txt'
));
Can anyone who has been able to do this recently shed some light on the setup here?
Solution 1:[1]
I ended up getting it working by just using the embeded credentials
<?php
date_default_timezone_set("America/Denver");
require "./aws/aws-autoloader.php";
use Aws\S3\S3Client;
$s3Client = S3Client::factory(array(
'credentials' => array(
'key' => 'key',
'secret' => 'secret',
),
"region" => "us-east-1",
"version" => "latest"
));
?>
Solution 2:[2]
Install Composer Once PHP CLI is installed, download the Composer installer script with:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
Compile Composer with PHP Version
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
check composer
composer
Install/download aws sdk library
composer require aws/aws-sdk-php
php -d memory_limit=-1 composer.phar require aws/aws-sdk-php
Sample Code Download File
<?php
require 'vendor/autoload.php';
use Aws\S3\S3Client;
$bucket ="sentimentanalysis2021";
$key ="KEY";
$secret ="SecretKEY";
$region ="ap-south-1";
$location = "/var/www/html/openpbx/record/temp/";
// Establish connection with DreamObjects with an S3 client.
$client = new Aws\S3\S3Client([
'version' => '2006-03-01',
'region' => $region,
'endpoint' => "https://s3.$region.amazonaws.com/",
'credentials' => [
'key' => $key,
'secret' => $secret,
]
]);
//var_dump($client);
//Download File
$file_s3 = null;
$objects = $client->getPaginator('ListObjects', ['Bucket' => $bucket]);
foreach ($objects as $listResponse) {
$items = $listResponse->search("Contents[?ends_with(Key,'wav')]");
foreach($items as $item) {
// download($client,$item['Key'],$location);
$client->getObject(array(
'Bucket' => $bucket,
'Key' => $item['Key'],
'SaveAs' => $location.$item['Key']
));
$file_s3 =$location.$item['Key'];
}
}
//Read Files
$objects = $client->listObjectsV2([
'Bucket' => $bucket,
]);
foreach ($objects['Contents'] as $object){
echo "{$object['Key']}\t{$object['LastModified']}\n";
}
function download($client,$file,$location){
$client->getObject(array(
'Bucket' => $bucket,
'Key' => $file,
'SaveAs' => $location.$file
));
}
// Create Folder
/* abc is the folder name */
$client->putObject(array(
'Bucket' => $bucket,
'Key' => "abc/",
'Body' => "",
'ACL' => 'public-read'
));
$file_Path = '/amol/20220516141901_919033_9999.wav';
$key = basename($file_Path);
try{
$result = $client->putObject([
'Bucket' => $bucket,
'Key' => $key,
'SourceFile' => $file_Path,
'ACL' => 'private',
]);
} catch (S3Exception $e) {
echo $e->getMessage() . "\n";
}
?>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | brycejl |
| Solution 2 |
