需要一个完整的使用php操作DynamoDB的示例

17
我想在PHP中编写一个服务,其中:
1)DynamoDB将拥有表t,该表将有两个列key和val。
2)我将检查表t中是否存在某些键。
3)如果存在,则读取数据。如果不存在,则在表t中插入新的键值对。
我正在查看一些链接: http://docs.aws.amazon.com/AWSSDKforPHP/latest/index.html#m=AmazonDynamoDB/put_item http://docs.aws.amazon.com/aws-sdk-php/guide/latest/quick-start.html 应该遵循哪个链接?
还能否给我一个快速示例和确切语法?
提前致谢。

6
您需要展示一些工作,并询问关于特定的问题。这个问题和“为我编写代码”是相同的,如果您支付了我的日费用,我可能会这样做! - simon at rcl
第一个链接已经过时,请使用第二个链接。您可以从AWS SDK for PHP用户指南中找到使用AWS SDK所需的一切,并且那里还有指向DynamoDB最新API文档的链接。 - Jeremy Lindblom
这个答案可能会对你有所帮助:[https://dev59.com/p6v2oIgBc1ULPQZFTDv7#74761407](AWS dynamodb query from php)。 - Arpit Jain
3个回答

55
完整的步骤说明在此处,它为您提供了设置凭证的逐步大纲,并配有易于使用的附加功能,可添加到AWS PHP SDK中。
  • 1. Go to AWS and get your PUBLIC_KEY and PRIVATE_KEY

    • AWS does have tutorials for this HERE and HERE
  • 2. Open Terminal

  • 3. If you haven't created your credentials yet, in Terminal's fresh page, type in:

    nano ~/.aws/credentials
    
    • The nano function page will open up. You will see GNU nano 2.0.6... at the top.
  • 4. Inside the nano page, type in:

    [default]
    aws_access_key_id = public_key_ABCDEFGHIJKLMNOPQRSTUVWXYZ
    aws_secret_access_key = private_key_s0m3_CR42Y_l3tt3rS_i5y0ur53cr3tK3y
    
  • 5. Once you've typed it out, hit CONTROL + X (Yes...Control, not Command).

  • 6. Hit Y then ENTER
  • 7. Get the [AWS_SDK_PHP][4]
  • 8. Go to Your Elastic Beanstalk
  • 9. When you are done creating your app and you see the Overview screen with the green check. Look to the side and hit Configuration.
  • 10. Under Software Configuration -> Document root: type in: /
  • 11. Under Property Name -> AWS_ACCESS_KEY_ID type in: [your_access_key]
  • 12. Under AWS_ACCESS_KEY_ID is the AWS_SECRET_KEY, type in: [your_secret_key]
  • 13. When your PHP Project is ready. Put all your files into one folder. Name the folder [whatever], then compress the files inside [whatever]. Do not compress the whole folder. Only compress the files in the folder. If one of these files includes your index.php or index.html, your project will show up on EBS default URL.
  • 14. Your project should be called Archive.zip (Mac). Go to EBS, upload the zip and there you go! All finished with AWS Setup!
AWS设置
  • 1. Put the AWS_SDK_PHP in an empty folder
  • 2. At the top of the file using the SDK (index.php or whatever), put in:

    require 'aws/aws-autoloader.php';
    date_default_timezone_set('America/New_York');
    
    use Aws\DynamoDb\DynamoDbClient;
    
    $client = new DynamoDbClient([
        'profile' => 'default',
        'region'  => 'us-east-1',
        'version' => 'latest'
    ]);
    
  • 数据类型
    • S = 字符串
    • N = 数字
    • B = 二进制
  • The Basic Methods

    • Describe Table

      $result = $client->describeTable(array(
          'TableName' => '[Table_Name]'
      ));
      
      echo $result;
      
    • Put Item

      $response = $client->putItem(array(
          'TableName' => '[Table_Name]', 
          'Item' => array(
              '[Hash_Name]'   => array('S' => '[Hash_Value]'),
              '[Range_Name]'  => array('S' => '[Range_Value]')
          )
      ));
      
      //Echoing the response is only good to check if it was successful. Status: 200 = Success
      echo $response;
      
    • Get Item

      $response = $client->getItem(array(
          'TableName' => '[Table_Name]',
          'Key' => array(
              '[Hash_Name]'  => array('S' => '[Hash_Value]'),
              '[Range_Name]' => array('S' => '[Range_Value]')
          )
      ));
      
      echo $response;
      
    • Delete Item

      $response = $client->deleteItem(array(
          'TableName' => '[Table_Name]',
          'Key' => array(
              '[Hash_Name]'  => array('S' => '[Hash_Value]'),
              '[Range_Name]' => array('S' => '[Range_Value]')
          )
      ));
      //Echoing the response is only good to check if it was successful. Status: 200 = Success
      echo $response;
      
    • Query Item

      $response = $client->query(array(
          'TableName' => '[Table_Name]',
          'KeyConditionExpression' => '[Hash_Name] = :v_hash and [Range_Name] = :v_range',
          'ExpressionAttributeValues' =>  array (
              ':v_hash'  => array('S' => '[Hash_Value]'),
              ':v_range' => array('S' => '[Range_Value]')
          )
      ));
      
      echo $response;
      
希望这能有所帮助。

请问您能否添加一个扫描示例? - Oscar Gallardo
有没有一个示例可以插入一组值,例如(1,2,3,4)? - Itay Moav -Malimovka
要是AWS的文档能这么简单明了就好了,谢谢! - schmoove

3

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接