I have an amazon s3 bucket that has 20+ records in it. How to get all file names with pagination support using PHP codeigniter.
Thanks in advance!
First you need to get all object by listObjects
$result = $s3->listObjects([
'Bucket' => 'your-bucket-name'
]);
it will return array of objects and with links( if your bucket is public than you can open those link else you need to use signed url or cloudfront )
And i would simply suggest you to use dataTable (it has pagination, Search ) and your record is not like 30-40k so it will work fine
As you have asked you can bucket object list by passing key and secret in constructor, i am using aws phpsdk v3
s3 = new Aws\S3\S3Client([
'version' => 'latest',
'region' => 'us-west-2',
'credentials.ini' => [
'key' => $credentials['key'],
'secret' => $credentials['secret'],
],
]);
Now just
$result = $s3->listObjects([
'Bucket' => 'your-bucket-name'
]);
That's it you got array of all object in your bucket
Related
I am sending file to AWS s3 using PHP SDK. I installed SDK using
composer require aws/aws-sdk-php
I am using following code
require_once('vendor/autoload.php');
$s3 = new Aws\S3\S3Client([
'region' => AWS_REGION,
'version' => 'latest',
'credentials' => [
'key' => AWS_ACCESS_KEY_ID,
'secret' => AWS_SECRET_ACCESS_KEY,
]
]);
$result = $s3->putObject([
'Bucket' => AWS_BUCKET,
'Key' => $filename,
'SourceFile' => $fileFullPath
]);
Following response, I am getting
I am trying to get status code from this response and tried different ways, but I could not get status code.
You are returned an object with a private array called "data" but you are also able to just call the data by attribute. So using $result['#metadata']['statusCode'] works just fine.
$result['#metadata']['statusCode'] == 200
According to your example.
Worked fine for me
$result['#metadata']['statusCode'] == 200
We're using the latest PHP AWS S3 SDK.
We've been given a ARN Bucket name
eg:
arn:aws:s3:::project-s3-eu-west-2-d-app-uploads
Using the PHP Code, we're trying:
<?php
use Aws\S3\S3Client;
use Aws\Exception\AwsException;
$s3 = new S3Client([
'version' => 'latest',
'region' => 'eu-west-2',
'use_arn_region' => TRUE, // or FALSE
]);
$s3->putObject([
'Bucket' => 'arn:aws:s3:::project-s3-eu-west-2-d-app-uploads',
// 'Bucket' => 'project-s3-eu-west-2-d-app-uploads.s3.eu-west-2.amazonaws.com', // this did not work
'Key' => 'random-unique-name.ext',
'Body' => fopen('/local/file/path/name.ext', 'r'),
'ACL' => ACL_PUBLIC_READ,
]);
?>
But - no matter what we try, we get the following error:
Bucket parameter parsed as ARN and failed with: Provided ARN was not a valid S3 access point ARN or S3 Outposts access point ARN.
Have you used an ARN for auth, not a Key/Secret?
Thanks
The question it self explanatory, when trying to create a presigned url I get the following error:
Error retrieving credentials from the instance profile metadata server. (cURL error 28: Connection timed out after 1001 milliseconds (see http://curl.haxx.se/libcurl/c/libcurl-errors.html))
I have used the code from here exactly https://docs.aws.amazon.com/aws-sdk-php/v3/guide/service/s3-presigned-url.html
My code is below:
$s3Client = new S3Client([
'region' => 'eu-west-1',
'version' => '2006-03-01',
]);
$cmd = $s3Client->getCommand('GetObject', [
'Bucket' => 'my-bucket-name',
'Key' => 'AKIAJNCZ5***********'
]);
$request = $s3Client->createPresignedRequest($cmd, '+20 minutes');
// Get the actual presigned-url
$presignedUrl = (string) $request->getUri();
print_r($presignedUrl);
Any reason why this is happening?
EDIT::
Ok so this fixed my problem, but it wasnt not actually even in the docs:
$s3Client = new S3Client([
'region' => 'eu-west-1',
'version' => '2006-03-01',
'credentials' => ['key' => 'AKIAJNCZ5MY*******8','secret'=>'NgeFc+2/Q2cUAmL/+lP2gp***********8']
]);
Adding the credentials assoc array :)
However I am now unsure how to use this presigned url to download one of my files aha, so if anyone knows and doesnt mind putting me in the right direction :)
'Key' in the getCommand array is the name/path to the file you want to generate a pre-signed URL for, not your AWS key :)
$cmd = $s3Client->getCommand('GetObject', [
'Bucket' => 'my-bucket-name',
'Key' => 'path/to/file.txt', // or just file.txt if it's in the root of the bucket
]);
I am trying to get pre-signed url of all object in bucket. I am using amazon php sdk version 3.
What I have tried is
$client = new Aws\S3\S3Client([
'version' => 'latest',
'region' => 'us-west-2',
'credentials.ini' => [
'key' => $credentials['key'],
'secret' => $credentials['secret'],
],
]);
$client->listObjects(['Bucket' => $bucketName]);
Above get me all object in arrayAccess but It have object url like
https://s3-us-west-2.amazonaws.com/some-demo/one2.txt
and I don't want that everyone have access to one2.txt so I have created a preassigned url by
$cmd = $client->getCommand('GetObject', [
'Bucket' => $bucket,
'Key' => $key
]);
$request = $client->createPresignedRequest($cmd, '+20 minutes');
$presignedUrl = (string) $request->getUri();
echo $presignedUrl;
Now I am getting url with token
https://s3-us-west-2.amazonaws.com/some-demo/one2.txt?X-Amz-Content-Sha256=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAJUZQHGPBTNOLEUXQ%2F20150828%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20150828T090256Z&X-Amz-SignedHeaders=Host&X-Amz-Expires=1200&X-Amz-Signature=77e52cf99c0f438d48851193dbaba0fsdfe1b4d8e604d6sdf11a22b3be45e410168ab81
which Is exactly what I want but Now my question is
How to get preassigned url all items in bucket rather than making for all item one by one ?
I think there is one way to get preassigned url of all items by creating an array of multiple getCommands, getCommand can handle multiple commands and then you can use toArray() function of Aws\CommandInterface to convert it into an array. The createPresignedRequest() function does not support for multiple requests either you have to called it repetitive or need to use an getObject()
The AWS PHP SDK documentation for deleteMatchingObjects states that it returns an integer: "Returns the number of deleted keys"
http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.S3.S3Client.html#_deleteMatchingObjects
My media is deleted successfully but I get an empty value back where I'm supposed to get a count of deleted objects. Am I doing something wrong?
This is in the beforeDelete method of a CakePHP 2.x Model Behavior.
$s3 = new Aws\Sdk(array('version' => 'latest', 'region' => 'us-west-1', 'credentials' => array('key' => $this->s3Key, 'secret' => $this->s3Secret)));
$client = $s3->createS3();
$delete = $client->deleteMatchingObjects($this->s3ReadBucket, $this->fileGroupPrefix($model->name, $model->id));
$this->__logger($delete);