PHP使用OSS存储

版权声明:本文为博主原创文章,未经博主允许也可以转载,但是烦请标明博客来源,谢谢。 https://blog.csdn.net/sinat_35861727/article/details/82590109

1.SDK的下载与安装
见阿里云OSS文档 : https://help.aliyun.com/document_detail/85580.html?spm=a2c4g.11186623.6.768.14915113ZsNHlV
2.上传文件
(1)在项目中引入OSS类库 , 并申明命名空间

require_once "./vendor/aliyun_oss/autoload.php";
use OSS\OssClient;
use OSS\Core\OssException;

(2)开始上传

/**
	 * OSS上传本地文件内容
	 * @param string $savename 上传的文件名称
	 * @param string $filepath 本地文件路径
	 * @return null
	 */
    function ossUpload($filepath,$savename){
        $config = oss_config();
        $accessKeyId = $config['accessKey'];		// Access Key ID
        $accessKeySecret = $config['secretKey'];	// Access Key Secret
        $endpoint = $config['domain'];				// 阿里云oss 外网地址endpoint
        $bucket = $config['bucket'];				// Bucket名称

        try {
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
            $result = $ossClient->uploadFile($bucket, $savename, $filePath);
        } catch (OssException $e) {
            print_r($e->getMessage() . "\n");
        }

        return $result;
    }

详情可参看官方文档 : https://help.aliyun.com/document_detail/32100.html?spm=a2c4g.11186623.6.770.359d2b70BMA6aH

猜你喜欢

转载自blog.csdn.net/sinat_35861727/article/details/82590109
今日推荐