1. 程式人生 > >阿里雲-物件儲存OSS

阿里雲-物件儲存OSS

文件地址

https://help.aliyun.com/document_detail/32099.html?spm=a2c4g.11186623.6.766.74cdc839H0RSId

安裝(此處為composer安裝)

1、PHP 5.3+
2、擴充套件依賴:cURL
3、在Ubuntu系統中,請使用apt-get包管理器安裝PHP的cURL擴充套件 sudo apt-get install php-curl
4、在CentOS系統中,請使用yum包管理器安裝PHP的cURL擴充套件 sudo yum install php-curl

composer安裝

1、根目錄執行:composer require aliyuncs/oss-sdk-php 或者 在composer.json檔案中新增依賴關係.
2、如果使用composer出現網路錯誤,可以使用composer中國區的映象源。方法是在命令列執行composer config -g repositories.packagist composer http://packagist.phpcomposer.com

"require": {
     "aliyuncs/oss-sdk-php": "~2.x.x"
 }

# composer install
// vendor/目錄下包含了所依賴的庫。您需要在app.php中新增依賴關係
# require_once __DIR__ . '/vendor/autoload.php';

原始碼安裝方式

1、Github地址:https://github.com/aliyun/aliyun-oss-php-sdk/releases?spm=a2c4g.11186623.2.11.35dac83942Vl1K
2、 在GitHub中選擇相應版本並下載打包好的zip檔案
3、解壓後的根目錄中包含一個autoload.php檔案,在程式碼中引入此檔案:require_once ‘/path/to/oss-sdk/autoload.php’;

常見操作

  • 建立儲存空間
  • 上傳檔案
  • 下載檔案
  • 列舉檔案
  • 刪除檔案

建立儲存空間
這裡寫圖片描述
這裡寫圖片描述

<?php
/*
 * 建立儲存空間
 */
if (is_file(__DIR__ . '/vendor/autoload.php')) {
    require_once __DIR__ . '/vendor/autoload.php';
}
use OSS\OssClient;
use OSS\Core\OssException;

//阿里雲主賬號AccessKey擁有所有API的訪問許可權
//建立RAM賬號:https://ram.console.aliyun.com
$accessKeyId = "YOUR ACCESSID";//需修改
$accessKeySecret = "YOUR ACCESSSECRET";//需修改
$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";//OSS資料中心訪問域名,此處為杭州,以實際情況為準
$bucket = "jason-test0727";// 儲存空間名稱(數字、小寫字母、-),唯一性
try {
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
    $ossClient->createBucket($bucket);
} catch (OssException $e) {
    print $e->getMessage();
}

上傳檔案

<?php
/**
 * 上傳檔案
 * PS:如果伺服器上有同名的檔案,則檔案內容會被覆蓋
 */
if (is_file(__DIR__ . '/vendor/autoload.php')) {
    require_once __DIR__ . '/vendor/autoload.php';
}
use OSS\OssClient;
use OSS\Core\OssException;
//accessId 和 accessSecret
$accessKeyId = "<yourAccessKeyId>";
$accessKeySecret = "<yourAccessKeySecret>";
//資料中心,此處為杭州
$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
//選擇儲存空間
$bucket= "jason-test0727";
//自定義檔名
$object = "Hello.md";
//自定義檔案內容
$content = "Hello World!";
try {
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
    $ossClient->putObject($bucket, $object, $content);
} catch (OssException $e) {
    print $e->getMessage();
}

下載檔案:

<?php
/**
 * 下載檔案
 * PS:只是獲取到檔案中的內容,未將整個檔案下載到本地
 */
if (is_file(__DIR__ . '/vendor/autoload.php')) {
    require_once __DIR__ . '/vendor/autoload.php';
}
use OSS\OssClient;
use OSS\Core\OssException;
//accessId 和 accessSecret
$accessKeyId = "<yourAccessKeyId>";
$accessKeySecret = "TNua9ItLYfhyt0OFX6RR1oTPMKQ2gX";
//資料中心,此處為杭州
$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
//選擇儲存空間
$bucket= "jason-test0727";
//選擇檔名
$object = "Hello.md";
try {
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
    $content = $ossClient->getObject($bucket, $object);
    print("object content: " . $content);
} catch (OssException $e) {
    print $e->getMessage();
}

列舉檔案:

<?php
if (is_file(__DIR__ . '/vendor/autoload.php')) {
    require_once __DIR__ . '/vendor/autoload.php';
}
use OSS\OssClient;
use OSS\Core\OssException;

$accessKeyId = "<yourAccessKeyId>";
$accessKeySecret = "<yourAccessKeySecret>";
$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// 儲存空間名稱
$bucket= "jason-test0727";
try {
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
    $listObjectInfo = $ossClient->listObjects($bucket);
    $objectList = $listObjectInfo->getObjectList();
    if (!empty($objectList)) {
        foreach ($objectList as $objectInfo) {
            print($objectInfo->getKey() . "\t" . $objectInfo->getSize() . "\t" . $objectInfo->getLastModified() . "<br>");
        }
    }
} catch (OssException $e) {
    print $e->getMessage();
}

刪除檔案

<?php
if (is_file(__DIR__ . '/vendor/autoload.php')) {
    require_once __DIR__ . '/vendor/autoload.php';
}
use OSS\OssClient;
use OSS\Core\OssException;

$accessKeyId = "<yourAccessKeyId>";
$accessKeySecret = "<yourAccessKeySecret>";
// Endpoint以杭州為例,其它Region請按實際情況填寫。
$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// 儲存空間名稱
$bucket= "jason-test0727";
// 檔名稱
$object = "hello.txt";
try {
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
    $ossClient->deleteObject($bucket, $object);
} catch (OssException $e) {
    print $e->getMessage();
}

初始化。。。。未完待續