AWS: Can't retrieve keys in S3 - php

I need to grab the contents on one of my buckets. I try to do this using the AWS PHP SDK, but nothing is returned. Here's my code:
use Aws\S3\S3Client;
$s3client = S3Client::factory(array('credentials' => array(
'key' => '???????',
'secret' => '???????' ), 'region' => '?????', 'version' => 'latest', ));
try {
$data = $s3client->getIterator('ListObjects', array("Bucket" => "?????"));
print_r($data);
} catch (S3Exception $e) {
echo $e->getMessage() . "\n";
}
Here's the ouput:
Generator Object ( )
The output I get from that code is showing there's nothing wrong. However, there should be some content. The credentials I use are the same ones I use for uploading objects to the bucket, so I don't think those are bad. Am I missing something? How do I retrieve my buckets keys?

you are getting the iterator and not the objects.
To get to the objects you need to use the iterator. Something like:
foreach ($data as $object) {
echo $object['Key'] . "\n";
}

Related

Delete a single file from AWS s3 bucket?

I am trying to delete a single file from AWS s3 bucket and my code is as below:
$removeUploadedDocFromTestingFolder = array(
"removeFromTestFolder" =>
array(
"bucket" => "my-bucket"
),
"AccessKeys" =>
array(
"access_key" => "my access key",
"seceret_key" => "my secret key"
)
);
$_SERVER['HOME'] = DIR_HOME;
ini_set('display_errors',1); error_reporting(E_ALL);
use Aws\S3\S3Client;
use Aws\Common\Credentials\Credentials;
$client = S3Client::factory();
//$objError = new ErrorReporting();
$bucket = $removeUploadedDocFromTestingFolder['removeFromTestFolder']['bucket'];
//testing file setup on local server....
$file1 = "my-file";
//File reference on cloud.....Object URL
$file1_cloud = "https://Object URL/myFile/myFile";
echo "here 0";
$client->deleteObject($bucket, $file1_cloud);
Can anyone tell what I am doing wrong that the code is not deleting the file.
I tried the following code but it didn't work:
try {
//$client->deleteObject($bucket, $file1_cloud);
$result = $client->deleteObject(['Bucket' => 'my-bucket','Key' => 'myFile.png']);
} catch(\Aws\S3\Exception\S3Exception $e) {
echo "error"+$e;
}
Thanks
The object is not being deleted because you have asked S3 to delete a non-existent object, and S3 treats this as a no-op rather than an error. This is because you have indicated a URL rather than the key of the object.
An example of an S3 object key is:
testing/cats/fluffykins.png
Note that it's not a URL and it doesn't begin with forward slash.
An example of the correct way to call the PHP deleteObject function is:
$result = $s3Client->deleteObject([
'Bucket' => $bucket,
'Key' => $key,
]);
Note that it takes an array of parameters, including bucket and key. It assumes PHP 5.4 or later in which you can use the short array syntax, which replaces array() with [].
I've resolved the issue by using the following code:
$client = S3Client::factory(array(
'key' => "mykey",
'secret' => "my secret key"
));
//File reference on cloud.....
$file1_cloud = "TestFile/myFile.png";
$result = $client->deleteObject(array(
'Bucket' => $bucket,
'Key' => 'TestFile/myFile.png'));

How do I fetch a resource from AWS S3 in PHP?

The AWS S3 documentation is quite clear and straightforward:
<?php
// Include the AWS SDK using the Composer autoloader.
require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
$bucket = '*** Your Bucket Name ***';
$keyname = '*** Your Object Key ***';
// Instantiate the client.
$s3 = S3Client::factory();
try {
// Get the object
$result = $s3->getObject(array(
'Bucket' => $bucket,
'Key' => $keyname
));
// Display the object in the browser
header("Content-Type: {$result['ContentType']}");
echo $result['Body'];
} catch (S3Exception $e) {
echo $e->getMessage() . "\n";
}
This strongly suggests that $result['Body'] is the actual content of the file (in my case, a JSON document). However, if I do print_r($result['Body']) I get a Guzzle Object:
Guzzle\Http\EntityBody Object
(
[contentEncoding:protected] =>
[rewindFunction:protected] =>
[stream:protected] => Resource id #9
[size:protected] =>
[cache:protected] => Array
(
[wrapper_type] => PHP
[stream_type] => TEMP
[mode] => w+b
[unread_bytes] => 0
[seekable] => 1
[uri] => php://temp
[is_local] => 1
[is_readable] => 1
[is_writable] => 1
)
[customData:protected] => Array
(
[default] => 1
)
)
How can I retrieve the actual content of the file?
composer.json
{
"require": {
"aws/aws-sdk-php": "2.*",
"guzzle/guzzle": "3.9.3",
}
}
Guzzle EntityBody classes can just be cast to a string to fetch the actual response, so in your case
$response = (string) $result['Body'];
For a bit of clarity — the reason the example works is that when calling echo, the following variable (or statement) is automatically cast to a string. When calling print_r instead, you get a more detailed view of the object, which may or may not include the string you're looking for.

How to get the item count on a DynamoDB table using PHP in real-time?

