function printImage($func, $imageFile) { $array = getimagesize($imageFile); Common::println("{$func}, image width: " . $array[0]); Common::println("{$func}, image height: " . $array[1]); Common::println("{$func}, image type: " . ($array[2] === 2 ? 'jpg' : 'png')); Common::println("{$func}, image size: " . ceil(filesize($imageFile))); }
use OSS\Core\OssException; $bucket = Common::getBucketName(); $ossClient = Common::getOssClient(); if (is_null($ossClient)) { exit(1); } //*******************************简单使用*************************************************************** /** * 查看完整用法中的 "putObjectByRawApis"函数,查看使用基础的分片上传api进行文件上传,用户可以基于这个自行实现断点续传等功能 */ // 使用分片上传接口上传文件, 接口会根据文件大小决定是使用普通上传还是分片上传 $ossClient->multiuploadFile($bucket, "file.php", __FILE__, array()); Common::println("local file " . __FILE__ . " is uploaded to the bucket {$bucket}, file.php"); // 上传本地目录到bucket内的targetdir子目录中 $ossClient->uploadDir($bucket, "targetdir", __DIR__); Common::println("local dir " . __DIR__ . " is uploaded to the bucket {$bucket}, targetdir/"); // 列出当前未完成的分片上传 $listMultipartUploadInfo = $ossClient->listMultipartUploads($bucket, array()); //******************************* 完整用法参考下面函数 **************************************************** multiuploadFile($ossClient, $bucket); putObjectByRawApis($ossClient, $bucket); uploadDir($ossClient, $bucket); listMultipartUploads($ossClient, $bucket); /** * 通过multipart上传文件 * * @param OssClient $ossClient OssClient实例 * @param string $bucket 存储空间名称 * @return null */ function multiuploadFile($ossClient, $bucket)
} //******************************* 简单使用 **************************************************************** //设置referer白名单 $refererConfig = new RefererConfig(); $refererConfig->setAllowEmptyReferer(true); $refererConfig->addReferer("www.aliiyun.com"); $refererConfig->addReferer("www.aliiyuncs.com"); $ossClient->putBucketReferer($bucket, $refererConfig); Common::println("bucket {$bucket} refererConfig created:" . $refererConfig->serializeToXml()); //获取Referer白名单 $refererConfig = $ossClient->getBucketReferer($bucket); Common::println("bucket {$bucket} refererConfig fetched:" . $refererConfig->serializeToXml()); //删除referer白名单 $refererConfig = new RefererConfig(); $ossClient->putBucketReferer($bucket, $refererConfig); Common::println("bucket {$bucket} refererConfig deleted"); //******************************* 完整用法参考下面函数 **************************************************** putBucketReferer($ossClient, $bucket); getBucketReferer($ossClient, $bucket); deleteBucketReferer($ossClient, $bucket); getBucketReferer($ossClient, $bucket); /** * 设置bucket的防盗链配置 * * @param OssClient $ossClient OssClient实例 * @param string $bucket 存储空间名称 * @return null */ function putBucketReferer($ossClient, $bucket) { $refererConfig = new RefererConfig();
Common::println("ETag: " . $result[1]); // 判断object是否存在 $doesExist = $ossClient->doesObjectExist($bucket, "c.file.copy"); Common::println("file c.file.copy exist? " . ($doesExist ? "yes" : "no")); // 删除object $result = $ossClient->deleteObject($bucket, "c.file.copy"); Common::println("c.file.copy is deleted"); Common::println("b.file is created"); Common::println($result['x-oss-request-id']); // 判断object是否存在 $doesExist = $ossClient->doesObjectExist($bucket, "c.file.copy"); Common::println("file c.file.copy exist? " . ($doesExist ? "yes" : "no")); // 批量删除object $result = $ossClient->deleteObjects($bucket, array("b.file", "c.file")); foreach ($result as $object) { Common::println($object); } sleep(2); unlink("c.file.localcopy"); //******************************* 完整用法参考下面函数 **************************************************** listObjects($ossClient, $bucket); listAllObjects($ossClient, $bucket); createObjectDir($ossClient, $bucket); putObject($ossClient, $bucket); uploadFile($ossClient, $bucket); getObject($ossClient, $bucket); getObjectToLocalFile($ossClient, $bucket); copyObject($ossClient, $bucket); modifyMetaForObject($ossClient, $bucket); getObjectMeta($ossClient, $bucket); deleteObject($ossClient, $bucket);
} //******************************* 简单使用 ******************************************************* //设置lifecycle规则 $lifecycleConfig = new LifecycleConfig(); $actions = array(); $actions[] = new LifecycleAction("Expiration", "Days", 3); $lifecycleRule = new LifecycleRule("delete obsoleted files", "obsoleted/", "Enabled", $actions); $lifecycleConfig->addRule($lifecycleRule); $ossClient->putBucketLifecycle($bucket, $lifecycleConfig); Common::println("bucket {$bucket} lifecycleConfig created:" . $lifecycleConfig->serializeToXml()); //获取lifecycle规则 $lifecycleConfig = $ossClient->getBucketLifecycle($bucket); Common::println("bucket {$bucket} lifecycleConfig fetched:" . $lifecycleConfig->serializeToXml()); //删除bucket的lifecycle配置 $ossClient->deleteBucketLifecycle($bucket); Common::println("bucket {$bucket} lifecycleConfig deleted"); //***************************** 完整用法参考下面函数 *********************************************** putBucketLifecycle($ossClient, $bucket); getBucketLifecycle($ossClient, $bucket); deleteBucketLifecycle($ossClient, $bucket); getBucketLifecycle($ossClient, $bucket); /** * 设置bucket的生命周期配置 * * @param OssClient $ossClient OssClient实例 * @param string $bucket 存储空间名称 * @return null */ function putBucketLifecycle($ossClient, $bucket) { $lifecycleConfig = new LifecycleConfig();
$bucket = Common::getBucketName(); //******************************* 简单使用 **************************************************************** //创建bucket $ossClient->createBucket($bucket, OssClient::OSS_ACL_TYPE_PUBLIC_READ_WRITE); Common::println("bucket {$bucket} created"); // 判断Bucket是否存在 $doesExist = $ossClient->doesBucketExist($bucket); Common::println("bucket {$bucket} exist? " . ($doesExist ? "yes" : "no")); // 获取Bucket列表 $bucketListInfo = $ossClient->listBuckets(); // 设置bucket的ACL $ossClient->putBucketAcl($bucket, OssClient::OSS_ACL_TYPE_PUBLIC_READ_WRITE); Common::println("bucket {$bucket} acl put"); // 获取bucket的ACL $acl = $ossClient->getBucketAcl($bucket); Common::println("bucket {$bucket} acl get: " . $acl); //******************************* 完整用法参考下面函数 **************************************************** createBucket($ossClient, $bucket); doesBucketExist($ossClient, $bucket); deleteBucket($ossClient, $bucket); putBucketAcl($ossClient, $bucket); getBucketAcl($ossClient, $bucket); listBuckets($ossClient); /** * 创建一个存储空间 * acl 指的是bucket的访问控制权限,有三种,私有读写,公共读私有写,公共读写。 * 私有读写就是只有bucket的拥有者或授权用户才有权限操作 * 三种权限分别对应 (OssClient::OSS_ACL_TYPE_PRIVATE,OssClient::OSS_ACL_TYPE_PUBLIC_READ, OssClient::OSS_ACL_TYPE_PUBLIC_READ_WRITE) * * @param OssClient $ossClient OssClient实例 * @param string $bucket 要创建的存储空间名称
Common::println("b.file is fetched to the local file: c.file.localcopy"); // 拷贝object $ossClient->copyObject($bucket, "c.file", $bucket, "c.file.copy"); Common::println("c.file is copied to c.file.copy"); // 判断object是否存在 $doesExist = $ossClient->doesObjectExist($bucket, "c.file.copy"); Common::println("file c.file.copy exist? " . ($doesExist ? "yes" : "no")); // 删除object $ossClient->deleteObject($bucket, "c.file.copy"); Common::println("c.file.copy is deleted"); // 判断object是否存在 $doesExist = $ossClient->doesObjectExist($bucket, "c.file.copy"); Common::println("file c.file.copy exist? " . ($doesExist ? "yes" : "no")); // 批量删除object $ossClient->deleteObjects($bucket, array("b.file", "c.file")); Common::println("b.file, c.file are deleted"); sleep(2); unlink("c.file.localcopy"); //******************************* 完整用法参考下面函数 **************************************************** listObjects($ossClient, $bucket); listAllObjects($ossClient, $bucket); createObjectDir($ossClient, $bucket); putObject($ossClient, $bucket); uploadFile($ossClient, $bucket); getObject($ossClient, $bucket); getObjectToLocalFile($ossClient, $bucket); copyObject($ossClient, $bucket); modifyMetaForObject($ossClient, $bucket); getObjectMeta($ossClient, $bucket); deleteObject($ossClient, $bucket); deleteObjects($ossClient, $bucket);
$bucket = Common::getBucketName(); $ossClient = Common::getOssClient(); if (is_null($ossClient)) { exit(1); } //*******************************简单使用*************************************************************** // 设置Bucket的静态网站托管模式 $websiteConfig = new WebsiteConfig("index.html", "error.html"); $ossClient->putBucketWebsite($bucket, $websiteConfig); Common::println("bucket {$bucket} websiteConfig created:" . $websiteConfig->serializeToXml()); // 查看Bucket的静态网站托管状态 $websiteConfig = $ossClient->getBucketWebsite($bucket); Common::println("bucket {$bucket} websiteConfig fetched:" . $websiteConfig->serializeToXml()); // 删除Bucket的静态网站托管模式 $ossClient->deleteBucketWebsite($bucket); Common::println("bucket {$bucket} websiteConfig deleted"); //******************************* 完整用法参考下面函数 **************************************************** putBucketWebsite($ossClient, $bucket); getBucketWebsite($ossClient, $bucket); deleteBucketWebsite($ossClient, $bucket); getBucketWebsite($ossClient, $bucket); /** * 设置bucket的静态网站托管模式配置 * * @param $ossClient OssClient * @param $bucket string 存储空间名称 * @return null */ function putBucketWebsite($ossClient, $bucket) { $websiteConfig = new WebsiteConfig("index.html", "error.html");
// 设置cors配置 $corsConfig = new CorsConfig(); $rule = new CorsRule(); $rule->addAllowedHeader("x-oss-header"); $rule->addAllowedOrigin("http://www.b.com"); $rule->addAllowedMethod("POST"); $rule->setMaxAgeSeconds(10); $corsConfig->addRule($rule); $ossClient->putBucketCors($bucket, $corsConfig); Common::println("bucket {$bucket} corsConfig created:" . $corsConfig->serializeToXml()); // 获取cors配置 $corsConfig = $ossClient->getBucketCors($bucket); Common::println("bucket {$bucket} corsConfig fetched:" . $corsConfig->serializeToXml()); // 删除cors配置 $ossClient->deleteBucketCors($bucket); Common::println("bucket {$bucket} corsConfig deleted"); //******************************* 完整用法参考下面函数 ***************************************************** putBucketCors($ossClient, $bucket); getBucketCors($ossClient, $bucket); deleteBucketCors($ossClient, $bucket); getBucketCors($ossClient, $bucket); /** * 设置bucket的cors配置 * * @param OssClient $ossClient OssClient实例 * @param string $bucket 存储空间名称 * @return null */ function putBucketCors($ossClient, $bucket) { $corsConfig = new CorsConfig();
$copiedObject = "multipart-callback-test.txt.copied"; $ossClient->putObject($bucket, $copiedObject, file_get_contents(__FILE__)); /** * step 1. 初始化一个分块上传事件, 也就是初始化上传Multipart, 获取upload id */ $upload_id = $ossClient->initiateMultipartUpload($bucket, $object); /** * step 2. uploadPartCopy */ $copyId = 1; $eTag = $ossClient->uploadPartCopy($bucket, $copiedObject, $bucket, $object, $copyId, $upload_id); $upload_parts[] = array('PartNumber' => $copyId, 'ETag' => $eTag); $listPartsInfo = $ossClient->listParts($bucket, $object, $upload_id); /** * step 3. */ $json = '{ "callbackUrl":"callback.oss-demo.com:23450", "callbackHost":"oss-cn-hangzhou.aliyuncs.com", "callbackBody":"{\\"mimeType\\":${mimeType},\\"size\\":${size},\\"x:var1\\":${x:var1},\\"x:var2\\":${x:var2}}", "callbackBodyType":"application/json" }'; $var = '{ "x:var1":"value1", "x:var2":"值2" }'; $options = array(OssClient::OSS_CALLBACK => $json, OssClient::OSS_CALLBACK_VAR => $var); $result = $ossClient->completeMultipartUpload($bucket, $object, $upload_id, $upload_parts, $options); Common::println($result['body']); Common::println($result['info']['http_code']);
$bucket = Common::getBucketName(); $ossClient = Common::getOssClient(); if (is_null($ossClient)) { exit(1); } //******************************* 简单使用 *************************************************************** $ossClient->uploadFile($bucket, "a.file", __FILE__); // 生成GetObject的签名url,用户可以使用这个url直接在浏览器下载 $signedUrl = $ossClient->signUrl($bucket, "a.file", 3600); Common::println($signedUrl); // 生成用于putObject的签名URL,用户可以直接用put方法使用这个url上传文件到 "a.file" $signedUrl = $ossClient->signUrl($bucket, "a.file", "3600", "PUT"); Common::println($signedUrl); // 生成从本地文件上传PutObject的签名url, 用户可以直接使用这个url把本地文件上传到 "a.file" $signedUrl = $ossClient->signUrl($bucket, "a.file", 3600, "PUT", array('Content-Type' => 'txt')); Common::println($signedUrl); //******************************* 完整用法参考下面函数 **************************************************** getSignedUrlForPuttingObject($ossClient, $bucket); getSignedUrlForPuttingObjectFromFile($ossClient, $bucket); getSignedUrlForGettingObject($ossClient, $bucket); /** * 生成GetObject的签名url,主要用于私有权限下的读访问控制 * * @param $ossClient OssClient OssClient实例 * @param $bucket string 存储空间名称 * @return null */ function getSignedUrlForGettingObject($ossClient, $bucket) { $object = "test/test-signature-test-upload-and-download.txt"; $timeout = 3600;
创建好直播频道之后调用getLiveChannelInfo可以得到频道相关的信息 */ $info = $ossClient->getLiveChannelInfo($bucket, 'test_rtmp_live'); Common::println("bucket {$bucket} LiveChannelInfo:\n" . "live channel info description: " . $info->getDescription() . "\n" . "live channel info status: " . $info->getStatus() . "\n" . "live channel info type: " . $info->getType() . "\n" . "live channel info fragDuration: " . $info->getFragDuration() . "\n" . "live channel info fragCount: " . $info->getFragCount() . "\n" . "live channel info playListName: " . $info->getPlayListName() . "\n"); /** 如果想查看一个频道历史推流记录,可以调用getLiveChannelHistory。目前最多可以看到10次推流的记录 */ $history = $ossClient->getLiveChannelHistory($bucket, "test_rtmp_live"); if (count($history->getLiveRecordList()) != 0) { foreach ($history->getLiveRecordList() as $recordList) { Common::println("bucket {$bucket} liveChannelHistory:\n" . "live channel history startTime: " . $recordList->getStartTime() . "\n" . "live channel history endTime: " . $recordList->getEndTime() . "\n" . "live channel history remoteAddr: " . $recordList->getRemoteAddr() . "\n"); } } /** 对于正在推流的频道调用get_live_channel_stat可以获得流的状态信息。 如果频道正在推流,那么stat_result中的所有字段都有意义。 如果频道闲置或者处于“Disabled”状态,那么status为“Idle”或“Disabled”,其他字段无意义。 */ $status = $ossClient->getLiveChannelStatus($bucket, "test_rtmp_live"); Common::println("bucket {$bucket} listLiveChannel:\n" . "live channel status status: " . $status->getStatus() . "\n" . "live channel status ConnectedTime: " . $status->getConnectedTime() . "\n" . "live channel status VideoWidth: " . $status->getVideoWidth() . "\n" . "live channel status VideoHeight: " . $status->getVideoHeight() . "\n" . "live channel status VideoFrameRate: " . $status->getVideoFrameRate() . "\n" . "live channel status VideoBandwidth: " . $status->getVideoBandwidth() . "\n" . "live channel status VideoCodec: " . $status->getVideoCodec() . "\n" . "live channel status AudioBandwidth: " . $status->getAudioBandwidth() . "\n" . "live channel status AudioSampleRate: " . $status->getAudioSampleRate() . "\n" . "live channel status AdioCodec: " . $status->getAudioCodec() . "\n"); /** * 如果希望利用直播推流产生的ts文件生成一个点播列表,可以使用postVodPlaylist方法。 * 指定起始时间为当前时间减去60秒,结束时间为当前时间,这意味着将生成一个长度为60秒的点播视频。 * 播放列表指定为“vod_playlist.m3u8”,也就是说这个接口调用成功之后会在OSS上生成一个名叫“vod_playlist.m3u8”的播放列表文件。 */ $current_time = time(); $ossClient->postVodPlaylist($bucket, "test_rtmp_live", "vod_playlist.m3u8", array('StartTime' => $current_time - 60, 'EndTime' => $current_time)); /** * 如果一个直播频道已经不打算再使用了,那么可以调用delete_live_channel来删除频道。 */ $ossClient->deleteBucketLiveChannel($bucket, "test_rtmp_live");