Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
192 lines (159 sloc) 4.32 KB

AWS_SDK_V3移行手順

GuzzleHttp対応

AWS SDK V2 → V3に設定してcomposer updateするとGuzzle 3.9.3 → GuzzleHttp 6.3.3になる。

AWS SDK V3にするならGuzzleHttpの対応も同時に必要。

参考:https://github.com/guzzle/guzzle/blob/master/UPGRADING.md

Guzzle\Http\Client → GuzzleHttp\Client

    $client = new Client($requestUri);

    $request = $client->get($requestUri, array(), array(
        'exceptions' => false,
        'timeout' => $this->timeout,
    ));

    try {
        $response = $request->send();
    } catch (CurlException $e) {
        return array('works' => array(), 'work_ids' => array());
    }

    if ($response->getStatusCode() < 300) {
        return $response->json();
    }

    $client = new Client([
        'timeout' => $this->timeout,
    ]);
    
    $response = null
    try {
        $response = $client->request('GET', $requestUri, array());
    } catch (RequestException $e) {
        return array('works' => array(), 'work_ids' => array());
    }
    
    if ($response->getStatusCode() < 300) {
        return json_decode($response->getBody()->getContents(), true);
    }

setDefoultOptionは削除されたので以下のように代替する

    $this->client = new Client();
    $http = $this->client->getClient();
    $http->setDefaultOption('config/curl/' . CURLOPT_TIMEOUT, 3); // 3s
    $http->setDefaultOption('config/curl/' . CURLOPT_CONNECTTIMEOUT, 1); // 1s

    $guzzleClient = new \GuzzleHttp\Client([
        'curl' => array(
            CURLOPT_TIMEOUT => 3,
            CURLOPT_CONNECTTIMEOUT => 1,
        ),
    ]);
    $this->client = new Client();
    $this->client->setClient($guzzleClient);
    $http = $this->client->getClient();

Guzzle\http\Url → 廃止

Guzzle\Plugin\Oauth\OauthPlugin → GuzzleHttp\Subscriber\Oauth\Oauth1

参考:https://github.com/guzzle/oauth-subscriber

composer.jsonに別途追加する必要がある

    "guzzlehttp/oauth-subscriber": "0.3.*",

自前で実装?

AWS SDK V3対応

Aws\Common\Enum\Region の削除

  • V3では廃止されている
    • 削除
use Aws\Common\Enum\Region;

リテラルにする

'region' => Region::AP_NORTHEAST_1,

'region' => 'ap-northeast-1',

S3

  • newで初期化
  • versionを付与
  • key、secretはcredentialsで囲う
$client = S3Client::factory(array(
    'key'    => Configure::read('AWS.AccessKey'),
    'secret' => Configure::read('AWS.SecretKey'),
    'region' => Region::AP_NORTHEAST_1,
));

$client = new S3Client([
    'version' => 'latest',
    'credentials' => [
        'key'    => Configure::read('AWS.AccessKey'),
        'secret' => Configure::read('AWS.SecretKey'),
    ],
    'region' => 'ap-northeast-1',
]);

CloudSearch

  • newで初期化
  • versionを付与
  • key、secretはcredentialsで囲う
  • endpointはhttpsをつける
$client = new CloudSearchClient::factory(array(
    'key' => Configure::read('AWS.AccessKey'),
    'secret' => Configure::read('AWS.SecretKey'),
    'endpoint' => 'jobsearch-xxxxx.ap-northeast-1.cloudsearch.amazonaws.com', 
    'region' => Region::AP_NORTHEAST_1,
    'domain' => 'jobsearch', 
));

$client = new CloudSearchClient([
    'version' => 'latest',
    'credentials' => [
        'key'    => Configure::read('AWS.AccessKey'),
        'secret' => Configure::read('AWS.SecretKey'),
    ],
    'endpoint' => 'https://jobsearch-xxxxx.ap-northeast-1.cloudsearch.amazonaws.com', 
    'region' => 'ap-northeast-1',
    'domain' => 'jobsearch', 
]);

SQS

  • newで初期化
  • versionを付与
  • key、secretはcredentialsで囲う
$client = SQSClient::factory(array(
    'key'       => Configure::read('AWS.AccessKey'),
    'secret'    => Configure::read('AWS.SecretKey'),
    'region'    => Region::AP_NORTHEAST_1,
));

$client = new SQSClient([
    'version' => 'latest',
    'credentials' => [
        'key'    => Configure::read('AWS.AccessKey'),
        'secret' => Configure::read('AWS.SecretKey'),
    ],
    'region' => 'ap-northeast-1'
]);

receiveMessage後の処理。 getPathではなくgetで取得する。

$body = $message->getPath('Messages/*/Body');

$body = $message->get('Messages')['Body'];

※$bodyは配列になっているので注意。getPathの取得値と若干構造が違うかも。

You can’t perform that action at this time.