I have a weird problem when fetching items from kinesis.
So when I have events in the stream, and Im querying for them with the correct timestamp, I get the result.
But if I don't have events in the stream
or
if I'm querying for times where not event are there
The call getShardIterator gets stuck for several minutes.
That's why I added the "timeout" of 2 seconds.
Is there a better way to just get an empty response from kinesis if no events found?
Thanks
<?php
$getKinessisClient = new GetKinessisClient();
$kinesisClient = $getKinessisClient->get([
]);
$params = [
'credentials' => array(
'key' => 'xxxx',
'secret' => 'xxxx',
),
'region' => 'xxxx',
'version' => 'latest',
'http' => [
'timeout' => 2
]
];
$kinesisClient = (new Sdk())->createKinesis($params);
// get all shard ids
$res = $kinesisClient->describeStream([ 'StreamName' => $streamName ]);
$shardIds = $res->search('StreamDescription.Shards[].ShardId');
$foundItems = [];
foreach ($shardIds as $shardId) {
try {
$getShardItParams = [
'ShardId' => $shardId,
'StreamName' => $streamName,
'ShardIteratorType' => 'AT_TIMESTAMP',
'Timestamp' => $from_timestamp, //PROBLEM HERE
];
// this gets stuck (without timeout)
$res = $kinesisClient->getShardIterator($getShardItParams);
Related
I have found the documentation for it here. I have PHP SDK installed. Now when I go through the documents there is not so much in detail about the PHP one. I have the following questions:
Here how can I specify the $client
$result = $client->createDatabase([
'DatabaseName' => '<string>', // REQUIRED
'KmsKeyId' => '<string>',
'Tags' => [
[
'Key' => '<string>', // REQUIRED
'Value' => '<string>', // REQUIRED
],
// ...
],
]);
Is there any good documents or videos regarding the timestream in PHP from where I can get some help.
There are two client classes. One for writing and one for reading.
TimestreamWriteClient
https://docs.aws.amazon.com/aws-sdk-php/v3/api/class-Aws.TimestreamWrite.TimestreamWriteClient.html
and
TimestreamQueryClient
https://docs.aws.amazon.com/aws-sdk-php/v3/api/class-Aws.TimestreamQuery.TimestreamQueryClient.html
You can use the function createTimestreamQuery and createTimestreamWrite on the $sdk object to instantiate those classes.
A sample Timestream client and query below.
// Create client
$client = new \Aws\TimestreamQuery\TimestreamQueryClient([
'version' => 'latest',
'region' => AWS_REGION, /* eg: eu-west-1 */
'endpoint' => AWS_TIMESTREAM_ENDPOINT, /* eg: https://query-cell3.timestream.eu-west-1.amazonaws.com */
'credentials' => new \Aws\Credentials\Credentials(AWS_KEY, AWS_SECRET)
]);
// Perform a basic query with the client
$client->query([
'QueryString' => 'select * from "db_timestream"."tbl_usage_logs"',
'ValidateOnly' => true,
]);
If you receive endpoint warning, such as "The endpoint required for this service is currently unable to be retrieved"
You can find endpoint using AWS CLI command,
aws timestream-query describe-endpoints --region eu-west-1
Sample response:
{
"Endpoints": [
{
"Address": "query-cell3.timestream.eu-west-1.amazonaws.com",
"CachePeriodInMinutes": 1440
}
]
}
One can create TimestreamWriteClient and write records in a similar way.
The documentation seems sparse and a bit misleading, to me anyhow.
This is how I got it going for a write client (assuming SDK is installed).
//Create the client
$client = new \Aws\TimestreamWrite\TimestreamWriteClient([
'version' => 'latest',
'region' => 'eu-west-1',
'credentials' => new \Aws\Credentials\Credentials('***KEY***', '***SECRET***')
]);
Note that the 'endpoint' is not specified, as I've seen in some examples. There seems to be some misleading documentation of what the endpoint should be for any given region. The SDK does some magic and creates a suitable endpoint; providing a specific endpoint didn't work for me.
$result = $client->writeRecords(
[
'DatabaseName' => 'testDB',
'TableName' => 'history',
'Records' =>
[
[
'Dimensions' => [
[
'DimensionValueType' => 'VARCHAR',
'Name' => 'Server',
'Value' => 'VM01',
],
],
'MeasureName' => 'CPU_utilization',
'MeasureValue' => '1.21',
'MeasureValueType' => 'DOUBLE',
'Time' => strval(time()),
'TimeUnit' => 'SECONDS',
]
]
]
);
This seems to be the minimum set of things needed to write a record to Timestream successfully. The code above writes one record, with one dimension, in this case, a 'Name' of a 'Server', recording its CPU utilization at time().
Note:
Time is required, although the documentation suggested it is optional.
Time has to be a String.
I can't figure out how to delete hosted zone resource record set with Amazon PHP sdk.
So my code is following
public function __construct(\ConsoleOutput $stdout = null, \ConsoleOutput $stderr = null, \ConsoleInput $stdin = null) {
parent::__construct($stdout, $stderr, $stdin);
/** #var \Aws\Route53\Route53Client route53Client */
$this->route53Client = Route53Client::factory([
'version' => '2013-04-01',
'region' => 'eu-west-1',
'credentials' => [
'key' => <my-key>,
'secret' => <my-secret-key>
]
]);
}
And this is my function for deleting resource record set
private function deleteResourceRecordSet() {
$response = $this->route53Client->changeResourceRecordSets([
'ChangeBatch' => [
'Changes' => [
[
'Action' => 'DELETE',
'ResourceRecordSet' => [
'Name' => 'pm-bounces.subdomain.myDomain.com.',
'Region' => 'eu-west-1',
'Type' => 'CNAME',
],
]
]
],
'HostedZoneId' => '/hostedzone/<myHostedZoneId>'
]);
var_dump($response);
die();
}
And the error I'm keep getting is
Error executing "ChangeResourceRecordSets" on "https://route53.amazonaws.com/2013-04-01/hostedzone/<myHostedZoneId>/rrset/"; AWS HTTP error: Client error: `POST https://route53.amazonaws.com/2013-04-01/hostedzone/<myHostedZoneId>/rrset/` resulted in a `400 Bad Request` response:
<?xml version="1.0"?>
<ErrorResponse xmlns="https://route53.amazonaws.com/doc/2013-04-01/"><Error><Type>Sender</Type><Co (truncated...)
InvalidInput (client): Invalid request: Expected exactly one of [AliasTarget, all of [TTL, and ResourceRecords], or TrafficPolicyInstanceId], but found none in Change with [Action=DELETE, Name=pm-bounces.subdomain.myDomain.com., Type=CNAME, SetIdentifier=null] - <?xml version="1.0"?>
<ErrorResponse xmlns="https://route53.amazonaws.com/doc/2013-04-01/"><Error><Type>Sender</Type><Code>InvalidInput</Code><Message>Invalid request: Expected exactly one of [AliasTarget, all of [TTL, and ResourceRecords], or TrafficPolicyInstanceId], but found none in Change with [Action=DELETE, Name=pm-bounces.subdomain.myDomain.com., Type=CNAME, SetIdentifier=null]</Message>
So what exactly is minimum required set of params so I will be available to delete resource record from hosted zone? If you need any additional informations, please let me know and I will provide. Thank you
Ok I have figure it out. If you wan't to delete resource record set from hosted zones, then the code/function for deleting record set should look like following
private function deleteResourceRecordSet($zoneId, $name, $ResourceRecordsValue, $recordType, $ttl) {
$response = $this->route53Client->changeResourceRecordSets([
'ChangeBatch' => [
'Changes' => [
[
'Action' => 'DELETE',
"ResourceRecordSet" => [
'Name' => $name,
'Type' => $recordType,
'TTL' => $ttl,
'ResourceRecords' => [
$ResourceRecordsValue // should be reference array of all resource records set
]
]
]
]
],
'HostedZoneId' => $zoneId
]);
}
I am trying to download my archive from Amazon Glacier using expedited option. I'm doing it via PHP with PHP SDK3. I have a little problem. I've launched job to get ArchiveID:
$credentials = new Credentials('GLA_AWS_KEY', 'GLA_AWS_SECRET');
$client = new GlacierClient(array(
'version' => 'latest',
'credentials' => $credentials,
'region' => 'GLA_AWS_REGION'
));
$result = $client->initiateJob(array(
'vaultName' => 'GLA_AWS_VAULT',
'jobParameters' => [
'Type' => 'archive-retrieval',
'ArchiveId' => $archiveId,
]
));
$jobid = $result->get('jobId');
How can you recover the file in expedited mode?
Thanx for any help ;D
Finally I found the answer. For anyone interested on it.
$result = $client->initiateJob(array(
'vaultName' => 'GLA_AWS_VAULT',
'jobParameters' => [
'Type' => 'archive-retrieval',
'ArchiveId' => $archiveId,
'Tier' => 'Expedited'
]
));
We need to add the Tier as Expedited. The download time reduces to 5 minuts more or less.
$accessKey = 'XZA...';
$accessSecret = 'YKW...';
$credentials = new Aws\Credentials\Credentials($accessKey, $accessSecret);
$sharedConfig = [
'region' => 'us-east-1',
'version' => 'latest',
'credentials' => $credentials
];
$sdk = new Aws\Sdk($sharedConfig);
$sns = new SnsClient($sharedConfig);
$payload = [
'PhoneNumber' => '+999999999', // E.164 format
'Message' => md5(time()),
'MessageAttributes' => [
'DefaultSenderID' => ['DataType'=>'String','StringValue'=>'MyBrandName'],
'DefaultSMSType' => ['DataType'=>'String','StringValue'=>'Transactional']
]
];
try {
$data = $sns->publish( $payload );
$MessageId = $data->get('MessageId');
} catch ( Exception $e ) { }
I'm using the AWS SDK for PHP - Version 3.
The code above works well when i'm sending a single SMS message except the attribute DefaultSenderID wich is not working when i send a SMS to a mobile device.
Amazon documentation says that DefaultSenderID – A string, such as your business brand, that is displayed as the sender on the receiving device. Support for sender IDs varies by country. The sender ID can be 1 - 11 alphanumeric characters, and it must contain at least one letter.
Anyone has experienced this problem using the Amazon SNS?
For anybody still struggling with this.
If you look at the documentation here, you will find that you need to add the key AWS.SNS.SMS.SenderID to the payload's MessageAttributes.
The following should work:
$payload = [
'PhoneNumber' => '+999999999', // E.164 format
'Message' => md5(time()),
'MessageAttributes' => [
'AWS.SNS.SMS.SenderID' => [
'DataType' => 'String',
'StringValue' => 'YourSenderID',
]
]
];
try {
$data = $sns->publish($payload);
$MessageId = $data->get('MessageId');
} catch (Exception $e) { }
I have to write 3 columns in google sheet using v4 api in php
$range = $sheetName;
$response = $service->spreadsheets_values->get($spreadsheetId, $range);
$values = $response->getValues();
$lastRow = count($values);
$rowIndex = $lastRow;
$columnIndex = 0;
$requests = array();
$requests[] = new Google_Service_Sheets_Request(array(
'updateCells' => array(
'start' => array(
'sheetId' => $sheetId,
'rowIndex' => $rowIndex,
'columnIndex' => $columnIndex
),
'rows' => array(
array(
'values' => array(
array(
'userEnteredValue' => array('numberValue' => '111111'),
'userEnteredFormat' => array('backgroundColor' => array('red'=>1, 'green'=>0.94, 'blue'=>0.8))
),
array(
'userEnteredValue' => array('stringValue' => 'aaaaaaaaa'),
'userEnteredFormat' => array('backgroundColor' => array('red'=>1, 'green'=>0.94, 'blue'=>0.8))
),
array(
'userEnteredValue' => array('numberValue' => '2015-05-05'),
'userEnteredFormat' => array('numberFormat' => array('type'=>'DATE', 'pattern'=>'yyyy-mm-dd'), 'backgroundColor' => array('red'=>1, 'green'=>0.94, 'blue'=>0.8))
)
)
)
),
'fields' => 'userEnteredValue,userEnteredFormat.backgroundColor'
)
));
$batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest(array(
'requests' => $requests
));
try {
$service->spreadsheets->batchUpdate($spreadsheetId, $batchUpdateRequest);
} catch(Exception $e) {
print_R($e);
}
I am using above code to do the task, the only problem I am facing is that I am getting an error while inserting date.
I read that datetimerenderoption is to be used for inserting the date but I am not getting how to do it.
DateTimeRenderOption is used in the values collection APIs (and are also just for reading, not writing). You're using the spreadsheets collection APIs, which are more bare-bones and don't use that convenience option.
If you paste the error you're receiving it will be easier to help, but I'm assuming it has something to do with the lines that set a numberValue. The docs say numberValue takes a number, not a string. If you'd like to set a date, you'll need to provide the number for that date in Serial Number format. You can review this SO answer for good suggestions on dealing with serial numbers.