aliyun / aliyun-oss-php-sdk

Aliyun OSS SDK for PHP
MIT License
994 stars 348 forks source link

Alibaba Cloud OSS SDK for PHP

Latest Stable Version Build Status Coverage Status

README of Chinese

Overview

Alibaba Cloud Object Storage Service (OSS) is a cloud storage service provided by Alibaba Cloud, featuring a massive capacity, security, a low cost, and high reliability. You can upload and download data on any application anytime and anywhere by calling APIs, and perform simple management of data through the web console. The OSS can store any type of files and therefore applies to various websites, development enterprises and developers.

Run environment

Tips:

Install OSS PHP SDK

Quick use

Common classes

Class Explanation
OSS\OssClient OSS client class. An OssClient instance can be used to call the interface.
OSS\Core\OssException OSS Exception class . You only need to pay attention to this exception when you use the OssClient.

Initialize an OssClient

The SDK's operations for the OSS are performed through the OssClient class. The code below creates an OssClient object:

<?php
$accessKeyId = "<AccessKeyID that you obtain from OSS>";
$accessKeySecret = "<AccessKeySecret that you obtain from OSS>";
$endpoint = "<Domain that you select to access an OSS data center, such as "oss-cn-hangzhou.aliyuncs.com>";
try {
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
} catch (OssException $e) {
    print $e->getMessage();
}

Operations on objects

Objects are the most basic data units on the OSS. You can simply consider objects as files. The following code uploads an object:

<?php
$bucket= "<Name of the bucket in use. Pay attention to naming conventions>";
$object = "<Name of the object in use. Pay attention to naming conventions>";
$content = "Hello, OSS!"; // Content of the uploaded file
try {
    $ossClient->putObject($bucket, $object, $content);
} catch (OssException $e) {
    print $e->getMessage();
}

Operations on buckets

Buckets are the space that you use to manage the stored objects. It is an object management unit for users. Each object must belong to a bucket. You can create a bucket with the following code:

<?php
$bucket= "<Name of the bucket in use. Pay attention to naming conventions>";
try {
    $ossClient->createBucket($bucket);
} catch (OssException $e) {
    print $e->getMessage();
}

Handle returned results

The OssClient provides the following two types of returned data from interfaces:

Run a sample project

Run a unit test

License

Contact us