开发者社区> 问答> 正文

如何使用智能云相册的PHP SDK完成照片上传

下面代码演示如何使用智能云相册的PHP SDK完成照片上传、获取照片列表、获取照片缩略图地址和删除照片等基本功能。
示例代码和SDK的目录结构如下: |____aliyun-oss-php-sdk-2.2.4/
|____aliyun-php-sdk-cloudphoto/
|____aliyun-php-sdk-core/
|____cloudphoto-phpdemo.php


cloudphoto-phpdemo.php的内容如下,请参考代码中的注释填写相关配置。 <?php
// 引入智能云相册的SDK
include_once 'aliyun-php-sdk-core/Config.php';
Autoloader::addAutoloadPath("aliyun-php-sdk-cloudphoto");
// 引入OSS的SDK
require_once 'aliyun-oss-php-sdk-2.2.4/autoload.php';
use CloudPhoto\Request\V20170711 as CloudPhoto;
// TODO - 修改这里的storeName, libraryID, 本地待上传文件路和AK
$storeName = "";
$libraryId = "";
$localFile = ""; // 本地待上传的文件
// 具有访问智能云相册权限的AK,可以是主账号的AK,也可以是授权后的子账号AK。
$accessKeyId = "";
$accessKeySecret = "";
// 请确认上述变量都已初始化。
// 创建客户端对象
$profile = DefaultProfile::getProfile("cn-shanghai", $accessKeyId, $accessKeySecret);
$cloudPhotoClient = new DefaultAcsClient($profile);
// 演示1:上传文件
// 1.1 开启上传事务,获取和OSS上传相关的信息
$createTransactionRequest = new CloudPhoto\CreateTransactionRequest();
$createTransactionRequest->setProtocol("https");
$createTransactionRequest->setStoreName($storeName);
$createTransactionRequest->setLibraryId($libraryId);
$createTransactionRequest->setMd5(md5_file($localFile));
$createTransactionRequest->setSize(filesize($localFile));
$createTransactionRequest->setExt("jpg");
$createTransactionResponse = $cloudPhotoClient->getAcsResponse($createTransactionRequest);
print_r($createTransactionResponse);
$sessionId = $createTransactionResponse->Transaction->Upload->SessionId;
$ossAccessKeyId = $createTransactionResponse->Transaction->Upload->AccessKeyId;
$ossAccessKeySecret = $createTransactionResponse->Transaction->Upload->AccessKeySecret;
$ossStsToken = $createTransactionResponse->Transaction->Upload->StsToken;
$bucket = $createTransactionResponse->Transaction->Upload->Bucket;
$objectKey = $createTransactionResponse->Transaction->Upload->ObjectKey;
$endPoint = $createTransactionResponse->Transaction->Upload->OssEndpoint;
$fileId = $createTransactionResponse->Transaction->Upload->FileId;
// 1.2 上传文件到OSS
$ossClient = new \OSS\OssClient($ossAccessKeyId, $ossAccessKeySecret, $endPoint, false, $ossStsToken);
$ossClient->uploadFile($bucket, $objectKey, $localFile);
// 1.3 创建照片对象
$createPhotoRequest = new CloudPhoto\CreatePhotoRequest();
$createPhotoRequest->setProtocol("https");
$createPhotoRequest->setStoreName($storeName);
$createPhotoRequest->setLibraryId($libraryId);
$createPhotoRequest->setFileId($fileId);
$createPhotoRequest->setSessionId($sessionId);
$createPhotoResponse = $cloudPhotoClient->getAcsResponse($createPhotoRequest);
print_r($createPhotoResponse);
// photoId是个64bit的数字,可能很大,将其转为字符串。
$photoId = number_format($createPhotoResponse->Photo->Id, 0, '', '');
print("photo id = " . $photoId)  . PHP_EOL;
// 演示2:获取照片列表
$listPhotoRequest = new CloudPhoto\ListPhotosRequest();
$listPhotoRequest->setProtocol("https");
$listPhotoRequest->setStoreName($storeName);
$listPhotoRequest->setLibraryId($libraryId);
$listPhotoRequest->setCursor(0);
$listPhotoRequest->setDirection("forward");
$listPhotoRequest->setSize(20);
$listPhotoRequest->setState("active");
$listPhotoResponse = $cloudPhotoClient->getAcsResponse($listPhotoRequest);
print_r($listPhotoResponse);
// 演示3:获取照片缩略图地址
$getThumbnailRequest = new CloudPhoto\GetThumbnailRequest();
$getThumbnailRequest->setProtocol("https");
$getThumbnailRequest->setStoreName($storeName);
$getThumbnailRequest->setLibraryId($libraryId);
$getThumbnailRequest->setPhotoId($photoId);
$getThumbnailRequest->setZoomType("image/resize,h_100"); // 缩放格式参考:https://help.aliyun.com/document_detail/44688.html
$getThumbnailResponse = $cloudPhotoClient->getAcsResponse($getThumbnailRequest);
print_r($getThumbnailResponse);
// 演示4:删除照片
$deletePhotoRequest = new CloudPhoto\DeletePhotosRequest();
$deletePhotoRequest->setProtocol("https");
$deletePhotoRequest->setStoreName($storeName);
$deletePhotoRequest->setLibraryId($libraryId);
$deletePhotoRequest->setPhotoIds(array($photoId));
$deletePhotoResponse = $cloudPhotoClient->getAcsResponse($deletePhotoRequest);
print_r($deletePhotoResponse);
?>

展开
收起
云栖大讲堂 2017-10-27 13:13:06 2073 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
PHP安全开发_从白帽角度做安全 立即下载
PHP在机器学习上的应用及云深度学习平台的架构设计与实现 立即下载
一个跨平台的云服务SDK需要什么 立即下载