I'm trying to use Filesystem, the implementation of league/flysystem-aws-s3-v3 with Laravel 5.1. but I can get the files from S3.
So, I coded this:
$s3 = Storage::disk('s3');
$imageName = Employee::filePath() . $employee->id . '.pdf';
if (Storage::disk('s3')->exists($imageName)) {
return response()->download(Storage::disk('s3')->get($imageName));
}
Then I get the follow error:
CredentialsException in InstanceProfileProvider.php line 79:
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))
The credential are set on config/filesystems.php, I know are good because I can put files there.
Do you have any idea?
Thanks in advance.
The problem was with my configuration, I was trying to setup with env, however for some reason doesn't load at moment of configuration, so I decide put directly on the configuration file filesystems.php
's3' => [
'driver' => 's3',
'key' => 'XXXXXXXXXX',
'secret' => 'XXXXXXXXXX',
'region' => env('S3_REGION'),
'bucket' => env('S3_BUCKET'),
],
Related
So I have a block of code to upload an image to an S3 Bucket. It's fairly boilerplate. Works perfectly running from localhost.
I push it to my Ubuntu EC2 server, and the code fails. No error, no exceptions, Debug => true outputs nothing.. Trying to var_dump the $s3Client variable reports nothing.
E_ALL Error reporting is on
PHP 8.1.4
Nginx 1.21.6
Compatability-test.php passes successfully - all required modules enabled.
Code:
<?php
require 'S3/aws-autoloader.php';
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
$bucket = 'XXXXX';
try {
//Create a S3Client
$s3Client = new S3Client([
'profile' => 'default',
'region' => 'us-east-1',
'version' => '2006-03-01',
'signature' => 'v4',
'debug' => true,
'credentials' => [
'key' => XXXXX,
'secret' => XXXXX,
]
]);
$result = $s3Client->putObject([
'Bucket' => $bucket,
'Key' => $fileName,
'SourceFile' => $filePath,
'ACL' => 'public-read'
]);
echo json_encode(array('success' => true, 'imageUrl' => $result->get('ObjectURL')));
} catch (S3Exception $exception) {
echo $exception->getMessage() . "\n";
}
Has anyone else experienced this 'quiet failure' with the AWS PHP-SDK S3?
I solved this, after 3 days. There were several problems, which I managed to solve and inspect by running the code from the PHP CLI.
First things to note:
Credentials must be stored in the /var/www/.aws/credentials file. This is where the S3Client class will look for your S3 credentials. Passing them into the S3 class instantiation does not appear to work.
Running from the CLI, indicated that I was missing the PHP module mbstring. I don't know why Amazon's compatibility-test.php file does not flag this module as missing. Go figure.
Why the script failed silently, I really don't know. Maybe some error suppression somewhere in the S3Client class. I highly recommend running from the PHP-CLI to diagnose any errors with the PHP AWS S3 SDK.
I realize this is a bit late, but I experienced a similar issue recently where specifically I did not want to use the credentials file, whereas your answer notes that the credentials must be stored in the credentials file.
In my experience, what wound up working for me was removing the 'profile' => 'default', in the s3Client creation. So like this ...
//Create a S3Client
$s3Client = new S3Client([
'region' => 'us-east-1',
'version' => '2006-03-01',
'signature' => 'v4',
'debug' => true,
'credentials' => [
'key' => XXXXX,
'secret' => XXXXX,
]
]);
So if you minimally want to use the inline credentials instead of the credentials file, this may help you out a bit.
Is that possible, to change system from Amazon S3 to work with the other S3 provider, like cap radosgw? I'm using league/fly system-aws-s3-v3 and aws/aws-SDK-PHP packages for Laravel, but they want me to type the region, which I don't have on Ceph radosgw, and automatically connect me with Amazon services. I was trying to add my server as an 'endpoint' in filesystems.php, but it is still forcing me to choose a region.
This is my s3 disk configuration:
's3' => [
'driver' => 's3',
'key' => 'XXXTXOXXXFXX77XXXXXX',
'secret' => 'XXXX2XXXXXXS85XXXX5XXXXXXXXRXXXXXXX4XXX',
'bucket' => 'my-bucket',
'endpoint' => 'http://s3.mysite.io'
],
And this is the error I'm getting:
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.
I am doing project in Laravel. For storing images I am using aws-s3. My project working very well on godaddy server but not on digital ocean server. I am getting error as,
Aws\Sqs\Exception\SqsException: Error executing "ReceiveMessage" on "https://sqs.us-east-1.amazonaws.com/your-account-id/your-queue-name"; AWS HTTP error: Client error: `POST https://sqs.us-east-1.amazonaws.com/your-account-id/your-queue-name` resulted in a `403 Forbidden` response:
<?xml version="1.0"?><ErrorResponse xmlns="http://queue.amazonaws.com/doc/2012-11-05/"><Error><Type>Sender</Type><Code>I (truncated...)
InvalidClientTokenId (client): The security token included in the request is invalid. - <?xml version="1.0"?><Err......
Here Is a code that I used for s3 setup in my project,
filesystem.php
's3' => [
'driver' => 's3',
'key' => '************',
'secret' => '***********',
'region' => 'ap-south-1',
'bucket' => 'xyz',
'options' => [
'ServerSideEncryption' => 'AES256',
]
]
.env
QUEUE_DRIVER=redis
I am getting this error irrespective of hitting any api. I don't know where I am going wrong. Thanks in advance.
Issue is documented under aws blog,
https://aws.amazon.com/premiumsupport/knowledge-center/security-token-expired/
Please check your credentials and appropriate roles / Policies attaches to roles to access S3.
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 am trying to upload/retrieve files from Amazon S3 using Laravels FileSystem.
I have setup an Amazon S3 account and created a new user using IAM. I've given the user the policy of AmazonS3FullAccess. I have generated the the keys and copied the access and secret key.
I've added the keys to the file system config along with the region Frankfurt and my bucketname. I've also set the default disk to s3.
When i try to simply upload a file to the S3 disk I'm getting an error.
's3' => [
'driver' => 's3',
'key' => env('S3_KEY'),
'secret' => env('S3_SECRET'),
'region' => env('S3_REGION'),
'bucket' => env('S3_BUCKET'),
],
Php:
Route::get('s3', function() {
Storage::put('myfile.txt' , 'Test File');
});
Error:
Error executing "ListObjects" on "https://s3.frankfurt.amazonaws.com/bucketnamehere?prefix=myfile.txt%2F&max-keys=1&encoding-type=url"; AWS HTTP error: Error creating resource: [message] fopen(https://s3.frankfurt.amazonaws.com/bucketnamehere?prefix=myfile.txt%2F&max-keys=1&encoding-type=url): failed to open stream: Connection refused
[file] /var/www/ProjectNameHere-Events/vendor/guzzlehttp/guzzle/src/Handler/StreamHandler.php
[line] 312
Two points I noticed from the HTTP request, you need to add the correct parameters in your .env
S3_REGION=eu-central-1
S3_BUCKET=CORRECT_BUCKET_NAME
I noticed from the endpoint that the bucket name is incorrect, unless you have changed for security.