<?php
/**
 * 阿里oss管理
 * User: wsc
 * Date: 2019/4/10
 * Time: 16:51
 */
use OSS\OssClient;
use OSS\Core\OssException;
class OssCommon
{
    const endpoint = "oss-us-east-1-internal.aliyuncs.com";
  const accessKeyId = "LTAI4G6in86ShrwQDAvGgatS";
  const accessKeySecret = "Q8Nz6U5fxMfbYGNl1zR3EpPEvQWNOy";
  const bucket = "crazycoin-out";
  /**
   * Get an OSSClient instance according to config.
   *
   * @return OssClient An OssClient instance
   */
  public static function getOssClient($tempEndpoint = "")
  {
      if(empty($tempEndpoint)){
          $tempEndpoint = self::endpoint;
      }

    try {
      $ossClient = new OssClient(self::accessKeyId, self::accessKeySecret, $tempEndpoint, false);
    } catch (OssException $e) {
      printf(__FUNCTION__ . "creating OssClient instance: FAILED\n");
      printf($e->getMessage() . "\n");
      return null;
    }
    return $ossClient;
  }

  public static function getBucketName()
  {
      return self::bucket;
  }

  /**
   * A tool function which creates a bucket and exists the process if there are exceptions
   */
  public static function createBucket()
  {
    $ossClient = self::getOssClient();
    if (is_null($ossClient)) exit(1);
    $bucket = self::getBucketName();
    $acl = OssClient::OSS_ACL_TYPE_PUBLIC_READ;
    try {
      $ossClient->createBucket($bucket, $acl);
    } catch (OssException $e) {

      $message = $e->getMessage();
      if (\OSS\Core\OssUtil::startsWith($message, 'http status: 403')) {
        echo "Please Check your AccessKeyId and AccessKeySecret" . "\n";
        exit(0);
      } elseif (strpos($message, "BucketAlreadyExists") !== false) {
        echo "Bucket already exists. Please check whether the bucket belongs to you, or it was visited with correct endpoint. " . "\n";
        exit(0);
      }
      printf(__FUNCTION__ . ": FAILED\n");
      printf($e->getMessage() . "\n");
      return;
    }
    print(__FUNCTION__ . ": OK" . "\n");
  }

  public static function println($message)
  {
    if (!empty($message)) {
      echo strval($message) . "\n";
    }
  }
}
