2017-05-11 12:03:31 +08:00

71 lines
1.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace OSS\Tests;
require_once __DIR__ . '/../../../autoload.php';
use OSS\OssClient;
use OSS\Core\OssException;
/**
* Class Common
*
* 示例程序【Samples/*.php】 的Common类用于获取OssClient实例和其他公用方法
*/
class Common
{
/**
* 根据Config配置得到一个OssClient实例
*
* @return OssClient 一个OssClient实例
*/
public static function getOssClient()
{
try {
$ossClient = new OssClient(
getenv('OSS_ACCESS_KEY_ID'),
getenv('OSS_ACCESS_KEY_SECRET'),
getenv('OSS_ENDPOINT'), false);
} catch (OssException $e) {
printf(__FUNCTION__ . "creating OssClient instance: FAILED\n");
printf($e->getMessage() . "\n");
return null;
}
return $ossClient;
}
public static function getBucketName()
{
return getenv('OSS_BUCKET');
}
/**
* 工具方法创建一个bucket
*/
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) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
print(__FUNCTION__ . ": OK" . "\n");
}
/**
* Wait for bucket meta sync
*/
public static function waitMetaSync()
{
if (getenv('TRAVIS')) {
sleep(10);
}
}
}