PHPackages                             jichangfeng/yun-storage - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [File &amp; Storage](/categories/file-storage)
4. /
5. jichangfeng/yun-storage

ActiveLibrary[File &amp; Storage](/categories/file-storage)

jichangfeng/yun-storage
=======================

Upload attachments to content storage platform like Aliyun OSS, Tencent COS

v1.12(2y ago)48352[1 issues](https://github.com/jichangfeng/yun-storage/issues)1MITPHPPHP &gt;=5.6

Since Sep 29Pushed 2y ago1 watchersCompare

[ Source](https://github.com/jichangfeng/yun-storage)[ Packagist](https://packagist.org/packages/jichangfeng/yun-storage)[ Docs](http://github.com/jichangfeng/yun-storage)[ RSS](/packages/jichangfeng-yun-storage/feed)WikiDiscussions main Synced yesterday

READMEChangelog (10)Dependencies (2)Versions (14)Used By (1)

Overview
========

[](#overview)

[![Latest Stable Version](https://camo.githubusercontent.com/55becdb65f00beb3ccafe1d45c005ae490e7b9b39aa33e1c117a8e5a1c2ae858/68747470733a2f2f706f7365722e707567782e6f72672f6a696368616e6766656e672f79756e2d73746f726167652f762f737461626c652e706e67)](https://packagist.org/packages/jichangfeng/yun-storage)[![Total Downloads](https://camo.githubusercontent.com/7a556626811cd0a0c777d64d2a81c2283d53dd400d53312654d3a71c580d8672/68747470733a2f2f706f7365722e707567782e6f72672f6a696368616e6766656e672f79756e2d73746f726167652f646f776e6c6f6164732e706e67)](https://packagist.org/packages/jichangfeng/yun-storage)[![License](https://camo.githubusercontent.com/0ca4a44cbcb1166d1e4aa1f00184ff3f2fb5cf919b0173b5915f6150973c3626/68747470733a2f2f706f7365722e707567782e6f72672f6a696368616e6766656e672f79756e2d73746f726167652f6c6963656e73652e706e67)](https://packagist.org/packages/jichangfeng/yun-storage)

Yun storage provides a layer that mediates between a user or configured storage frontend and one or several storage backends.

Note: [jichangfeng/laravel-yun-storage](https://github.com/jichangfeng/laravel-yun-storage) is a simple, but elegant laravel wrapper around yun storage.

Supported back-end storage
==========================

[](#supported-back-end-storage)

- [Aliyun OSS](https://www.aliyun.com/product/oss)
- [Tencent COS](https://cloud.tencent.com/product/cos)

Run environment
===============

[](#run-environment)

- PHP 5.6+

Install
=======

[](#install)

### Composer

[](#composer)

Execute the following command to get the latest version of the package:

```
composer require jichangfeng/yun-storage

```

Usage
=====

[](#usage)

#### Initialize

[](#initialize)

```
try {
    //Make a storage manager instance.
    $storage = new \YunStorage\StorageManager();
    //
    //Register Aliyun OSS Storage Adapter
    //Note: The first registered storage adapter will be the default.
    $storage->registerAdapter('oss', [
        'accessKeyId' => '',
        'accessKeySecret' => '',
        'endpoint' => ''
    ]);
    //
    //Register Tencent COS Storage Adapter
    $storage->registerAdapter('cos', [
        'accessKeyId' => '',
        'accessKeySecret' => '',
        'region' => '',
        'schema' => '',
        'appid' => ''
    ]);
    //
    //Set the default storage adapter name. Supported: "oss", "cos"
    $storage->setDefaultAdapter('oss');
    //
    //If your application interacts with default storage adapter.
    $storage->putObject($bucket, $object, $content);
    //
    //If your application interacts with multiple storage adapters,
    //you may use the 'adapter' method to work on a particular storage adapter.
    $storage->adapter('oss')->putObject($bucket, $object, $content);
    $storage->adapter('cos')->putObject($bucket, $object, $content);
    //
    //Directly call the storage object at the back-end of the storage adapter
    $storage->adapter()->client();
    $storage->adapter('oss')->client()->listObjects($bucket, $options);
    $storage->adapter('cos')->client()->listObjects($arg);
} catch (\Exception $e) {
    echo $e->getMessage();
}
```

#### Method

[](#method)

```
try {
    //Creates bucket
    $storage->createBucket($bucket);
    //
    //Checks if a bucket exists
    $storage->doesBucketExist($bucket);
    //
    //Deletes bucket
    $storage->deleteBucket($bucket);
    //
    //Lists the Bucket
    $storage->listBuckets();
    //
    //Uploads the $content object.
    $storage->putObject($bucket, $object, $content);
    //
    //Checks if the object exists
    $storage->doesObjectExist($bucket, $object);
    //
    //Deletes a object
    $storage->deleteObject($bucket, $object);
    //
    //Deletes multiple objects in a bucket
    $storage->deleteObjects($bucket, $objects);
    //
    //Gets Object content
    $storage->getObject($bucket, $object);
    //
    //Lists the bucket's object keys
    $storage->listObjectKeys($bucket, $prefix);
    //
    //Uploads a local file
    $storage->uploadFile($bucket, $object, $localfile);
    //
    //Downloads to local file
    $storage->downloadFile($bucket, $object, $localfile);
    //
    // Gets the storage client, return the actual storage object
    $storage->adapter()->client();
} catch (\Exception $e) {
    echo $e->getMessage();
}
```

#### Example

[](#example)

```
