alphasnow / aliyun-oss-appserver
通过Web应用程序上传数据到OSS
1.1.0
2023-06-15 09:18 UTC
Requires
- php: ^7.2.5|^8.0
- ext-curl: *
- ext-json: *
- ext-openssl: *
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.4
- mockery/mockery: ^1.3
- orchestra/testbench: ^4.18
- phpstan/phpstan: ^1.8
- phpunit/phpunit: ^8.5.23
README
英语 | 简体中文
阿里云OSS AppServer
通过Web应用程序上传数据到OSS。在服务器上添加签名,配置上传回调,并直接传输数据。
安装
composer require alphasnow/aliyun-oss-appserver
配置
修改环境文件 .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/>
(可选) 修改配置文件 config/oss-appserver.php
php artisan vendor:publish --provider=AlphaSnow\OSS\AppServer\Laravel\ServiceProvider
使用方法
OSS配置
- CORS配置 / 创建规则 / 示例:
来源:*, 允许方法:POST
Laravel服务器
添加路由 routes/api.php
,使用默认控制器。
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客户端
- 下载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
- 找到
upload.js
的第30行,并将其更改为实际的服务器地址,例如:http://laravel.local
// serverUrl = "http://88.88.88.88:8888" serverUrl = "http://laravel.local/api/app-server/oss-token"
示例
动态配置
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");