测试阿里云OSS

说明:开通阿里云OSS,下载PHP SDK,修改信息


在根目录下创建一个index.php

create_bucket($bucket, $acl);
        $msg = "创建bucket " . $bucket;
        OSSUtil::print_res($res, $msg);
    }
 
 
}
//初始化
header("Content-Type: text/html; charset=UTF-8");
date_default_timezone_set('Asia/Shanghai');
 
$oss = SampleUtil::get_oss_client();
$bucket = SampleUtil::get_bucket_name();
 
exit;
//上传图片
$object = "test/a.jpg";
$file_path = './1.jpg';
$url="http://".OSS_TEST_BUCKET.".".OSS_ENDPOINT."/".$object;
$options = array();
$res = $oss->upload_file_by_file($bucket, $object, $file_path, $options);
$msg = "上传本地文件 :" . $file_path . " 到 /" . $bucket . "/"
 . $object."  url:".$url;
OSSUtil::print_res($res, $msg);
echo "";
 
exit;
//创建文件夹
$object = "test";
$res = $oss->create_object_dir($bucket, $object);
$msg = "创建模拟文件夹 /" . $bucket . "/" . $object;
OSSUtil::print_res($res, $msg);
 
exit;
//列出Bucket内所有文件 递归列出目录下所有文件
$prefix = '';
$marker = '';
$delimiter = '';
$next_marker = '';
$maxkeys = 1000;
$index = 1;
while (true)
{
    $options = array(
        'delimiter' => $delimiter,
        'prefix' => $prefix,
        'max-keys' => $maxkeys,
        'marker' => $next_marker,
    );
    $res = $oss->list_object($bucket, $options);
    $msg = "列出Bucket内所有文件" . $bucket;
    OSSUtil::print_res($res, $msg);
    if ($res->isOk()){
        $body = $res->body;
        $tmp_object_list = OSSUtil::get_object_list_marker_from_xml($body, $next_marker);
        //打印出所有的object名称
        foreach ($tmp_object_list as $key) {
            SampleUtil::my_echo("No. " . $index . "
; : " . $key."
"); $index++; } } if (empty($next_marker)) { break; } } exit;



你可能感兴趣的:(PHP)