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

Overview

Overview

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 is a simple, but elegant laravel wrapper around yun storage.

Supported back-end storage

Run environment

  • PHP 5.6+

Install

Composer

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

composer require jichangfeng/yun-storage

Usage

Initialize

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(); } ">
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

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);
    //
    // Gets the storage client, return the actual storage object
    $storage->adapter()->client();
} catch (\Exception $e) {
    echo $e->getMessage();
}

Example

createBucket($bucket)); echo PHP_EOL; echo 'doesBucketExist: ' . PHP_EOL; print_r($storage->doesBucketExist($bucket)); echo PHP_EOL; echo 'listBuckets: ' . PHP_EOL; print_r($storage->listBuckets($bucket)); echo PHP_EOL; echo 'putObject: ' . PHP_EOL; print_r($storage->putObject($bucket, $object, $content)); echo PHP_EOL; echo 'putObject2: ' . PHP_EOL; print_r($storage->putObject($bucket, $object2, $content2)); echo PHP_EOL; echo 'doesObjectExist: ' . PHP_EOL; print_r($storage->doesObjectExist($bucket, $object)); echo PHP_EOL; echo 'getObject: ' . PHP_EOL; print_r($storage->getObject($bucket, $object)); echo PHP_EOL; echo 'getObject2: ' . PHP_EOL; print_r($storage->getObject($bucket, $object2)); echo PHP_EOL; echo 'listObjectKeys: ' . PHP_EOL; print_r($storage->listObjectKeys($bucket, $prefix)); echo PHP_EOL; echo 'deleteObject: ' . PHP_EOL; print_r($storage->deleteObject($bucket, $object)); echo PHP_EOL; echo 'deleteObject2: ' . PHP_EOL; print_r($storage->deleteObject($bucket, $object2)); echo PHP_EOL; echo 'deleteObjects: ' . PHP_EOL; print_r($storage->deleteObjects($bucket, [$object, $object2])); echo PHP_EOL; echo 'deleteBucket: ' . PHP_EOL; print_r($storage->deleteBucket($bucket)); echo PHP_EOL; } catch (\Exception $e) { echo 'exception: ' . $e->getCode() . ' - ' . $e->getMessage(); echo PHP_EOL; } ">

try {
    // Assuming $storage is initialized
    $bucket = 'yun-storage-example';
    $object = 'aa-bb/cc-dd/2021-09-26/ee-ff.json';
    $object2 = 'aa-bb/cc-dd/2021-09-26/ee-ff-2.json';
    $content = '{"type":"text", "data":{"msg":"some message"}}';
    $content2 = '{"type":"text", "data":{"msg":"other message"}}';
    $prefix = 'aa-bb/cc-dd';
    echo 'createBucket: ' . PHP_EOL;
    print_r($storage->createBucket($bucket));
    echo PHP_EOL;
    echo 'doesBucketExist: ' . PHP_EOL;
    print_r($storage->doesBucketExist($bucket));
    echo PHP_EOL;
    echo 'listBuckets: ' . PHP_EOL;
    print_r($storage->listBuckets($bucket));
    echo PHP_EOL;
    echo 'putObject: ' . PHP_EOL;
    print_r($storage->putObject($bucket, $object, $content));
    echo PHP_EOL;
    echo 'putObject2: ' . PHP_EOL;
    print_r($storage->putObject($bucket, $object2, $content2));
    echo PHP_EOL;
    echo 'doesObjectExist: ' . PHP_EOL;
    print_r($storage->doesObjectExist($bucket, $object));
    echo PHP_EOL;
    echo 'getObject: ' . PHP_EOL;
    print_r($storage->getObject($bucket, $object));
    echo PHP_EOL;
    echo 'getObject2: ' . PHP_EOL;
    print_r($storage->getObject($bucket, $object2));
    echo PHP_EOL;
    echo 'listObjectKeys: ' . PHP_EOL;
    print_r($storage->listObjectKeys($bucket, $prefix));
    echo PHP_EOL;
    echo 'deleteObject: ' . PHP_EOL;
    print_r($storage->deleteObject($bucket, $object));
    echo PHP_EOL;
    echo 'deleteObject2: ' . PHP_EOL;
    print_r($storage->deleteObject($bucket, $object2));
    echo PHP_EOL;
    echo 'deleteObjects: ' . PHP_EOL;
    print_r($storage->deleteObjects($bucket, [$object, $object2]));
    echo PHP_EOL;
    echo 'deleteBucket: ' . PHP_EOL;
    print_r($storage->deleteBucket($bucket));
    echo PHP_EOL;
} catch (\Exception $e) {
    echo 'exception: ' . $e->getCode() . ' - ' . $e->getMessage();
    echo PHP_EOL;
}
You might also like...
A simple mailable trait and interface to export mails to a storage disk once being sent.

