How add "external-id" parameter into AssumeRole method? - php

I'm trying to connect to another account's bucket through "AssumeRole". Through CLI everything is working (I'm just adding "External ID" through parameter "--external-id"), but through SDK - I cannot to find how to add "External ID" correctly.
Code Example:
$stsClient = new StsClient([
'region' => 'us-west-2',
'version' => 'latest',
'credentials' => $provider
]);
$ARN = "arn:aws:iam::12345678:role/some_test_role";
$sessionName = "some-test-session-name";
$externalId = "TEST-EXTERNAL-ID"; //How to pass it into AssumeRole?
$assumeRoleResult = $stsClient->AssumeRole([
'RoleArn' => $ARN,
'RoleSessionName' => $sessionName
]);

Related

how to get statuscode from aws s3 putobject response

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

Credentials must be an instance of Aws\\Credentials\\CredentialsInterface error on PHP SDK with SQS

I am trying to delete a message from SQS in AWS using the php SDK. I have the following configuration.
$sqsClient = new SqsClient([
'version' => '2012-11-05',
'region' => 'us-east-1',
'credentials' => [
'key' => KEY,
'secret' => SECRET
]
]);
Then I am trying to delete like the following :
$sqsClient->deleteMessage([
'QueueUrl' => quque-url
'ReceiptHandle' => handle
]);
I am getting the following error on initialization :
Credentials must be an instance of Aws\\Credentials\\CredentialsInterface, an associative array that contains \"key\", \"secret\", and an optional \"token\" key-value pairs, a credentials provider function, or false."
The credentials I am using is correct. Before I was not passing the config and then also the same error was appearing. How this can be fixed ?
I found this code example and I think your code is correct.
How about create instance of Credentials?
You can use these code for initiating instance.
$credentials = new Aws\Credentials\Credentials(KEY, SECRET);
$sqsClient = new SqsClient([
'version' => '2012-11-05',
'region' => 'us-east-1',
'credentials' => $credentials
]);
Check this document.
And for security reason, I recommend that you'd better use credentials file or set environment variable.
I hope all is well.

AWS S3 authorization mechanism - Signature 4

I'm currently struggling with getting a S3 download link working. I've been using this code as reference but when I try to open the file, I get the error:
The authorization mechanism you have provided is not supported. Please
use AWS4-HMAC-SHA256.
I tried a few other scripts floating around, but all ended with some other error message.
Is there an easy way to migrate the script I'm using to make it work with Signature v4?
UPDATE: as suggested by hjpotter92, I used the AWS-SDK and came up with this working code:
$client = S3Client::factory([
'version' => 'latest',
'region' => 'eu-central-1',
'signature' => 'v4',
'credentials' => [
'key' => '12345',
'secret' => 'ABCDE'
]
]);
$cmd = $client->getCommand('GetObject', [
'Bucket' => '###name###',
'Key' => $fileName
]);
$request = $client->createPresignedRequest($cmd, '+2 minutes');
$presignedUrl = (string) $request->getUri();
return $presignedUrl;

Amazon SDK for Php SNS Publish Error 500

I am getting an error 500 when trying to do a SNS publish using the Amazon AWS SDK for PHP. If I run PHP sendPush.php in terminal it works perfectly fine but if I go to the URL in chrome I get an error 500.
Here is my PHP file:
<?php
require 'vendor/autoload.php';
use Aws\Credentials\CredentialProvider;
use Aws\Sns\SnsClient;
$arn = "<my arn endpoint here>";
$message = 'test1';
$provider = CredentialProvider::env();
$client = SnsClient::factory(array(
'profile' => 'default',
'credentials' => $provider,
'version' => 'latest',
'region' => 'us-east-1'
));
$message_atr = array(
'String' => array(
'DataType' => 'String',
'StringValue' => $message
)
);
$publish_message = array('TargetArn' => $arn,'Message' => $message,'MessageAttributes'=> $message_atr);
$client->publish($publish_message);
?>
I had the .aws folder in the wrong location, it works now.

Laravel AWS PHP SDK - CreatePlatformEndpoint throws 400 Bad Request

I am using "aws/aws-sdk-php-laravel": "~3.0"
When i am trying to register the android device its throwing the error.
Below is the snapshot of it.
I tried with 2 different code formats but both gives the same exception.
$device = 'gcm_id_here';
$sns = App::make('aws')->createClient('sns');
$result = $sns->createPlatformEndpoint(['Token' => 'device_unique_id',
'PlatformApplicationArn' => $device]);
// or
$credentials = new Credentials('key', 'secret');
$client = new SnsClient([
'version' => 'latest',
'region' => 'us-west-2',
'credentials' => $credentials
]);
$SNSEndPointData = $client->createPlatformEndpoint([
'PlatformApplicationArn' => $device,
'Token' => 'device_unique_id'
]);
Please help me solve this issue.

Categories