selectObjectContent.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. require dirname(__FILE__) . '/../vendor/autoload.php';
  3. $secretId = "COS_SECRETID"; //"云 API 密钥 SecretId";
  4. $secretKey = "COS_SECRETKEY"; //"云 API 密钥 SecretKey";
  5. $region = "ap-beijing"; //设置一个默认的存储桶地域
  6. $cosClient = new Qcloud\Cos\Client(array(
  7. 'region' => $region,
  8. 'schema' => 'https', //协议头部,默认为http
  9. 'credentials'=> array(
  10. 'secretId' => $secretId ,
  11. 'secretKey' => $secretKey
  12. )
  13. ));
  14. try {
  15. $result = $cosClient->selectObjectContent(array(
  16. 'Bucket' => $bucket, //格式:BucketName-APPID
  17. 'Key' => $key,
  18. 'Expression' => 'Select * from COSObject s',
  19. 'ExpressionType' => 'SQL',
  20. 'InputSerialization' => array(
  21. 'CompressionType' => 'None',
  22. 'CSV' => array(
  23. 'FileHeaderInfo' => 'NONE',
  24. 'RecordDelimiter' => '\n',
  25. 'FieldDelimiter' => ',',
  26. 'QuoteEscapeCharacter' => '"',
  27. 'Comments' => '#',
  28. 'AllowQuotedRecordDelimiter' => 'FALSE'
  29. )
  30. ),
  31. 'OutputSerialization' => array(
  32. 'CSV' => array(
  33. 'QuoteField' => 'ASNEEDED',
  34. 'RecordDelimiter' => '\n',
  35. 'FieldDelimiter' => ',',
  36. 'QuoteCharacter' => '"',
  37. 'QuoteEscapeCharacter' => '"'
  38. )
  39. ),
  40. 'RequestProgress' => array(
  41. 'Enabled' => 'FALSE'
  42. )
  43. ));
  44. // 请求成功
  45. foreach ($result['Data'] as $data) {
  46. // 迭代遍历select结果
  47. print_r($data);
  48. }
  49. } catch (\Exception $e) {
  50. // 请求失败
  51. echo($e);
  52. }