PHP 阿里雲oss sdk詳細介紹
阿新 • • 發佈:2019-02-03
不足的地方請大家不吝賜教。那我們就開始漫長的SDK使用之旅吧。
第一步 : 去OSS官方註冊賬戶,並且開通OSS服務,當OSS服務開通後,進入管理中心獲取相應的API金鑰。如下截圖:
第二步: 在獲得API金鑰後,獲取OSS PHP SDK,從官方下載壓縮包,解壓後,我們會看到一個conf.inc.php的檔案,把我們第一步獲得的API金鑰填入相應的位置,程式碼如下:
//個人測試
//ACCESS_ID
define('OSS_ACCESS_ID', ‘這裡填入ACCESS ID’);
//ACCESS_KEY
define('OSS_ACCESS_KEY', '這裡填入ACCESS KEY');
第三步:當完成以上兩步以後,我們就可以開始我們神奇的OSS PHP SDK使用之旅了,接下來我們會詳細介紹每個API的使用方法,並且提供詳細的測試程式碼以及文字說明
1. 載入SDK 並且初始化相關引數
/**
* 載入sdk包以及錯誤程式碼包
*/
require_once '../sdk.class.php';
//例項化OSS Service
$oss_sdk_service = new ALIOSS();
//設定是否開啟curl除錯模式,該模式主要是為了方便除錯使用,可以設定TRUE|FALSE
$oss_sdk_service->set_debug_mode(FALSE);
2. list_bucket 方法使用
/**
* 獲取bucket列表,該方法主要獲取該賬戶下所有的bucket列表
* 使用方法如下:
* $bucket_list = $oss_sdk_service->list_bucket();
*/
$bucket_list = $oss_sdk_service->list_bucket();
print_r($bucket_list);die();
3. get_bucket_acl方法使用
/**
* 獲得指定bucket的ACL
* 呼叫方法如下:
* $get_bucket_acl = $oss_sdk_service->get_bucket_acl($bucket,$options);
* 其中需要傳入的引數為 某一$bucket的名稱,$options為一個關聯陣列,該方法可以傳入Content-Type,使用系統內建的定義方式
*/
$bucket = 'php-sdk-1330835674';
$options = array(
ALIOSS::OSS_CONTENT_TYPE => 'text/xml',
);
$get_bucket_acl = $oss_sdk_service->get_bucket_acl($bucket,$options);
print_r($get_bucket_acl);die();
4. set_bucket_acl方法使用
/**
* 設定指定bucket的ACL,目前只有三種acl private,public-read,public-read-write
* 呼叫方法如下:
* $set_bucket_acl = $oss_sdk_service->set_bucket_acl($bucket, $acl);
* 其中需要指定$bucket,以及$acl的值,$acl的取值只能是private,public-read,public-read-write其中之一,$acl使用系統內建的定義方式
* 對應的值為 private => ALIOSS::OSS_ACL_TYPE_PRIVATE ,public-read=> ALIOSS::OSS_ACL_TYPE_PUBLIC_READ ,public-read-write=> ALIOSS::OSS_ACL_TYPE_PUBLIC_READ_WRITE
*/
$bucket = 'php-sdk-1329818657';
$acl = ALIOSS::OSS_ACL_TYPE_PUBLIC_READ;
$set_bucket_acl = $oss_sdk_service->set_bucket_acl($bucket, $acl);
print_r($set_bucket_acl);die();
5. create_bucket方法使用
/**
* 建立bucket
* 呼叫方法如下:
* $create_bucket = $oss_sdk_service->create_bucket($bucket, $acl);
* 其中需要傳入$bucket,$acl是可選的,如果不指定ACL,則預設會是private,
*$acl的取值只能是private,public-read,public-read-write其中之一,$acl使用系統內建的定義方式
* 對應的值為 private => ALIOSS::OSS_ACL_TYPE_PRIVATE ,public-read=> ALIOSS::OSS_ACL_TYPE_PUBLIC_READ ,public-read-write=> ALIOSS::OSS_ACL_TYPE_PUBLIC_READ_WRITE
*/
$bucket = 'oss-php-sdk-'.time();
$acl = ALIOSS::OSS_ACL_TYPE_PUBLIC_READ;
$create_bucket = $oss_sdk_service->create_bucket($bucket, $acl);
print_r($create_bucket);die();
6. delete_bucket方法使用
/**
* 刪除bucket
* 呼叫方法如下:
* $delete_bucket = $oss_sdk_service->delete_bucket($bucket);
* 需要傳入$bucket引數,其中$bucket內不能有內容,如果有內容則本次刪除請求不會成功
*/
$bucket = 'oss-php-sdk-1330928803';
$delete_bucket = $oss_sdk_service->delete_bucket($bucket);
print_r($delete_bucket);die();
7. list_object方法使用
/**
* 獲得bucket下的object列表
* 呼叫方法如下:
* $list_object = $oss_sdk_service->list_object($bucket,$options);
* 其中需要傳入$bucket引數,$options為可選引數,如果需要傳入$options,必須為陣列,其中資料的key必須為指定的,否則引數無效
* $options = array(
* 'max-keys' => max-keys用於限定此次返回object的最大數,如果不設定,預設為100,max-keys取值不能大於100。
* 'prefix' => 限定返回的object key必須以prefix作為字首。注意使用prefix查詢時,返回的key中仍會包含prefix。
* 'delimiter' => 是一個用於對Object名字進行分組的字元。所有名字包含指定的字首且第一次出現delimiter字元之間的object作為一組元素
* 'marker' => 使用者設定結果從marker之後按字母排序的第一個開始返回。
* )
* 其中 prefix,marker用來實現分頁顯示效果,引數的長度必須小於256位元組。
*/
$bucket = 'php-sdk-1330835674';
$options = array(
'delimiter' => '/',
'prefix' => '',
'max-keys' => 10,
'marker' => 'myobject-1330850469.pdf',
);
$list_object = $oss_sdk_service->list_object($bucket,$options);
print_r($list_object);die();
8. get_object方法使用
/**
* 獲得$bucket下的某個object,$object為檔案,不能為目錄
* 呼叫方法如下:
* $get_object = $oss_sdk_service->get_object($bucket,$object,$options);
* 其中需要傳入$bucket,$object引數,引數為必須,$options為可選引數,是一個key=>value格式的關聯陣列,其中的key定義如下
* $options = array(
* 'lastmodified' => 設定請求header 'If-Modified-Since'
* 'etag' => 設定請求header Etag
* 'range' => 設定請求header Range,主要用來實現斷點下載
* 'fileDownload' => 使用者寫入本地檔案,需要有寫許可權
* )
*/
$bucket = 'php-sdk-1329818657';
$object = 'myobject-pdf-1331429369.pdf';
$filepath = "D:\\download\\myobject-".time().".pdf";
$options = array(
'fileDownload' => $filepath,
);
$get_object_response = $oss_sdk_service->get_object($bucket,$object,$options);
print_r($get_object_response);die();
9. create_object_dir方法使用
/**
* 建立資料夾(是虛擬資料夾)
* 呼叫方法如下:
* $create_object_dir = $oss_sdk_service->create_object_dir($bucket,$object);
* 其中需要傳入$bucket,$object引數,引數為必須
*/
$bucket = 'efrwerwertyrty';
$object = '我的OSSbucket-'.time();
$create_object_dir = $oss_sdk_service->create_object_dir($bucket,$object);
print_r($create_object_dir);die();
10. upload_file_by_content方法使用
/**
* 通過http body上傳檔案,適用於直接寫入內容的上傳,比較小的檔案
* 呼叫方法如下:
* $upload_file_by_content = $oss_sdk_service->upload_file_by_content($bucket,$object,$upload_file_options);
* 其中的$bucket,$object以及$upload_file_options為必選引數,$upload_file_options必須為陣列,且key必須為規定的值,否則會上傳失敗,
* $object是檔名稱,如果上傳的檔案不是直接位於bucket下,而是位於某一子目錄下,則$object = 'dir_name/dir_name/file_name'
* 其中的content 為檔案的內容,$length為檔案的大小
* $upload_file_options = array(
* 'content' => $content,
* 'length' => $length,
* );
*/
$bucket = 'php-sdk-1328520184';
$object = 'oss-file-name-'.time().'.txt';
$content = '';
for($i = 0;$i<= 10000;$i ){
$content .= $i."\n";
}
$upload_file_options = array(
'content' => $content,
'length' => strlen($content),
);
$upload_file_by_content = $oss_sdk_service->upload_file_by_content($bucket,$object,$upload_file_options);
print_r($upload_file_by_content);die();
11. upload_file_by_file 方法使用
/**
* 通過檔案方式上傳,適合小檔案上傳,大檔案上傳請使用multipart
*/
$bucket = 'php-sdk-1328520898';
$file = "d:\\ccc.pdf"; 檔案路徑
$object = 'cpp.how.to.program-'.time().'.pdf'; //object名稱
$response_upload_file_by_file = $oss_sdk_service->upload_file_by_file($bucket,$object,$file);
print_r($response_upload_file_by_file);die();
12. copy_object 方法使用
/**
* Copy Object
* 呼叫方法如下:
* $copy_object_result = $oss_sdk_service->copy_object($from_bucket, $from_object,$to_bucket, $to_object)
* 其中需要傳入$from_bucket,$from_object,$to_bucket,$to_object等引數,均不能為空
*/
$from_bucket = 'aaaaaaaaaaa';
$to_bucket = 'efrwerwertyrty';
$from_object = 'CreateLaunchConfigurationRequest.cs';
$to_object = 'copy-object-'.time().'.cs';
$copy_object_result = $oss_sdk_service->copy_object($from_bucket, $from_object,$to_bucket, $to_object);
print_r($copy_object_result);die();
13. get_sign_url方法使用
/**
* 獲取帶簽名的外鏈URL
* 呼叫方法如下:
* $get_sign_url = $oss_sdk_service->get_sign_url($bucket,$object,$timeout)
* 其中需要傳入$bucket,$object不能為空,$timeout為過期的秒數,從當前時間往後多少秒過期,若不傳,則使用預設值60
*/
$bucket = 'php-sdk-1330835674';
$object = 'Cpp.How.to.Program.7-1330855122.pdf';
$timeout = 3600;
$get_sign_url = $oss_sdk_service->get_sign_url($bucket,$object,$timeout);
print_r($get_sign_url);die();
14. is_object_exist方法使用
/**
* 檢測Object是否存在
* 呼叫方法如下:
* $is_object_exist = $oss_sdk_service->is_object_exist($bucket, $object);
* 其中需要傳入$bucket,$object引數,引數為必須
*/
$bucket = 'php-sdk-1330835674';
$object = 'Cpp.How.to.Program.7-1330855122.pdf';
$is_object_exist = $oss_sdk_service->is_object_exist($bucket, $object);
var_dump($is_object_exist?'Exist':'Not Exist');die();
15 . get_object_url方法使用
/**
* 獲得某一個object的URL
* 呼叫方法如下:
* $get_object_url = $oss_sdk_service->get_object_url($bucket, $object);
* 其中需要傳入$bucket,$object引數,引數為必須,$object為檔案
*/
$bucket = 'php-sdk-1330835674';
$object = 'Cpp.How.to.Program.7-1330855122.pdf';
$get_object_url = $oss_sdk_service->get_object_url($bucket, $object);
var_dump($get_object_url?$get_object_url:'Not Exist');die();
16. get_object_meta方法使用
/**
* 獲得object的meta
* 呼叫方法如下:
* $get_object_meta = $oss_sdk_service->get_object_meta($bucket, $object);
* 其中需要傳入$bucket,$object引數,引數為必須,如果object為資料夾,則需要新增'/'
*/
$bucket = 'php-sdk-1330835674';
$object = 'Cpp.How.to.Program.7-1330855122.pdf';
$get_object_meta = $oss_sdk_service->get_object_meta($bucket, $object);
print_r($get_object_meta);die();
17. delete_object方法使用
/**
* 刪除object
* 呼叫方法如下:
* $delete_object = $oss_sdk_service->delete_object($bucket,$object);
* 其中需要傳入$bucket,$object引數,如果object為檔案,則會直接刪除,如果為資料夾,若資料夾不為空,則不能刪除
*/
$bucket = 'php-sdk-1330835674';
$object = 'Cpp.How.to.Program.7-1330855122.pdf';
$delete_object = $oss_sdk_service->delete_object($bucket,$object);
print_r($delete_object);die();
18. delete_objects方法使用
/**
* 批量刪除某一bucket下的objects
* 呼叫方法如下:
* $delete_objects_response = $oss_sdk_service->delete_objects($bucket,$objects,$options);
* 其中需要傳入$bucket,$object引數,如果objects為bucket下的object陣列
* $options 可以設定返回模式,目前提供兩種返回模式:詳細(verbose)模式和簡單(quiet)模式,預設為verbose模式,模式的設定如下
* $options = array('quite'=>true|false),true 為quiet模式,false為verbose模式
*/
$bucket = 'myossbucket002';
$objects = array(
'AliUpdate.dll','libGLESv2.dll'
);
$options = array(
'quiet' => true,
);
$delete_objects_response = $oss_sdk_service->delete_objects($bucket, $objects,$options);
print_r($delete_objects_response);die();
19. create_object_group方法使用
/**
* 建立object group
* 呼叫方法如下:
* $create_object_group = $oss_sdk_service->create_object_group($bucket,$object_group,$object_group_array);
* 其中引數$object_group,$bucket,$object_group_array不為空,且$object_group_array內的object必須位於同一bucket下
*/
$bucket = 'php-sdk-1330835674';
$object_group = 'object-group-'.time();
$object_group_array = array(
'Cpp.How.to.Program.7-1330855122.pdf',
'copy-object-1330929729.pdf',
'hh_axur-setup.pdf',
);
$create_object_group = $oss_sdk_service->create_object_group($bucket,$object_group,$object_group_array);
print_r($create_object_group);die();
20. get_object_group方法使用
/*
* 獲取object group
* 呼叫方法如下:
* $get_object_group = $oss_sdk_service->get_object_group($bucket,$object_group);
* 其中引數$object_group,$bucket為必須引數
*/
$bucket = 'php-sdk-1330835674';
$object_group = 'object-group-1330930277';
$get_object_group = $oss_sdk_service->get_object_group($bucket,$object_group);
print_r($get_object_group);die();
21. get_object_group_index方法使用
/**
* 獲取object group index
* 呼叫方法如下:
* $get_object_group_index = $oss_sdk_service->get_object_group_index($bucket,$object_group);
* 其中引數$object_group,$bucket為必須引數
*/
$bucket = 'php-sdk-1330835674';
$object_group = 'object-group-1330930277';
$get_object_group_index = $oss_sdk_service->get_object_group_index($bucket,$object_group);
print_r($get_object_group_index);die();
22. get_object_group_meta方法使用
/**
* 獲取object group meta
* 呼叫方法如下:
* $get_object_group_meta = $oss_sdk_service->get_object_group_meta($bucket,$object_group);
* 其中引數$object_group,$bucket為必須引數
*/
$bucket = 'php-sdk-1330835674';
$object_group = 'object-group-1330930277';
$get_object_group_meta = $oss_sdk_service->get_object_group_meta($bucket,$object_group);
print_r($get_object_group_meta);die();
23. delete_object_group方法使用
/**
* 刪除object group
* 呼叫方法如下:
* $delete_object_group = $oss_sdk_service->delete_object_group($bucket,$object_group);
* 其中引數$object_group,$bucket為必須引數
*/
$bucket = 'php-sdk-1330835674';
$object_group = 'object-group-1330930277';
$delete_object_group = $oss_sdk_service->delete_object_group($bucket,$object_group);
print_r($delete_object_group);die();
/*%*********************************************************************************************************************%*/
//Mulit Part相關 ,適合大檔案上傳
/**
* 使用Multi-Part上傳檔案,該操作需要經過如下的步驟
* 1. 初始化上傳,呼叫initiate_multipart_upload,其中$bucket,$object為必選
* 2. 上傳part,呼叫upload_part,$bucket, $object, $uploadId為必選,還需要傳入一個關聯陣列作為引數,該陣列如下定義:
* array(
* 'fileUpload' => $filepath, 檔案路徑
* 'partNumber' => ($i 1), 檔案編號
* 'seekTo' => (integer) $part['seekTo'], 讀取檔案位置
* 'length' => (integer) $part['length'], 該部分part檔案長度
* )
*
* 其中的seekTo,length會有程式自動生成
*
* 3. 完成上傳,呼叫complete_multipart_upload,其中$bucket, $object, $uploadId, $upload_parts為必選
*/
//步驟1 ,初始化multipart
$bucket = 'php-sdk-1329818657';
$object = 'Cpp.How.to.Program.7-'.time().'.pdf';
$filepath = "D:\\ccc.pdf";
$response_initiate_multipart_upload = $oss_sdk_service->initiate_multipart_upload($bucket,$object);
if(!$response_initiate_multipart_upload->isOK()){
die('initial multipart upload failed....');
}
//解析返回,獲取uploadId
$xml = new SimpleXmlIterator($response_initiate_multipart_upload->body);
$uploadId = (string)$xml->UploadId;
//步驟2 上傳part
//首先獲取檔案可以分多少個part
$parts = $oss_sdk_service->get_multipart_counts(filesize($filepath), 5242880);
$response_upload_part = array();
foreach ($parts as $i => $part){
//開始上傳part
$response_upload_part[] = $oss_sdk_service->upload_part($bucket, $object, $uploadId, array(
'fileUpload' => $filepath,
'partNumber' => ($i 1),
'seekTo' => (integer) $part['seekTo'],
'length' => (integer) $part['length'],
));
}
$upload_parts = array();
$result = true;
//獲取上傳Part返回結果
foreach ($response_upload_part as $i=>$response){
$result = $result && $response->isOk();
}
if(!$result){
$oss_sdk_service->abort_multipart_upload($bucket, $object, $uploadId);
die('any part upload failed...');
}
//構造upload part
foreach ($response_upload_part as $i=>$response){
$upload_parts[] = array(
'PartNumber' => ($i 1),
'ETag' => (string) $response->header['etag']
);
}
//步驟3 完成multipart upload
$complete_multipart_upload_response = $oss_sdk_service->complete_multipart_upload($bucket, $object, $uploadId, $upload_parts);
print_r($complete_multipart_upload_response);die();
/**
* 列出multipart上傳中的part,由於在列出part的時候需要有正在進行的multipart,故需要完成multi-part的步驟1和2,呼叫步驟如下:
* 1. 初始化上傳,呼叫initiate_multipart_upload,其中$bucket,$object為必選
* 2. 上傳part,呼叫upload_part,$bucket, $object, $uploadId為必選,還需要傳入一個關聯陣列作為引數,該陣列如下定義:
* array(
* 'fileUpload' => $filepath, 檔案路徑
* 'partNumber' => ($i 1), 檔案編號
* 'seekTo' => (integer) $part['seekTo'], 讀取檔案位置
* 'length' => (integer) $part['length'], 該部分part檔案長度
* )
*
* 其中的seekTo,length會有程式自動生成
*
* 3. 列出parts,$oss_sdk_service->list_parts($bucket, $object, $uploadId,$options);其中$bucket, $object, $uploadId為必選,$options可選
*/
//步驟1 初始化
$bucket = 'php-sdk-1329818657';
$object = 'Cpp.How.to.Program.7-'.time().'.pdf';
$filepath = "D:\\ccc.pdf";
$response_initiate_multipart_upload = $oss_sdk_service->initiate_multipart_upload($bucket,$object);
if(!$response_initiate_multipart_upload->isOK()){
die('initial multipart upload failed....');
}
$xml = new SimpleXmlIterator($response_initiate_multipart_upload->body);
$uploadId = (string)$xml->UploadId;
獲取分片
$parts = $oss_sdk_service->get_multipart_counts(filesize($filepath), 5242880);
$response_upload_part = array();
//步驟2 上傳part
foreach ($parts as $i => $part){
$response_upload_part[] = $oss_sdk_service->upload_part($bucket, $object, $uploadId, array(
'fileUpload' => $filepath,
'partNumber' => ($i 1),
'seekTo' => (integer) $part['seekTo'],
'length' => (integer) $part['length'],
));
}
/*列出parts,呼叫方法($bucket, $object, $uploadId,$options);,其中$bucket, $object, $uploadId為必選引數,$options可選,$options若傳,定義方式如下
*
*$options = array(
* 'max-parts' => 10 一次最多取多少個part
* 'part-number-marker' => 1, 從哪個part-number開始
*)
*/
$options = array(
'max-parts' => 10,
'part-number-marker' => 1,
);
//步驟3 列出parts
$list_parts_response = $oss_sdk_service->list_parts($bucket, $object, $uploadId,$options);
print_r($list_parts_response);die();
/**
* 終止multi-part upload,因為有終止進行中的任務,故需要執行步驟1和2
* 1. 初始化上傳,呼叫initiate_multipart_upload,其中$bucket,$object為必選
* 2. 上傳part,呼叫upload_part,$bucket, $object, $uploadId為必選,還需要傳入一個關聯陣列作為引數,該陣列如下定義:
* array(
* 'fileUpload' => $filepath, 檔案路徑
* 'partNumber' => ($i 1), 檔案編號
* 'seekTo' => (integer) $part['seekTo'], 讀取檔案位置
* 'length' => (integer) $part['length'], 該部分part檔案長度
* )
*
* 其中的seekTo,length會有程式自動生成
*
* 3. 終止multi-part,呼叫$oss_sdk_service->abort_multipart_upload($bucket, $object, $uploadId);其中$bucket, $object, $uploadId為必選
*/
//步驟1 初始化
$bucket = 'php-sdk-1330835674';
$object = 'Cpp.How.to.Program.7-'.time().'.pdf';
$filepath = "D:\\ccc.pdf";
$response_initiate_multipart_upload = $oss_sdk_service->initiate_multipart_upload($bucket,$object);
if(!$response_initiate_multipart_upload->isOK()){
die('initial multipart upload failed....');
}
$xml = new SimpleXmlIterator($response_initiate_multipart_upload->body);
$uploadId = (string)$xml->UploadId;
$parts = $oss_sdk_service->get_multipart_counts(filesize($filepath), 5242880);
$response_upload_part = array();
//步驟2 上傳part
foreach ($parts as $i => $part){
$response_upload_part[] = $oss_sdk_service->upload_part($bucket, $object, $uploadId, array(
'fileUpload' => $filepath,
'partNumber' => ($i 1),
'seekTo' => (integer) $part['seekTo'],
'length' => (integer) $part['length'],
));
}
//步驟3 終止
$abort_multipart_upload_response = $oss_sdk_service->abort_multipart_upload($bucket, $object, $uploadId);
print_r($abort_multipart_upload_response);die();
/**
* 列出所有的multipart upload
* 呼叫方法 $oss_sdk_service->list_multipart_uploads($bucket);其中$bucket是必選引數
*/
$bucket = 'php-sdk-1330835674';
$list_multipart_uploads_response = $oss_sdk_service->list_multipart_uploads($bucket);
print_r($list_multipart_uploads_response);die();
/**
* 一次性完成multi-part upload,由於mulit-part upload涉及三個步驟,所以封裝了該方法使得使用multi-part更加方便
* 呼叫方法如下:
* $oss_sdk_service->create_mpu_object($bucket, $object,$options);
* 其中的$bucket,$object,$options為必須,$options的定義如下
* $options = array(
* 'fileUpload' => $filepath, 檔案路徑
* 'partSize' => 5242880, 分片大小為了保證上傳的效率,建議該值不要太大,建議使用5M = 5242880
* )
*/
$bucket = 'php-sdk-1329818657';
$object = 'myobject-pdf-'.time().'.rar';
$filepath = "d:\\hh_AxureRP-Pro-Setup.rar";
$options = array(
'fileUpload' => $filepath,
'partSize' => 5242880,
);
$create_mpu_object_response = $oss_sdk_service->create_mpu_object($bucket, $object,$options);
print_r($create_mpu_object_response);die();
/**
* 上傳整個目錄,通過multi-part,通過該方式建立的object預設為檔名
* $oss_sdk_service->create_mtu_object_by_dir($bucket,$dir,$recursive = false,$exclude = ".|..|.svn",$options = null)
* 其中$bucket,$dir為必選引數,$recursive,$exclude,$options可選,其中o
* $bucket 為bucket名稱
* $dir 某一目錄
* $recursive 該引數設定是否遞迴讀取目錄。
* $exclude 要過濾掉的檔案,預設為系統預設生成的.,..,和svn檔案.svn
*/
$bucket = 'php-sdk-windows';
$dir = "C:\\Windows";
$recursive = true;
$create_mtu_object_by_dir_response = $oss_sdk_service->create_mtu_object_by_dir($bucket,$dir,$recursive);
print_r($create_mtu_object_by_dir_response);die();
第一步 : 去OSS官方註冊賬戶,並且開通OSS服務,當OSS服務開通後,進入管理中心獲取相應的API金鑰。如下截圖:
第二步: 在獲得API金鑰後,獲取OSS PHP SDK,從官方下載壓縮包,解壓後,我們會看到一個conf.inc.php的檔案,把我們第一步獲得的API金鑰填入相應的位置,程式碼如下:
//個人測試
//ACCESS_ID
define('OSS_ACCESS_ID', ‘這裡填入ACCESS ID’);
//ACCESS_KEY
define('OSS_ACCESS_KEY', '這裡填入ACCESS KEY');
第三步:當完成以上兩步以後,我們就可以開始我們神奇的OSS PHP SDK使用之旅了,接下來我們會詳細介紹每個API的使用方法,並且提供詳細的測試程式碼以及文字說明
1. 載入SDK 並且初始化相關引數
/**
* 載入sdk包以及錯誤程式碼包
*/
require_once '../sdk.class.php';
//例項化OSS Service
$oss_sdk_service = new ALIOSS();
//設定是否開啟curl除錯模式,該模式主要是為了方便除錯使用,可以設定TRUE|FALSE
$oss_sdk_service->set_debug_mode(FALSE);
2. list_bucket 方法使用
/**
* 獲取bucket列表,該方法主要獲取該賬戶下所有的bucket列表
* 使用方法如下:
* $bucket_list = $oss_sdk_service->list_bucket();
*/
$bucket_list = $oss_sdk_service->list_bucket();
print_r($bucket_list);die();
3. get_bucket_acl方法使用
/**
* 獲得指定bucket的ACL
* 呼叫方法如下:
* $get_bucket_acl = $oss_sdk_service->get_bucket_acl($bucket,$options);
* 其中需要傳入的引數為 某一$bucket的名稱,$options為一個關聯陣列,該方法可以傳入Content-Type,使用系統內建的定義方式
*/
$bucket = 'php-sdk-1330835674';
$options = array(
ALIOSS::OSS_CONTENT_TYPE => 'text/xml',
);
$get_bucket_acl = $oss_sdk_service->get_bucket_acl($bucket,$options);
print_r($get_bucket_acl);die();
4. set_bucket_acl方法使用
/**
* 設定指定bucket的ACL,目前只有三種acl private,public-read,public-read-write
* 呼叫方法如下:
* $set_bucket_acl = $oss_sdk_service->set_bucket_acl($bucket, $acl);
* 其中需要指定$bucket,以及$acl的值,$acl的取值只能是private,public-read,public-read-write其中之一,$acl使用系統內建的定義方式
* 對應的值為 private => ALIOSS::OSS_ACL_TYPE_PRIVATE ,public-read=> ALIOSS::OSS_ACL_TYPE_PUBLIC_READ ,public-read-write=> ALIOSS::OSS_ACL_TYPE_PUBLIC_READ_WRITE
*/
$bucket = 'php-sdk-1329818657';
$acl = ALIOSS::OSS_ACL_TYPE_PUBLIC_READ;
$set_bucket_acl = $oss_sdk_service->set_bucket_acl($bucket, $acl);
print_r($set_bucket_acl);die();
5. create_bucket方法使用
/**
* 建立bucket
* 呼叫方法如下:
* $create_bucket = $oss_sdk_service->create_bucket($bucket, $acl);
* 其中需要傳入$bucket,$acl是可選的,如果不指定ACL,則預設會是private,
*$acl的取值只能是private,public-read,public-read-write其中之一,$acl使用系統內建的定義方式
* 對應的值為 private => ALIOSS::OSS_ACL_TYPE_PRIVATE ,public-read=> ALIOSS::OSS_ACL_TYPE_PUBLIC_READ ,public-read-write=> ALIOSS::OSS_ACL_TYPE_PUBLIC_READ_WRITE
*/
$bucket = 'oss-php-sdk-'.time();
$acl = ALIOSS::OSS_ACL_TYPE_PUBLIC_READ;
$create_bucket = $oss_sdk_service->create_bucket($bucket, $acl);
print_r($create_bucket);die();
6. delete_bucket方法使用
/**
* 刪除bucket
* 呼叫方法如下:
* $delete_bucket = $oss_sdk_service->delete_bucket($bucket);
* 需要傳入$bucket引數,其中$bucket內不能有內容,如果有內容則本次刪除請求不會成功
*/
$bucket = 'oss-php-sdk-1330928803';
$delete_bucket = $oss_sdk_service->delete_bucket($bucket);
print_r($delete_bucket);die();
7. list_object方法使用
/**
* 獲得bucket下的object列表
* 呼叫方法如下:
* $list_object = $oss_sdk_service->list_object($bucket,$options);
* 其中需要傳入$bucket引數,$options為可選引數,如果需要傳入$options,必須為陣列,其中資料的key必須為指定的,否則引數無效
* $options = array(
* 'max-keys' => max-keys用於限定此次返回object的最大數,如果不設定,預設為100,max-keys取值不能大於100。
* 'prefix' => 限定返回的object key必須以prefix作為字首。注意使用prefix查詢時,返回的key中仍會包含prefix。
* 'delimiter' => 是一個用於對Object名字進行分組的字元。所有名字包含指定的字首且第一次出現delimiter字元之間的object作為一組元素
* 'marker' => 使用者設定結果從marker之後按字母排序的第一個開始返回。
* )
* 其中 prefix,marker用來實現分頁顯示效果,引數的長度必須小於256位元組。
*/
$bucket = 'php-sdk-1330835674';
$options = array(
'delimiter' => '/',
'prefix' => '',
'max-keys' => 10,
'marker' => 'myobject-1330850469.pdf',
);
$list_object = $oss_sdk_service->list_object($bucket,$options);
print_r($list_object);die();
8. get_object方法使用
/**
* 獲得$bucket下的某個object,$object為檔案,不能為目錄
* 呼叫方法如下:
* $get_object = $oss_sdk_service->get_object($bucket,$object,$options);
* 其中需要傳入$bucket,$object引數,引數為必須,$options為可選引數,是一個key=>value格式的關聯陣列,其中的key定義如下
* $options = array(
* 'lastmodified' => 設定請求header 'If-Modified-Since'
* 'etag' => 設定請求header Etag
* 'range' => 設定請求header Range,主要用來實現斷點下載
* 'fileDownload' => 使用者寫入本地檔案,需要有寫許可權
* )
*/
$bucket = 'php-sdk-1329818657';
$object = 'myobject-pdf-1331429369.pdf';
$filepath = "D:\\download\\myobject-".time().".pdf";
$options = array(
'fileDownload' => $filepath,
);
$get_object_response = $oss_sdk_service->get_object($bucket,$object,$options);
print_r($get_object_response);die();
9. create_object_dir方法使用
/**
* 建立資料夾(是虛擬資料夾)
* 呼叫方法如下:
* $create_object_dir = $oss_sdk_service->create_object_dir($bucket,$object);
* 其中需要傳入$bucket,$object引數,引數為必須
*/
$bucket = 'efrwerwertyrty';
$object = '我的OSSbucket-'.time();
$create_object_dir = $oss_sdk_service->create_object_dir($bucket,$object);
print_r($create_object_dir);die();
10. upload_file_by_content方法使用
/**
* 通過http body上傳檔案,適用於直接寫入內容的上傳,比較小的檔案
* 呼叫方法如下:
* $upload_file_by_content = $oss_sdk_service->upload_file_by_content($bucket,$object,$upload_file_options);
* 其中的$bucket,$object以及$upload_file_options為必選引數,$upload_file_options必須為陣列,且key必須為規定的值,否則會上傳失敗,
* $object是檔名稱,如果上傳的檔案不是直接位於bucket下,而是位於某一子目錄下,則$object = 'dir_name/dir_name/file_name'
* 其中的content 為檔案的內容,$length為檔案的大小
* $upload_file_options = array(
* 'content' => $content,
* 'length' => $length,
* );
*/
$bucket = 'php-sdk-1328520184';
$object = 'oss-file-name-'.time().'.txt';
$content = '';
for($i = 0;$i<= 10000;$i ){
$content .= $i."\n";
}
$upload_file_options = array(
'content' => $content,
'length' => strlen($content),
);
$upload_file_by_content = $oss_sdk_service->upload_file_by_content($bucket,$object,$upload_file_options);
print_r($upload_file_by_content);die();
11. upload_file_by_file 方法使用
/**
* 通過檔案方式上傳,適合小檔案上傳,大檔案上傳請使用multipart
*/
$bucket = 'php-sdk-1328520898';
$file = "d:\\ccc.pdf"; 檔案路徑
$object = 'cpp.how.to.program-'.time().'.pdf'; //object名稱
$response_upload_file_by_file = $oss_sdk_service->upload_file_by_file($bucket,$object,$file);
print_r($response_upload_file_by_file);die();
12. copy_object 方法使用
/**
* Copy Object
* 呼叫方法如下:
* $copy_object_result = $oss_sdk_service->copy_object($from_bucket, $from_object,$to_bucket, $to_object)
* 其中需要傳入$from_bucket,$from_object,$to_bucket,$to_object等引數,均不能為空
*/
$from_bucket = 'aaaaaaaaaaa';
$to_bucket = 'efrwerwertyrty';
$from_object = 'CreateLaunchConfigurationRequest.cs';
$to_object = 'copy-object-'.time().'.cs';
$copy_object_result = $oss_sdk_service->copy_object($from_bucket, $from_object,$to_bucket, $to_object);
print_r($copy_object_result);die();
13. get_sign_url方法使用
/**
* 獲取帶簽名的外鏈URL
* 呼叫方法如下:
* $get_sign_url = $oss_sdk_service->get_sign_url($bucket,$object,$timeout)
* 其中需要傳入$bucket,$object不能為空,$timeout為過期的秒數,從當前時間往後多少秒過期,若不傳,則使用預設值60
*/
$bucket = 'php-sdk-1330835674';
$object = 'Cpp.How.to.Program.7-1330855122.pdf';
$timeout = 3600;
$get_sign_url = $oss_sdk_service->get_sign_url($bucket,$object,$timeout);
print_r($get_sign_url);die();
14. is_object_exist方法使用
/**
* 檢測Object是否存在
* 呼叫方法如下:
* $is_object_exist = $oss_sdk_service->is_object_exist($bucket, $object);
* 其中需要傳入$bucket,$object引數,引數為必須
*/
$bucket = 'php-sdk-1330835674';
$object = 'Cpp.How.to.Program.7-1330855122.pdf';
$is_object_exist = $oss_sdk_service->is_object_exist($bucket, $object);
var_dump($is_object_exist?'Exist':'Not Exist');die();
15 . get_object_url方法使用
/**
* 獲得某一個object的URL
* 呼叫方法如下:
* $get_object_url = $oss_sdk_service->get_object_url($bucket, $object);
* 其中需要傳入$bucket,$object引數,引數為必須,$object為檔案
*/
$bucket = 'php-sdk-1330835674';
$object = 'Cpp.How.to.Program.7-1330855122.pdf';
$get_object_url = $oss_sdk_service->get_object_url($bucket, $object);
var_dump($get_object_url?$get_object_url:'Not Exist');die();
16. get_object_meta方法使用
/**
* 獲得object的meta
* 呼叫方法如下:
* $get_object_meta = $oss_sdk_service->get_object_meta($bucket, $object);
* 其中需要傳入$bucket,$object引數,引數為必須,如果object為資料夾,則需要新增'/'
*/
$bucket = 'php-sdk-1330835674';
$object = 'Cpp.How.to.Program.7-1330855122.pdf';
$get_object_meta = $oss_sdk_service->get_object_meta($bucket, $object);
print_r($get_object_meta);die();
17. delete_object方法使用
/**
* 刪除object
* 呼叫方法如下:
* $delete_object = $oss_sdk_service->delete_object($bucket,$object);
* 其中需要傳入$bucket,$object引數,如果object為檔案,則會直接刪除,如果為資料夾,若資料夾不為空,則不能刪除
*/
$bucket = 'php-sdk-1330835674';
$object = 'Cpp.How.to.Program.7-1330855122.pdf';
$delete_object = $oss_sdk_service->delete_object($bucket,$object);
print_r($delete_object);die();
18. delete_objects方法使用
/**
* 批量刪除某一bucket下的objects
* 呼叫方法如下:
* $delete_objects_response = $oss_sdk_service->delete_objects($bucket,$objects,$options);
* 其中需要傳入$bucket,$object引數,如果objects為bucket下的object陣列
* $options 可以設定返回模式,目前提供兩種返回模式:詳細(verbose)模式和簡單(quiet)模式,預設為verbose模式,模式的設定如下
* $options = array('quite'=>true|false),true 為quiet模式,false為verbose模式
*/
$bucket = 'myossbucket002';
$objects = array(
'AliUpdate.dll','libGLESv2.dll'
);
$options = array(
'quiet' => true,
);
$delete_objects_response = $oss_sdk_service->delete_objects($bucket, $objects,$options);
print_r($delete_objects_response);die();
19. create_object_group方法使用
/**
* 建立object group
* 呼叫方法如下:
* $create_object_group = $oss_sdk_service->create_object_group($bucket,$object_group,$object_group_array);
* 其中引數$object_group,$bucket,$object_group_array不為空,且$object_group_array內的object必須位於同一bucket下
*/
$bucket = 'php-sdk-1330835674';
$object_group = 'object-group-'.time();
$object_group_array = array(
'Cpp.How.to.Program.7-1330855122.pdf',
'copy-object-1330929729.pdf',
'hh_axur-setup.pdf',
);
$create_object_group = $oss_sdk_service->create_object_group($bucket,$object_group,$object_group_array);
print_r($create_object_group);die();
20. get_object_group方法使用
/*
* 獲取object group
* 呼叫方法如下:
* $get_object_group = $oss_sdk_service->get_object_group($bucket,$object_group);
* 其中引數$object_group,$bucket為必須引數
*/
$bucket = 'php-sdk-1330835674';
$object_group = 'object-group-1330930277';
$get_object_group = $oss_sdk_service->get_object_group($bucket,$object_group);
print_r($get_object_group);die();
21. get_object_group_index方法使用
/**
* 獲取object group index
* 呼叫方法如下:
* $get_object_group_index = $oss_sdk_service->get_object_group_index($bucket,$object_group);
* 其中引數$object_group,$bucket為必須引數
*/
$bucket = 'php-sdk-1330835674';
$object_group = 'object-group-1330930277';
$get_object_group_index = $oss_sdk_service->get_object_group_index($bucket,$object_group);
print_r($get_object_group_index);die();
22. get_object_group_meta方法使用
/**
* 獲取object group meta
* 呼叫方法如下:
* $get_object_group_meta = $oss_sdk_service->get_object_group_meta($bucket,$object_group);
* 其中引數$object_group,$bucket為必須引數
*/
$bucket = 'php-sdk-1330835674';
$object_group = 'object-group-1330930277';
$get_object_group_meta = $oss_sdk_service->get_object_group_meta($bucket,$object_group);
print_r($get_object_group_meta);die();
23. delete_object_group方法使用
/**
* 刪除object group
* 呼叫方法如下:
* $delete_object_group = $oss_sdk_service->delete_object_group($bucket,$object_group);
* 其中引數$object_group,$bucket為必須引數
*/
$bucket = 'php-sdk-1330835674';
$object_group = 'object-group-1330930277';
$delete_object_group = $oss_sdk_service->delete_object_group($bucket,$object_group);
print_r($delete_object_group);die();
/*%*********************************************************************************************************************%*/
//Mulit Part相關 ,適合大檔案上傳
/**
* 使用Multi-Part上傳檔案,該操作需要經過如下的步驟
* 1. 初始化上傳,呼叫initiate_multipart_upload,其中$bucket,$object為必選
* 2. 上傳part,呼叫upload_part,$bucket, $object, $uploadId為必選,還需要傳入一個關聯陣列作為引數,該陣列如下定義:
* array(
* 'fileUpload' => $filepath, 檔案路徑
* 'partNumber' => ($i 1), 檔案編號
* 'seekTo' => (integer) $part['seekTo'], 讀取檔案位置
* 'length' => (integer) $part['length'], 該部分part檔案長度
* )
*
* 其中的seekTo,length會有程式自動生成
*
* 3. 完成上傳,呼叫complete_multipart_upload,其中$bucket, $object, $uploadId, $upload_parts為必選
*/
//步驟1 ,初始化multipart
$bucket = 'php-sdk-1329818657';
$object = 'Cpp.How.to.Program.7-'.time().'.pdf';
$filepath = "D:\\ccc.pdf";
$response_initiate_multipart_upload = $oss_sdk_service->initiate_multipart_upload($bucket,$object);
if(!$response_initiate_multipart_upload->isOK()){
die('initial multipart upload failed....');
}
//解析返回,獲取uploadId
$xml = new SimpleXmlIterator($response_initiate_multipart_upload->body);
$uploadId = (string)$xml->UploadId;
//步驟2 上傳part
//首先獲取檔案可以分多少個part
$parts = $oss_sdk_service->get_multipart_counts(filesize($filepath), 5242880);
$response_upload_part = array();
foreach ($parts as $i => $part){
//開始上傳part
$response_upload_part[] = $oss_sdk_service->upload_part($bucket, $object, $uploadId, array(
'fileUpload' => $filepath,
'partNumber' => ($i 1),
'seekTo' => (integer) $part['seekTo'],
'length' => (integer) $part['length'],
));
}
$upload_parts = array();
$result = true;
//獲取上傳Part返回結果
foreach ($response_upload_part as $i=>$response){
$result = $result && $response->isOk();
}
if(!$result){
$oss_sdk_service->abort_multipart_upload($bucket, $object, $uploadId);
die('any part upload failed...');
}
//構造upload part
foreach ($response_upload_part as $i=>$response){
$upload_parts[] = array(
'PartNumber' => ($i 1),
'ETag' => (string) $response->header['etag']
);
}
//步驟3 完成multipart upload
$complete_multipart_upload_response = $oss_sdk_service->complete_multipart_upload($bucket, $object, $uploadId, $upload_parts);
print_r($complete_multipart_upload_response);die();
/**
* 列出multipart上傳中的part,由於在列出part的時候需要有正在進行的multipart,故需要完成multi-part的步驟1和2,呼叫步驟如下:
* 1. 初始化上傳,呼叫initiate_multipart_upload,其中$bucket,$object為必選
* 2. 上傳part,呼叫upload_part,$bucket, $object, $uploadId為必選,還需要傳入一個關聯陣列作為引數,該陣列如下定義:
* array(
* 'fileUpload' => $filepath, 檔案路徑
* 'partNumber' => ($i 1), 檔案編號
* 'seekTo' => (integer) $part['seekTo'], 讀取檔案位置
* 'length' => (integer) $part['length'], 該部分part檔案長度
* )
*
* 其中的seekTo,length會有程式自動生成
*
* 3. 列出parts,$oss_sdk_service->list_parts($bucket, $object, $uploadId,$options);其中$bucket, $object, $uploadId為必選,$options可選
*/
//步驟1 初始化
$bucket = 'php-sdk-1329818657';
$object = 'Cpp.How.to.Program.7-'.time().'.pdf';
$filepath = "D:\\ccc.pdf";
$response_initiate_multipart_upload = $oss_sdk_service->initiate_multipart_upload($bucket,$object);
if(!$response_initiate_multipart_upload->isOK()){
die('initial multipart upload failed....');
}
$xml = new SimpleXmlIterator($response_initiate_multipart_upload->body);
$uploadId = (string)$xml->UploadId;
獲取分片
$parts = $oss_sdk_service->get_multipart_counts(filesize($filepath), 5242880);
$response_upload_part = array();
//步驟2 上傳part
foreach ($parts as $i => $part){
$response_upload_part[] = $oss_sdk_service->upload_part($bucket, $object, $uploadId, array(
'fileUpload' => $filepath,
'partNumber' => ($i 1),
'seekTo' => (integer) $part['seekTo'],
'length' => (integer) $part['length'],
));
}
/*列出parts,呼叫方法($bucket, $object, $uploadId,$options);,其中$bucket, $object, $uploadId為必選引數,$options可選,$options若傳,定義方式如下
*
*$options = array(
* 'max-parts' => 10 一次最多取多少個part
* 'part-number-marker' => 1, 從哪個part-number開始
*)
*/
$options = array(
'max-parts' => 10,
'part-number-marker' => 1,
);
//步驟3 列出parts
$list_parts_response = $oss_sdk_service->list_parts($bucket, $object, $uploadId,$options);
print_r($list_parts_response);die();
/**
* 終止multi-part upload,因為有終止進行中的任務,故需要執行步驟1和2
* 1. 初始化上傳,呼叫initiate_multipart_upload,其中$bucket,$object為必選
* 2. 上傳part,呼叫upload_part,$bucket, $object, $uploadId為必選,還需要傳入一個關聯陣列作為引數,該陣列如下定義:
* array(
* 'fileUpload' => $filepath, 檔案路徑
* 'partNumber' => ($i 1), 檔案編號
* 'seekTo' => (integer) $part['seekTo'], 讀取檔案位置
* 'length' => (integer) $part['length'], 該部分part檔案長度
* )
*
* 其中的seekTo,length會有程式自動生成
*
* 3. 終止multi-part,呼叫$oss_sdk_service->abort_multipart_upload($bucket, $object, $uploadId);其中$bucket, $object, $uploadId為必選
*/
//步驟1 初始化
$bucket = 'php-sdk-1330835674';
$object = 'Cpp.How.to.Program.7-'.time().'.pdf';
$filepath = "D:\\ccc.pdf";
$response_initiate_multipart_upload = $oss_sdk_service->initiate_multipart_upload($bucket,$object);
if(!$response_initiate_multipart_upload->isOK()){
die('initial multipart upload failed....');
}
$xml = new SimpleXmlIterator($response_initiate_multipart_upload->body);
$uploadId = (string)$xml->UploadId;
$parts = $oss_sdk_service->get_multipart_counts(filesize($filepath), 5242880);
$response_upload_part = array();
//步驟2 上傳part
foreach ($parts as $i => $part){
$response_upload_part[] = $oss_sdk_service->upload_part($bucket, $object, $uploadId, array(
'fileUpload' => $filepath,
'partNumber' => ($i 1),
'seekTo' => (integer) $part['seekTo'],
'length' => (integer) $part['length'],
));
}
//步驟3 終止
$abort_multipart_upload_response = $oss_sdk_service->abort_multipart_upload($bucket, $object, $uploadId);
print_r($abort_multipart_upload_response);die();
/**
* 列出所有的multipart upload
* 呼叫方法 $oss_sdk_service->list_multipart_uploads($bucket);其中$bucket是必選引數
*/
$bucket = 'php-sdk-1330835674';
$list_multipart_uploads_response = $oss_sdk_service->list_multipart_uploads($bucket);
print_r($list_multipart_uploads_response);die();
/**
* 一次性完成multi-part upload,由於mulit-part upload涉及三個步驟,所以封裝了該方法使得使用multi-part更加方便
* 呼叫方法如下:
* $oss_sdk_service->create_mpu_object($bucket, $object,$options);
* 其中的$bucket,$object,$options為必須,$options的定義如下
* $options = array(
* 'fileUpload' => $filepath, 檔案路徑
* 'partSize' => 5242880, 分片大小為了保證上傳的效率,建議該值不要太大,建議使用5M = 5242880
* )
*/
$bucket = 'php-sdk-1329818657';
$object = 'myobject-pdf-'.time().'.rar';
$filepath = "d:\\hh_AxureRP-Pro-Setup.rar";
$options = array(
'fileUpload' => $filepath,
'partSize' => 5242880,
);
$create_mpu_object_response = $oss_sdk_service->create_mpu_object($bucket, $object,$options);
print_r($create_mpu_object_response);die();
/**
* 上傳整個目錄,通過multi-part,通過該方式建立的object預設為檔名
* $oss_sdk_service->create_mtu_object_by_dir($bucket,$dir,$recursive = false,$exclude = ".|..|.svn",$options = null)
* 其中$bucket,$dir為必選引數,$recursive,$exclude,$options可選,其中o
* $bucket 為bucket名稱
* $dir 某一目錄
* $recursive 該引數設定是否遞迴讀取目錄。
* $exclude 要過濾掉的檔案,預設為系統預設生成的.,..,和svn檔案.svn
*/
$bucket = 'php-sdk-windows';
$dir = "C:\\Windows";
$recursive = true;
$create_mtu_object_by_dir_response = $oss_sdk_service->create_mtu_object_by_dir($bucket,$dir,$recursive);
print_r($create_mtu_object_by_dir_response);die();