alphasnow/aliyun-oss-appserver

upload data to OSS through web applications

1.1.0 2023-06-15 09:18 UTC

This package is auto-updated.

Last update: 2024-12-05 10:31:19 UTC


README

English | 简体中文

AliYun OSS AppServer

aliyun-oss-appserver

Upload data to OSS through Web applications. Add signatures on the server, configure upload callback, and directly transfer data.

Latest Stable Version Total Downloads License Tests

Installation

composer require alphasnow/aliyun-oss-appserver

Configuration

Modify the environment file .env

OSS_ACCESS_KEY_ID=<Your aliyun accessKeyId, Required, Example: LT************Hz>
OSS_ACCESS_KEY_SECRET=<Your aliyun accessKeySecret, Required, Example: Q5**************************PD>
OSS_BUCKET=<Your oss bucket name, Required, Example: x-storage>
OSS_ENDPOINT=<Your oss endpoint domain, Required, Example: oss-cn-hangzhou.aliyuncs.com>
OSS_SSL=<Whether to use ssl, Optional, Example: true>
OSS_DOMAIN=<Domain name addresses, Optional, Example: x-storage.domain.com>
OSS_CALLBACK_URL=<Default callback address, Optional, Example: https://api.domain.com/callback>
OSS_POLICY_MAX_SIZE=<Default maximum file size 1GB, Optional, Example: 1048576000>
OSS_POLICY_EXPIRE_TIME=<Default expiration time 1 hour, Optional, Example: 3600>
OSS_POLICY_USER_DIR=<Default Upload Directory upload/, Optional, Example: upload/>

(Optional) Modify the config file config/oss-appserver.php

php artisan vendor:publish --provider=AlphaSnow\OSS\AppServer\Laravel\ServiceProvider

Usage

OSS configuration

  • CORS configuration / Create rule / Example: Soucre: *, Allow Methods: POST

Laravel server

Add route routes/api.php, Use the default controller.

Route::get("app-server/oss-token", "\AlphaSnow\OSS\AppServer\Laravel\ServerController@token");
Route::post("app-server/oss-callback", "\AlphaSnow\OSS\AppServer\Laravel\ServerController@callback");

Web client

  1. Download https://www.alibabacloud.com/help/en/object-storage-service/latest/add-signatures-on-the-client-by-using-javascript-and-upload-data-to-oss#title-l7m-nho-uap
  2. Find line 30 of upload.js and change it to the actual server address, example: http://laravel.local
    // serverUrl = "http://88.88.88.88:8888"
    serverUrl = "http://laravel.local/api/app-server/oss-token"

Examples

Use js client to upload file

Example of single-file services with clients

Use curl to upload file

  1. Get the token
    {
        "accessid": "access_key_id",
        "host": "https://bucket.endpoint.com",
        "policy": "eyJleHBpcmF0aW9uIjoiMjAyMi0wMy0yMVQwODoyNzoxNi4wMDBaIiwiY29uZGl0aW9ucyI6W1siY29udGVudC1sZW5ndGgtcmFuZ2UiLDAsMTA0ODU3NjAwMF0sWyJzdGFydHMtd2l0aCIsIiRrZXkiLCJ1cGxvYWRcLyJdXX0=",
        "signature": "P2qcKX8/CKiCzEiDh6CE02HoTRk=",
        "expire": 1647851236,
        "callback": "eyJjYWxsYmFja1VybCI6Imh0dHA6XC9cL2RvbWFpbi5jb21cL2NhbGxiYWNrIiwiY2FsbGJhY2tCb2R5IjoiZmlsZW5hbWU9JHtvYmplY3R9JnNpemU9JHtzaXplfSZtaW1lVHlwZT0ke21pbWVUeXBlfSZoZWlnaHQ9JHtpbWFnZUluZm8uaGVpZ2h0fSZ3aWR0aD0ke2ltYWdlSW5mby53aWR0aH0iLCJjYWxsYmFja0JvZHlUeXBlIjoiYXBwbGljYXRpb25cL3gtd3d3LWZvcm0tdXJsZW5jb2RlZCJ9",
        "dir": "upload/"
    }
  2. Upload file
    curl --location "https://bucket.endpoint.com" \
    --form 'key="upload/${filename}"' \
    --form 'policy="eyJleHBpcmF0aW9uIjoiMjAyMi0wMy0yMVQwODoyNzoxNi4wMDBaIiwiY29uZGl0aW9ucyI6W1siY29udGVudC1sZW5ndGgtcmFuZ2UiLDAsMTA0ODU3NjAwMF0sWyJzdGFydHMtd2l0aCIsIiRrZXkiLCJ1cGxvYWRcLyJdXX0="' \
    --form 'OSSAccessKeyId="access_key_id"' \
    --form 'success_action_status="200"' \
    --form 'callback="eyJjYWxsYmFja1VybCI6Imh0dHA6XC9cL2RvbWFpbi5jb21cL2NhbGxiYWNrIiwiY2FsbGJhY2tCb2R5IjoiZmlsZW5hbWU9JHtvYmplY3R9JnNpemU9JHtzaXplfSZtaW1lVHlwZT0ke21pbWVUeXBlfSZoZWlnaHQ9JHtpbWFnZUluZm8uaGVpZ2h0fSZ3aWR0aD0ke2ltYWdlSW5mby53aWR0aH0iLCJjYWxsYmFja0JvZHlUeXBlIjoiYXBwbGljYXRpb25cL3gtd3d3LWZvcm0tdXJsZW5jb2RlZCJ9"' \
    --form 'signature="P2qcKX8/CKiCzEiDh6CE02HoTRk="' \
    --form 'file=@"~/Downloads/image.jpg"'

Dynamic configuration

use AlphaSnow\OSS\AppServer\Factory;

$token = (new Factory($config))->makeToken();

// Change the address of the direct transmission server
$token->access()->setOssHost("https://bucket.endpoint.com");

// Change the upload directory/timeout period to 60 seconds/maximum file limit to 500 MB
$token->policy()->setUserDir("upload/")->setExpireTime(60)->setMaxSize(500*1024*1024);

// Change the callback address/callback body/callback header
$token->callback()->setCallbackUrl("http://domain.com/oss-callback")
    ->setCallbackBody("filename=\${object}&size=\${size}&mimeType=\${mimeType}&height=\${imageInfo.height}&width=\${imageInfo.width}")
    ->setCallbackBodyType("application/x-www-form-urlencoded");

Ali document

https://www.alibabacloud.com/help/en/object-storage-service/latest/obtain-signature-information-from-the-server-and-upload-data-to-oss