No Description

upload_with_zone.php 1.0KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. require_once __DIR__ . '/../autoload.php';
  3. use Qiniu\Auth;
  4. use Qiniu\Zone;
  5. use Qiniu\Config;
  6. use Qiniu\Storage\UploadManager;
  7. // 控制台获取密钥:https://portal.qiniu.com/user/key
  8. $accessKey = getenv('QINIU_ACCESS_KEY');
  9. $secretKey = getenv('QINIU_SECRET_KEY');
  10. $auth = new Auth($accessKey, $secretKey);
  11. // 上传到七牛存储后文件所在的空间和文件名
  12. $bucket = getenv('QINIU_TEST_BUCKET');
  13. $key = 'php-logo.png';
  14. $uptoken = $auth->uploadToken($bucket);
  15. // 上传文件的本地路径
  16. $filePath = './php-logo.png';
  17. // 指定 zone 上传
  18. // 参考文档:https://developer.qiniu.com/kodo/manual/1671/region-endpoint
  19. $zone = Zone::zonez0(); // 华东:z0,华北:z1,华南:z2,北美:na0,东南亚:as0
  20. $config = new Config($zone);
  21. $config->useHTTPS = true;
  22. // 指定 config
  23. $uploadMgr = new UploadManager($config);
  24. list($ret, $err) = $uploadMgr->putFile($uptoken, $key, $filePath);
  25. echo "\n====> putFile result: \n";
  26. if ($err !== null) {
  27. var_dump($err);
  28. } else {
  29. var_dump($ret);
  30. }