Home >Backend Development >PHP Tutorial >Using Alibaba Cloud OSS Composer package sharing in Laravel_PHP tutorial
This article mainly introduces the use of Alibaba Cloud OSS Composer package sharing in Laravel. This article explains how to use it. For file downloads, please go to The github address in the file, friends in need can refer to it
Alibaba Cloud provides the V2 version of the SDK based on the namespace, but the documentation is not very complete and the usage threshold is relatively high, so I packaged a Composer package: https://github.com/johnlui/AliyunOSS
Installation
Add the following content to composer.json:
The code is as follows:
"johnlui/aliyun-oss": "dev-master"
}
Then run composer update
Use
The code is as follows:
//Construct OSSClient object
// Three parameters: server address, AccessKeyId provided by Alibaba Cloud, AccessKeySecret
$oss = AliyunOSS::boot('http://oss-cn-qingdao.aliyuncs.com', $AccessKeyId, $AccessKeySecret);
// Set Bucket
$oss = $oss->setBucket($bucketName);
// Upload a file (the example file is robots.txt in the public directory)
//Two parameters: resource name, file path
$oss->uploadFile('robots.txt', public_path('robots.txt'));
// Get the URL of this resource from the server and print it
// Two parameters: resource name, expiration time
echo $oss->getUrl('robots.txt', new DateTime("+1 day"));
It’s that simple. If you like it, you can star it on Github!