Laravel Mail Export This package can export any mail sent with Laravel's Mailable class to any desired filesystem disk and path as a .eml file. This c

Greyhole uses Samba to create a storage pool of all your available hard drives, and allows you to create redundant copies of the files you store.

Greyhole Greyhole is an application that uses Samba to create a storage pool of all your available hard drives (whatever their size, however they're c

A Qiniu Storage filesystem for Laravel

Laravel filesystem Qiniu Qiniu storage for Laravel based on overtrue/flysystem-qiniu. Requirement PHP = 5.5.9 Installation $ composer require "overtr

High-performance, low-memory-footprint, single-file embedded database for key/value storage

LDBA - a fast, pure PHP, key-value database. Information LDBA is a high-performance, low-memory-footprint, single-file embedded database for key/value

Laravel & Google Drive Storage - Demo project with Laravel 6.x and earlier

Laravel & Google Drive Storage Demo project with Laravel 8.X Look at the commit history to see each of the steps I have taken to set this up. Set up t

This Repo is a storage of Postman collections for Magento

Magento Postman repository This Repository is a storage of Postman collections for Magento. If you have what to share, you are welcome to contribute a

File uploads with validation and storage strategies

Upload This component simplifies file validation and uploading. Usage Assume a file is uploaded with this HTML form: form method="POST" enctype="mult

Google Cloud Storage for PHP

Google Cloud Storage for PHP Idiomatic PHP client for Cloud Storage. API documentation NOTE: This repository is part of Google Cloud PHP. Any support

File Storage Api

flux-file-storage-api File Storage Api Installation Native Download RUN (mkdir -p /%path%/libs/flux-file-storage-api && cd /%path%/libs/flux-file-stor

Owner
Changfeng Ji
Changfeng Ji
Fresns core library: Cross-platform general-purpose multiple content forms social network service software

About Fresns Fresns is a free and open source social network service software, a general-purpose community product designed for cross-platform, and su

Fresns 82 Dec 31, 2022
It's goal is to create my own platform where I can play various board games I like.

Card games The goal of this project is to create a social platform where players can communicate, create lobbies and play various board games. Current

Ondřej Mastík 2 Oct 29, 2021
⭐ It is an platform for people to help them get connected with the like minding folks around the globe.

Meetups It is an Platform for people to help them get connected with the like minded folks around the globe. Live on Web: Cick here ?? Meet and Connec

Hardik Kaushik 5 Apr 26, 2022
This is a plugin written in PHP programming language and running on the PocketMine platform that works stably on the API 3.25.0 platform

This is a plugin written in PHP programming language and running on the PocketMine platform that works stably on the API 3.25.0 platform. It allows you to hear the sound

Thành Nhân 10 Sep 27, 2022
Simple Symfony API-Platform Template which you can use to start to develop with symfony and api-platform

symfony-api-platform-skeleton Simple Template for Symfony API You can fork it and change the git remote to your Repo git remote set-url <your-git-remo

null 1 Jan 23, 2022
Add information about PGP public keys on upload in Kirby v3

Kirby3 GnuPG This plugin adds information about PGP public keys on upload, using gpg binary (which needs to be installed for this to work). Getting st

Fundevogel 2 Oct 11, 2021
I create this code to upload CSV in database in packets of 100.

PHP-CSV-Upload I create this code to upload CSV in the database in packets of 100 elements in one for loop. Hi, This is Anmol Singh. To reach our Goal

Anmol Singh 2 Feb 12, 2022
File Upload widget with multiple file selection, drag&drop support, progress bar, validation and preview images, audio and video for jQuery

File Upload widget with multiple file selection, drag&drop support, progress bar, validation and preview images, audio and video for jQuery. Supports cross-domain, chunked and resumable file uploads. Works with any server-side platform (Google App Engine, PHP, Python, Ruby on Rails, Java, etc.) that supports standard HTML form file uploads.

Sebastian Tschan 31.1k Dec 30, 2022
A dockerized PHP application containing some file upload vulnerability challenges (scenarios)

File Upload Vulnerability Scenarios (Challenges) This repository is a dockerized PHP application containing some file upload vulnerability challenges

Moein Fatehi 15 Dec 23, 2022
This is a small piece of code to steal firefox's cookies and upload to server (Written in VBA)

Steal-firefox-cookies-VBA-macro This is a small piece of code to steal firefox's cookies and upload to server (Written in VBA) Please note For educati

HitmanAlharbi 6 Sep 27, 2022