Пример #1
0
function sqs_client_enqueue($data, $config)
{
    $result = array();
    $err = '';
    $region = empty($config['sqs_region']) ? empty($config['s3_region']) ? 'us-east-1' : $config['s3_region'] : $config['sqs_region'];
    // fallback to same region as s3 bucket
    $message_id = '';
    $options_array = ['version' => 'latest', 'region' => $region];
    if ($config['aws_secret_access_key'] && $config['aws_access_key_id']) {
        $options_array['credentials'] = [];
        $options_array['credentials']['key'] = $config['aws_access_key_id'];
        $options_array['credentials']['secret'] = $config['aws_secret_access_key'];
    }
    try {
        $sqs = new Aws\Sqs\SqsClient($options_array);
        $response = $sqs->sendMessage(['MessageBody' => json_encode($data), 'QueueUrl' => $config['sqs_queue_url']]);
        if ($config['log_api_response']) {
            log_file("sqs response:\n" . print_r($response, 1), $config);
        }
        if (empty($response['MessageId'])) {
            $err = 'Got no MessageId in sqs response';
        } else {
            $result['id'] = $response['MessageId'];
        }
    } catch (Exception $e) {
        $err = $e->getMessage();
    }
    if ($err) {
        $result['error'] = $err;
    }
    return $result;
}
 /**
  * Get the AWS SDK SQS instance (pseudo singleton).
  *
  * @return AWS SQS instance.
  */
 public function getInstance()
 {
     if ($this->_sqs === null) {
         if ($this->_accessKey === null && $this->_secretKey === null) {
             // If you don't specify the key and secret, then the SDK will use the IAM role of the machine for the credentials.
             if ($this->_awsApiVersion == 2) {
                 $this->_sqs = Aws\Sqs\SqsClient::factory(array('region' => $this->_region));
             } else {
                 $this->_sqs = Aws\Sqs\SqsClient::factory(array('region' => $this->_region, 'version' => '2012-11-05'));
             }
         } else {
             if ($this->_awsApiVersion == 2) {
                 $this->_sqs = Aws\Sqs\SqsClient::factory(array('key' => $this->_accessKey, 'secret' => $this->_secretKey, 'region' => $this->_region));
             } else {
                 $this->_sqs = Aws\Sqs\SqsClient::factory(array('region' => $this->_region, 'version' => '2012-11-05', 'credentials' => array('key' => $this->_accessKey, 'secret' => $this->_secretKey)));
             }
         }
     }
     return $this->_sqs;
 }
Пример #3
0
 /**
  * @param string $name
  * @return Aws
  */
 public function __get($name)
 {
     switch ($name) {
         case 'sqs':
             if (empty($this->sqs_client)) {
                 $this->sqs_client = Aws\Sqs\SqsClient::factory(array('key' => $this->CI->config->item('sqs_access_key_id'), 'secret' => $this->CI->config->item('sqs_secret_key'), 'region' => $this->CI->config->item('aws_region')));
             }
             $this->client = $this->sqs_client;
             break;
         case 's3':
             if (empty($this->s3_client)) {
                 $this->s3_client = Aws\S3\S3Client::factory(array('key' => $this->CI->config->item('s3_access_key_id'), 'secret' => $this->CI->config->item('s3_secret_key'), 'region' => $this->CI->config->item('aws_region')));
             }
             $this->client = $this->s3_client;
             break;
         default:
             break;
     }
     return $this;
 }