English | 简体中文
Upload data to OSS through Web applications. Add signatures on the server, configure upload callback, and directly transfer data.
composer require alphasnow/aliyun-oss-appserver
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
- CORS configuration / Create rule / Example:
Soucre: *, Allow Methods: POST
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");
- 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
- 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"
Example of single-file services with clients
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");