I'm trying to create a cloudfront distribution while doing the authentication via hardcoded credentials.
However i receive this error when i run my code
Fatal error: Uncaught Aws\Exception\CredentialsException: Cannot read credentials from /.aws/credentials
It seems that the aws sdk is trying to authentificate using the second method listed here ( https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html ) - the one when you put the credentials in ./aws folder
Here is my code (taken from aws documentation ) - Any idea why this is not working ?
public function create_cloudfront_client(){
$region='us-east-1';
$client = new Aws\CloudFront\CloudFrontClient([
'profile' => 'default',
'version' => 'latest',
'region' => 'us-east-1',
'debug' => true,
'credentials' =>[
'key' => $this->aws_key,
'secret' => $this->aws_secret,
],
]);
$originName = 'cloudfrontme';
$s3BucketURL = 'https://s3.amazonaws.com/cloudfrontme';
$callerReference = 'uniquestring99';
$comment = 'Created by AWS SDK for PHP';
$cacheBehavior = [
'AllowedMethods' => [
'CachedMethods' => [
'Items' => ['HEAD', 'GET'],
'Quantity' => 2,
],
'Items' => ['HEAD', 'GET'],
'Quantity' => 2,
],
'Compress' => false,
'DefaultTTL' => 0,
'FieldLevelEncryptionId' => '',
'ForwardedValues' => [
'Cookies' => [
'Forward' => 'none',
],
'Headers' => [
'Quantity' => 0,
],
'QueryString' => false,
'QueryStringCacheKeys' => [
'Quantity' => 0,
],
],
'LambdaFunctionAssociations' => ['Quantity' => 0],
'MaxTTL' => 0,
'MinTTL' => 0,
'SmoothStreaming' => false,
'TargetOriginId' => $originName,
'TrustedSigners' => [
'Enabled' => false,
'Quantity' => 0,
],
'ViewerProtocolPolicy' => 'allow-all',
];
$enabled = false;
$origin = [
'Items' => [
[
'DomainName' => $s3BucketURL,
'Id' => $originName,
'OriginPath' => '',
'CustomHeaders' => ['Quantity' => 0],
'S3OriginConfig' => ['OriginAccessIdentity' => ''],
],
],
'Quantity' => 1,
];
$distribution = [
'CallerReference' => $callerReference,
'Comment' => $comment,
'DefaultCacheBehavior' => $cacheBehavior,
'Enabled' => $enabled,
'Origins' => $origin,
];
try {
$result = $client->createDistribution([
'DistributionConfig' => $distribution, //REQUIRED
]);
var_dump($result);
} catch (AwsException $e) {
// output error message if fails
echo $e->getMessage();
echo "\n";
}
}
The solution was to create the cloudfront client like this
$client = Aws\CloudFront\CloudFrontClient::factory(array(
'region' => $bucket_region,
'version' => 'latest',
'credentials' => [
'key' => $this->aws_key,
'secret' => $this->aws_secret,
]
));
However i don't understand why this version works while the one below (from aws docs ) does not. Can anyone explain this ?
Thanks
$client = new Aws\CloudFront\CloudFrontClient([
'version' => 'latest',
'region' => $bucket_region,
'debug' => true,
'credentials' =>[
'key' => $this->aws_key,
'secret' => $this->aws_secret,
],
]);
Related
I'm having a tough time figuring out how to properly code sequenced SOAP requests to the Bing Ads API. Prefer not to use their SDK, which I have used in the past.
The parameters 'Scope', 'Time', 'Filter', and 'Sort' do not affect the result. The entire account keywords are returned instead. For 'Scope', I am using the Adgroups param to select keywords in that Adgroup. Any help is greatly appreciated.
Reference: https://learn.microsoft.com/en-us/advertising/reporting-service/keywordperformancereportrequest?view=bingads-13
WSDL: https://reporting.api.bingads.microsoft.com/Api/Advertiser/Reporting/V13/ReportingService.svc?singleWsdl
$request = [
'ReportRequest' => new SoapVar(
[
'Format' => 'Csv',
'ReportName' => 'Bing Keyword Performance Report',
'ReturnOnlyCompleteData' => false,
'Aggregation' => 'Daily',
'Sort' => array('SortColumn' => 'Clicks','SortOrder' => 'Ascending'),
'Scope' => ['AdGroups' => array(array('AccountId' => $bClientId,
'AdGroupId' => $apiDBIdGroupBing,
'CampaignId' => $apiDBIdCampaignBing,
))],
'Time' => [
'CustomDateRangeStart' =>
array('Day' => $startDay,'Month' => $startMonth,'Year' => $startYear),
'CustomDateRangeEnd' =>
array('Day' => $endDay,'Month' => $endMonth,'Year' => $endYear)
],
'Filter' => ['Keywords' => array($criteriaValue)],
'Columns' => [
"TimePeriod",
"Spend",
"Clicks",
"CurrentMaxCpc",
"Impressions",
"AdGroupName"
]
],
SOAP_ENC_OBJECT,
'KeywordPerformanceReportRequest',
"https://bingads.microsoft.com/Reporting/v13"
)];
Solved:
$request = [
'ReportRequest' => new SoapVar(
[
'Format' => 'Csv',
'ReportName' => 'Bing Keyword Performance Report',
'ReturnOnlyCompleteData' => false,
'Aggregation' => 'Monthly',
'Sort' => array('SortColumn' => 'Clicks','SortOrder' => 'Ascending'),
'Scope' => ['AccountIds' => [$bClientId]],
'MaxRows' => '9000000',
'Time' => ['PredefinedTime' => $reportTimeFrame],
'Columns' => [
"Keyword",
"Spend",
"CampaignId",
"AdGroupId",
"AveragePosition",
"CurrentMaxCpc",
"KeywordId",
"BidMatchType",
"Impressions",
"Clicks",
"TimePeriod",
"QualityScore",
"ExpectedCtr",
"AdRelevance",
"LandingPageExperience",
"CampaignStatus",
"AdGroupStatus",
"KeywordStatus",
"AccountName",
"CampaignName",
"AdGroupName",
"BidStrategyType",
]
],
SOAP_ENC_OBJECT,
'KeywordPerformanceReportRequest',
"https://bingads.microsoft.com/Reporting/v13"
)];
$response = $SoapClient->SubmitGenerateReport($request);
I want to update the status of an Amazon CloudFront distribution using the updatedistribution method provided by AWS.
I don't know default value of all the required parameters.
My code is:
list($before, $after) = explode('.', $domain, 2);
$domain_Items = "*." . $after;
$result = $client->updateDistribution([
'DistributionConfig' =>
[
'Aliases' =>
[
'Items' => [$domain_Items, $after],
'Quantity' => 2
],
'CallerReference' => $domain,
'Comment' => 'custom domain for ' . $domain,
'Enabled' => false,
'PriceClass' => 'PriceClass_All',
'CacheBehaviors' => [
'Items' => [
[
'AllowedMethods' =>
[
'CachedMethods' =>
[
'Items' => ['HEAD', 'GET'],
'Quantity' => 2,
],
'Items' => ['HEAD', 'GET', 'OPTIONS', 'PUT', 'POST', 'PATCH', 'DELETE'],
'Quantity' => 7,
],
'Compress' => true,
'DefaultTTL' => 0,
//'FieldLevelEncryptionId' => '<string>',
'ForwardedValues' => [
'Cookies' => [
'Forward' => 'all',
'WhitelistedNames' => [
'Quantity' => 5,
'Items' => ['Host', 'Referer', 'Orign', 'User-Agent', 'HTTP_REFERER'],
],
],
'Headers' =>
[
'Items' => ['Host', 'Referer', 'Orign', 'User-Agent', 'HTTP_REFERER'],
'Quantity' => 5
],
'QueryString' => true,
'QueryStringCacheKeys' => [
'Items' => ['Host', 'Referer', 'Orign', 'User-Agent', 'HTTP_REFERER'],
'Quantity' => 5
],
],
'LambdaFunctionAssociations' => [
'Items' => [
[
'EventType' => 'viewer-request',
'IncludeBody' => true,
'LambdaFunctionARN' => '', //<string>
]
],
'Quantity' => 1,
],
'MaxTTL' => 0,
'MinTTL' => 0,
'PathPattern' => '', //<string>
'SmoothStreaming' => true,
'TargetOriginId' => 'ELB-saglus-test-uat-web-783948842',
'TrustedSigners' => [
'Enabled' => false,
//'Items' => ['<string>'],
'Quantity' => 0,
],
'ViewerProtocolPolicy' => 'redirect-to-https',
],
],
'Quantity' => 1,
],
'DefaultCacheBehavior' =>
[
'AllowedMethods' =>
[
'CachedMethods' =>
[
'Items' => ['HEAD', 'GET'],
'Quantity' => 2,
],
'Items' => ['HEAD', 'GET', 'OPTIONS', 'PUT', 'POST', 'PATCH', 'DELETE'],
'Quantity' => 7,
],
'Compress' => true,
'DefaultTTL' => 0,
'FieldLevelEncryptionId' => '',
'ForwardedValues' =>
[
'Cookies' =>
[
'Forward' => 'all'
],
'WhitelistedNames' => [
'Quantity' => 5,
'Items' => ['Host', 'Referer', 'Orign', 'User-Agent', 'HTTP_REFERER'],
],
'Headers' =>
[
'Items' => ['Host', 'Referer', 'Orign', 'User-Agent', 'HTTP_REFERER'],
'Quantity' => 5,
],
'QueryString' => true,
'QueryStringCacheKeys' => [
'Items' => ['Host', 'Referer', 'Orign', 'User-Agent', 'HTTP_REFERER'],
'Quantity' => 5, // REQUIRED
],
],
'LambdaFunctionAssociations' => [
'Items' => [
[
'EventType' => 'viewer-request',
'IncludeBody' => false,
'LambdaFunctionARN' => '', // REQUIRED <string>
]
],
'Quantity' => 1, // REQUIRED
],
'MaxTTL' => 600,
'MinTTL' => 0,
'SmoothStreaming' => false,
'TargetOriginId' => 'ELB-saglus-test-uat-web-783948842',
'TrustedSigners' =>
[
'Enabled' => false,
'Quantity' => 0
],
'ViewerProtocolPolicy' => 'redirect-to-https'
],
'DefaultRootObject' => '',
'HttpVersion' => 'http2',
'IsIPV6Enabled' => false,
'Logging' => [
'Bucket' => 'saglus-aws-logs.s3.amazonaws.com', // REQUIRED
'Enabled' => true, // REQUIRED
'IncludeCookies' => true, // REQUIRED
'Prefix' => 'logs-for-' . $domain, // REQUIRED
],
'Origins' =>
[
'Items' =>
[
[
'CustomHeaders' =>
[
'Items' =>
[
[
'HeaderName' => 'X-Origin-Verify',
'HeaderValue' => 'cnxpwcausbtobmebhebadbergdifn'
],
],
'Quantity' => 1
],
'CustomOriginConfig' =>
[
'HTTPPort' => 80,
'HTTPSPort' => 443,
'OriginKeepaliveTimeout' => 5,
'OriginProtocolPolicy' => 'http-only',
'OriginReadTimeout' => 120,
'OriginSslProtocols' =>
[
'Items' => ['TLSv1'],
'Quantity' => 1
],
],
'DomainName' => 'saglus-test-uat-web-783948842.us-east-1.elb.amazonaws.com',
'Id' => 'ELB-saglus-test-uat-web-783948842',
'OriginPath' => ''
],
],
'Quantity' => 1,
],
'WebACLId' => '108e7697-00db-4330-8d55-bbe57ca94e44'
],
'Id' => $distribution_id,
'IfMatch' => $ETag,
]);
However, I get the error:
Reference link: updatedistribution in AWS SDK for PHP 3.x
$client = new Aws\CloudFront\CloudFrontClient([
'region' => 'us-east-1',
'version' => 'latest',
'credentials' => [
'key' => $this->AcmKey,
'secret' => $this->AcmSecret
]
]);
// $id = 'E2SYUN95DWJFXC';
$id = $business_custom_data->distribution_id;
try {
$result = $client->getDistribution([
'Id' => $id,
]);
} catch (AwsException $e) {
// output error message if fails
echo $e->getMessage();
echo "\n";
}
$currentConfig = $result["Distribution"]["DistributionConfig"];
$ETag = $result["ETag"];
$distribution = [
'CallerReference' => $currentConfig["CallerReference"], // REQUIRED
'Comment' => $currentConfig["Comment"], // REQUIRED
'DefaultCacheBehavior' => $currentConfig["DefaultCacheBehavior"], // REQUIRED
'DefaultRootObject' => $currentConfig["DefaultRootObject"],
//'Enabled' => $currentConfig["Enabled"], // REQUIRED
'Enabled' => False, // REQUIRED
'Origins' => $currentConfig["Origins"], // REQUIRED
'Aliases' => $currentConfig["Aliases"],
'CustomErrorResponses' => $currentConfig["CustomErrorResponses"],
'HttpVersion' => $currentConfig["HttpVersion"],
'CacheBehaviors' => $currentConfig["CacheBehaviors"],
'Logging' => $currentConfig["Logging"],
'PriceClass' => $currentConfig["PriceClass"],
'Restrictions' => $currentConfig["Restrictions"],
'ViewerCertificate' => $currentConfig["ViewerCertificate"],
'WebACLId' => $currentConfig["WebACLId"],
];
try {
$result = $client->updateDistribution([
'DistributionConfig' => $distribution,
'Id' => $id,
'IfMatch' => $ETag
]);
$status = true;
//var_dump($result);
//die;
} catch (AwsException $e) {
// output error message if fails
echo $e->getMessage();
echo "\n";
}
I am having issues when sending documents for a recipient to sign it. The documents should be prefilled using the data from my database which works fine. I know this because when I var_dump $this->textTabs I can see the data. $global is false.
The issue I am having is that when the recipient receives an email to sign the document, the fields which should be prefilled are blank. The Data Label matches the tabLabel in the code below and image
$this->textTabs = [
[
'tabLabel' => 'RegisteredName',
'value' => $contactData->company->name,
'global' => $global,
],
[
'tabLabel' => 'NumberOfEmployees',
'value' => $contactData->company->employee_size,
'global' => $global,
],
[
'tabLabel' => 'RegisteredNumber',
'value' => $contactData->company->reg_no,
'global' => $global,
],
[
'tabLabel' => 'SupplyAddress',
'value' => $supplyAddress_1 . $supplyAddress_2 . $supplyCity . $supplyTown . $supplyCounty . $supplyPostcode,
'global' => $global,
],
[
'tabLabel' => 'SicCode',
'value' => $contactData->company->gapSite->scat_code,
'global' => $global,
],
[
'tabLabel' => 'Recipient_UserTitle',
'value' => $title,
'global' => $global,
],
[
'tabLabel' => 'Recipient_UserName',
'value' => $title . $firstName . $lastName,
'global' => $global,
],
[
'tabLabel' => 'Recipient_Email',
'value' => $contactData->email,
'global' => $global,
],
[
'tabLabel' => 'ContactTelephone',
'value' => $contactData->telephone,
'global' => $global,
],
];
private function send(string $status): ?array
{
try {
return Docusign::createEnvelope([
'templateId' => $this->templateId,
'emailSubject' => $this->emailSubject,
'status' => $status,
'templateRoles' => [
[
'name' => $this->recipient['name'],
'email' => $this->recipient['email'],
'roleName' => 'Client',
'tabs' => [
'textTabs' => $this->textTabs,
/* 'signHereTabs' => [
[
"xPosition" => 400,
"yPosition" => 263,
"documentId" => 1,
"pageNumber" => 6
]
],
*/
]
]
],
'eventNotification' => [
'url' => 'https://webhook.site/697fabec-145a-491f-87ce-1d62be82b298',
'LoggingEnabled' => true,
'RequireAcknowledgment' => true,
'UseSoapInterface' => false,
'IncludeCertificateWithSoap' => false,
'SignMessageWithX509Cert' => false,
'includeDocuments' => true,
'includeEnvelopeVoidReason' => true,
'includeTimeZone' => true,
'includeSenderAccountAsCustomField' => true,
'includeDocumentFields' => true,
'includeCertificateOfCompletion' => true,
'envelopeEventStatusCode' => $this->getRecipientEvents(),
'recipientEvents' => $this->getEventNotification(),
]
]);
} catch (\Exception $e) {
}
}
The fact That recipient receives the email to sign the document, already eliminate any issue to do with my code but why is it not prefilling with the data?
After a while, I decided to create a new docusign account and this seems to have solved the issue. I believe my account was bugger or maybe some technical issue on docusign,
I am using aws php sdk and want to use dynamoDb with it.
I created an ec2 instance and install php and aws php sdk.
Now I am running this code
require 'vendor/autoload.php';
date_default_timezone_set('UTC');
use Aws\DynamoDb\Exception\DynamoDbException;
$sdk = new Aws\Sdk([
'endpoint' => 'http://my_ec2_instance.com',
'region' => 'us-east-1c',
'version' => 'latest',
'credentials' => array(
'key' => 'MY_KEY',
'secret' => 'My_SECRET',
)
]);
$dynamodb = $sdk->createDynamoDb();
params = [
'TableName' => 'Movies',
'KeySchema' => [
[
'AttributeName' => 'year',
'KeyType' => 'HASH' //Partition key
],
[
'AttributeName' => 'title',
'KeyType' => 'RANGE' //Sort key
]
],
'AttributeDefinitions' => [
[
'AttributeName' => 'year',
'AttributeType' => 'N'
],
[
'AttributeName' => 'title',
'AttributeType' => 'S'
],
],
'ProvisionedThroughput' => [
'ReadCapacityUnits' => 10,
'WriteCapacityUnits' => 10
]
];
try {
$result = $dynamodb->createTable($params);
echo 'Created table. Status: ' .
$result['TableDescription']['TableStatus'] ."\n";
} catch (DynamoDbException $e) {
echo "Unable to create table:\n";
echo $e->getMessage() . "\n";
}
Above example is according to aws doc and I am trying code on server not on local. When I am running above code , server become unresponsive and no table created neither any error message.
Please help.
Thanks
I'm having trouble digging through the documentation for Amazon's AWS PHP-sdk.
Basically, I just need to send a standard text message to a number. I know it is possible because amazon allows you to send messages through the console directly via this screen:
It says something about using the "publish" method, but looking through that documentation really didn't provide any answers. #Publish documentation link
Any help or guidance is appreciated. I am currently looking for a solution that uses V2 of the sdk.
Thanks in advance.
No where have a doc showing it to use with PHP. Reading the Java and C# sdk I wrote the PHP version that works.
Updated on dec 20 '18
The args passed to publish method now have a new format. Fixed!
How to send a SMS over AWS using PHP
First install aws/aws-sdk-php. Using composer:
composer require aws/aws-sdk-php
Create a php file with:
require './vendor/autoload.php';
error_reporting(E_ALL);
ini_set("display_errors", 1);
$params = array(
'credentials' => array(
'key' => 'YOUR_KEY_HERE',
'secret' => 'YOUR_SECRET_HERE',
),
'region' => 'us-east-1', // < your aws from SNS Topic region
'version' => 'latest'
);
$sns = new \Aws\Sns\SnsClient($params);
$args = array(
"MessageAttributes" => [
'AWS.SNS.SMS.SenderID' => [
'DataType' => 'String',
'StringValue' => 'YOUR_SENDER_ID'
],
'AWS.SNS.SMS.SMSType' => [
'DataType' => 'String',
'StringValue' => 'Transactional'
]
],
"Message" => "Hello World! Visit www.tiagogouvea.com.br!",
"PhoneNumber" => "FULL_PHONE_NUMBER"
);
$result = $sns->publish($args);
echo "<pre>";
var_dump($result);
echo "</pre>";
The result must have one array with many data, including MessageId.
If you are using AWS SDK version prior to 3.0, you still have create a topic and subscribe with an SMS type. But from 3.0 onward, you could send SMS direct to a number.
$client = SnsClient::factory(array(
'region' => 'us-east-1',
'version' => 'latest',
'credentials' => array(
'key' => 'key',
'secret' => 'secret')
));
$message = 'Your verification code is 4';
$payload = [
'TopicArn' => 'arn:aws:sns:XXXXX',
'Message' => $message,
'MessageStructure' => 'string',
'MessageAttribute' => [
'AWS.SNS.SMS.SenderID' => [
'DataType' => 'String',
'StringValue' => 'Sender',
],
'AWS.SNS.SMS.SMSType' => [
'DataType' => 'String',
'StringValue' => 'Transactional',
]
]
];
$result = $client->subscribe(array(
'TopicArn' => 'arn:aws:sns:XXXXX',
'Protocol' => 'sms',
'Endpoint' => 'XXXXXXXXXXX',
));
$subarn = $result['SubscriptionArn'];
$result = $client->publish($payload);
$result = $client->unsubscribe(array(
'SubscriptionArn' => $subarn,
));
Somehow Tiago's answer didn't work for me. So I had a look at the publish API from AWS-SDK. Seems like there are no parameters of SMSType & SenderID in publish method. Check here -
https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-sns-2010-03-31.html#publish
So if you want to override these parameters, the following variation of Tiago's code should work fine -
require './vendor/autoload.php';
error_reporting(E_ALL);
ini_set("display_errors", 1);
$params = array(
'credentials' => array(
'key' => 'YOUR_KEY_HERE',
'secret' => 'YOUR_SECRET_HERE',
),
'region' => 'us-east-1', // < your aws from SNS Topic region
'version' => 'latest'
);
$sns = new \Aws\Sns\SnsClient($params);
$args = array(
"MessageAttributes" => [
'AWS.SNS.SMS.SenderID' => [
'DataType' => 'String',
'StringValue' => 'YOUR_SENDER_ID'
],
'AWS.SNS.SMS.SMSType' => [
'DataType' => 'String',
'StringValue' => 'Transactional'
]
],
"Message" => "Hello World! Visit www.tiagogouvea.com.br!",
"PhoneNumber" => "FULL_PHONE_NUMBER"
);
$result = $sns->publish($args);
Hope this help for people who still using PHP AWS SDK v2
Same question: https://stackoverflow.com/a/51208701/551559
You would need to add new parameter in the source code.
// update file: aws-sdk-php/src/Aws/Sdk/Resources/sns-2010-03-31.php
'Publish' => array(
'parameters' => array(
'PhoneNumber' => array( // new parameter
'type' => 'string',
'location' => 'aws.query',
),
),
),
// You just need to publish it and include the `PhoneNumber` parameter
$snsClientResult = $snsClient->publish([
'Message' => 'YOUR_MESSAGE',
'PhoneNumber' => 'PHONE_NUMBER',
'MessageStructure' => 'SMS',
'MessageAttributes' => [
'AWS.SNS.SMS.SenderID' => [
'DataType' => 'String',
'StringValue' => 'SENDER_ID',
],
'AWS.SNS.SMS.SMSType' => [
'DataType' => 'String',
'StringValue' => 'Promotional', // Transactional
]
]
]);
// Get the response
$snsClientResult['MessageId']
this is my solution in php (add to params : 'http'=> [ 'verify' => false] )
<?php require './vendor/autoload.php';
$params = array(
'credentials' => array(
'key' => 'xxxx',
'secret' => 'xxxx',
),
'region' => 'us-east-1', // < your aws from SNS Topic region
'version' => 'latest',
'http' => [
'verify' => false
] ); $sns = new \Aws\Sns\SnsClient($params);
$args = array(
"MessageAttributes" => [
// You can put your senderId here. but first you have to verify the senderid by customer support of AWS then you can use your senderId.
// If you don't have senderId then you can comment senderId
// 'AWS.SNS.SMS.SenderID' => [
// 'DataType' => 'String',
// 'StringValue' => ''
// ],
'AWS.SNS.SMS.SMSType' => [
'DataType' => 'String',
'StringValue' => 'Transactional'
]
],
"Message" => "TESTING SMS ORDUZ",
"PhoneNumber" => "+573148832216" // Provide phone number with country code );
$result = $sns->publish($args);
var_dump($result); // You can check the response
To use the Publish action for sending a message to a mobile endpoint, such as an app on a Kindle device or mobile phone, you must specify the EndpointArn.
$result = $client->publish(array(
'TopicArn' => 'string',
'TargetArn' => 'string',
// Message is required
'Message' => 'string',
'Subject' => 'string',
'MessageStructure' => 'string',
'MessageAttributes' => array(
// Associative array of custom 'String' key names
'String' => array(
// DataType is required
'DataType' => 'string',
'StringValue' => 'string',
'BinaryValue' => 'string',
),
// ... repeated
),
));