I have a table in Amazon DynamoDB. I want to get the number of items in the table using PHP script.
Now, I know that there are a lot of questions about this topic, but they are all obsolete, and the solutions use an out-of-date APIs.
I tried running this code, but probably, as I said, it uses an out-of-date API, since it can't find the class:
$dynamodb = new DynamoMetadata();
$scan_response = $dynamodb->scan(array(
'TableName' => 'Table',
'Count' => true
));
echo "Count: ".$scan_response->body->Count."\n";
I also tried with this, but again, it couldn't find the class:
$dynamodb = new AmazonDynamoDB();
I also tried using describeTable, to get ItemCount, but this number isn't updated. It gets updated only every 6 hours.
Is there a way so I could view the number of of items in the table in real-time?
Following solution helped me to get counts using php7 and AWS SDK v3:
<?php
require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\DynamoDb\Exception\DynamoDbException;
$sharedConfig = [
'region' => 'us-east-1',
'version' => 'latest',
'DynamoDb' => [
'region' => 'us-east-1'
]
];
$sdk = new Aws\Sdk($sharedConfig);
$client = $sdk->createDynamoDb();
$tableName = '<TABLE NAME>';
$params = [
'TableName' => $tableName,
'Select' => 'COUNT'
];
try {
$result = $client->scan($params);
$count = current($result)['Count'];
echo $count;
} catch (DynamoDbException $e) {
echo "Unable to query:\n";
echo $e->getMessage() . "\n";
}
So, I found a solution:
$result = $client->describeTable(array(
'TableName' => 'errors'
));
// The result of an operation can be used like an array
echo $result['Table']['ItemCount'] . "\n";
But this is not what I wanted...
I need a solution that will show me the number in real-time and not the number which was true 6 hours ago...

Iterating over the objects in a folder on amazon S3

We have an application where in user can create his own webpages and host them.We are using S3 to store the pages as they are static.Here,as we have a limitation of 100 buckets per user,we decided to go with folders for each user inside a bucket.
Now,if a user wants to host his website on his domain,we ask him for the domain name(when he starts we publish it on our subdomain) and I have to rename the folder.
S3 being a flat file system I know there are actually no folders but just delimeter / separated values so I cannot go into the folder and check how many pages it contains.The API allows it one by one but for that we have to know the object names in the bucket.
I went through the docs and came across iterators,which I have not implemented yet.This uses guzzle of which I have no experience and facing challenges in implementing
Is there any other path I can take or I need to go this way.
You can create an iterator for the contents of a "folder" by doing the following:
$objects = $s3->getIterator('ListObjects', array(
'Bucket' => 'bucket-name',
'Prefix' => 'subfolder-name/',
'Delimiter' => '/',
));
foreach ($objects as $object) {
// Do things with each object
}
If you just need a count, you could this:
echo iterator_count($s3->getIterator('ListObjects', array(
'Bucket' => 'bucket-name',
'Prefix' => 'subfolder-name/',
'Delimiter' => '/',
)));
Bit of a learning curve with s3, eh? I spent about 2 hours and ended up with this codeigniter solution. I wrote a controller to loop over my known sub-folders.
function s3GetObjects($bucket) {
$CI =& get_instance();
$CI->load->library('aws_s3');
$prefix = $bucket.'/';
$objects = $CI->aws_s3->getIterator('ListObjects', array(
'Bucket' => $CI->config->item('s3_bucket'),
'Prefix' => $prefix,
'Delimiter' => '/',
));
foreach ($objects as $object) {
if ($object['Key'] == $prefix) continue;
echo $object['Key'].PHP_EOL;
if (!file_exists(FCPATH.$object['Key'])) {
try {
$r = $CI->aws_s3->getObject(array(
'Bucket' => $CI->config->item('s3_bucket'),
'Key' => $object['Key'],
'SaveAs' => FCPATH.$object['Key']
));
} catch (Exception $e) {
echo $e->getMessage().PHP_EOL;
//return FALSE;
}
echo PHP_EOL;
} else {
echo ' -- file exists'.PHP_EOL;
}
}
return TRUE;
}

How to retrieve contents of Amazon S3 bucket in JSON using the AWS PHP sdk?

I want to get the list of all the filenames that are inside my Amazon S3 bucket from my PHP server. Is that possible with the Amazon PHP sdk? If not, is there another way? Thanks!
Using the official AWS SDK for PHP v2, you can do something like the following:
<?php
require 'vendor/autoload.php';
use Aws\Common\Aws;
// Instantiate an S3 client
$s3 = Aws::factory('/path/to/config.php')->get('s3');
$objects = $s3->getIterator('ListObjects', array(
'Bucket' => $bucket
));
foreach ($objects as $object) {
echo $bucket['Name'] . '/' . $object['Key'] . PHP_EOL;
}
It worked fine for me except that MaxKeys was ignored.
Is there a difference between using the above method and the one below. Although I couldn't get to array elements within $result.
use Aws\Common\Aws;
use Aws\S3\S3Client;
// Prepare S3 Client to speak with AWS
$s3client;
$s3client = S3Client::factory(array(
'key' => '',
'secret' => '',
'region' => Region::US_EAST_1
));
// List objects from a bucket
$result = $s3client->listObjects(array(
'Bucket' => $aws_bucket,
'MaxKeys' => 25,
'Marker' => 'docs/'
));
$objects = $result->getIterator();
foreach ($objects as $wo)
{
// Prints junk
echo $wo['Key'] . ' - ' . $wo['Size'] . PHP_EOL;
}

Categories