upload.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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(
  7. array(
  8. 'region' => $region,
  9. 'schema' => 'https', //协议头部,默认为http
  10. 'credentials'=> array(
  11. 'secretId' => $secretId ,
  12. 'secretKey' => $secretKey)));
  13. $local_path = "/data/exampleobject";
  14. try {
  15. $result = $cosClient->upload(
  16. $bucket = 'examplebucket-125000000', //格式:BucketName-APPID
  17. $key = 'exampleobject',
  18. $body = fopen($local_path, 'rb')
  19. /*
  20. $options = array(
  21. 'ACL' => 'string',
  22. 'CacheControl' => 'string',
  23. 'ContentDisposition' => 'string',
  24. 'ContentEncoding' => 'string',
  25. 'ContentLanguage' => 'string',
  26. 'ContentLength' => integer,
  27. 'ContentType' => 'string',
  28. 'Expires' => 'string',
  29. 'GrantFullControl' => 'string',
  30. 'GrantRead' => 'string',
  31. 'GrantWrite' => 'string',
  32. 'Metadata' => array(
  33. 'string' => 'string',
  34. ),
  35. 'ContentMD5' => 'string',
  36. 'ServerSideEncryption' => 'string',
  37. 'StorageClass' => 'string'
  38. )
  39. */
  40. );
  41. // 请求成功
  42. print_r($result);
  43. } catch (\Exception $e) {
  44. // 请求失败
  45. echo($e);
  46. }