1.composer安装阿里云OSS SDK

composer require aliyuncs/oss-sdk-php

2.控制器里面引用

use OSS\Core\OssException;

use OSS\OssClient;

use OSS\Core\OssUtil; 

2.上传代码逻辑

//        接收文件数据
    $file = request()->file('img');

// 取出文件名截取后缀

    $name = $file->getOriginalName();
    $suffix = strchr($name,'.');

// 阿里云配置

    $accessKeyId = "LTAI5t88SfZ5yhH4Sgu1u3gt";
    $accessKeySecret = "vbLBZWvzazGZvJpPap20ZyKDoBbdq2";
    // Endpoint以杭州为例,其它Region请按实际情况填写。
    $endpoint = "oss-cn-shanghai.aliyuncs.com";
    // 设置存储空间名称。
    $bucket= "*";
    // 设置文件名称。
    //这里是由sha1加密生成文件名 之后连接上文件后缀
    $object = sha1(date('YmdHis', time()) . uniqid()) . $suffix;
    $url = 'http://test.caotengfei.xyz/'.$object;
    // <yourLocalFile>由本地文件路径加文件名包括后缀组成,例如/users/local/myfile.txt。
    $filePath = $file->getPathname();
    $options = array(
        OssClient::OSS_CHECK_MD5 => true,
        OssClient::OSS_PART_SIZE => 1,
    );

    try{
        $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
        //返回uploadId。uploadId是分片上传事件的唯一标识,您可以根据uploadId发起相关的操作,如取消分片上传、查询分片上传等。
        $uploadId = $ossClient->initiateMultipartUpload($bucket, $object);
    } catch(OssException $e) {
        printf(__FUNCTION__ . ": initiateMultipartUpload FAILED\n");
        printf($e->getMessage() . "\n");
        return ;
    }
    print(__FUNCTION__ . ": initiateMultipartUpload OK" . "\n");

    $partSize = 10 * 1024 * 1024;
    $uploadFileSize = filesize($filePath);
    $pieces = $ossClient->generateMultiuploadParts($uploadFileSize, $partSize);
    $responseUploadPart = array();
    $uploadPosition = 0;
    $isCheckMd5 = true;
    foreach ($pieces as $i => $piece) {
        $fromPos = $uploadPosition + (integer)$piece[$ossClient::OSS_SEEK_TO];
        $toPos = (integer)$piece[$ossClient::OSS_LENGTH] + $fromPos - 1;
        $upOptions = array(
            // 上传文件。
            $ossClient::OSS_FILE_UPLOAD => $filePath,
            // 设置分片号。
            $ossClient::OSS_PART_NUM => ($i + 1),
            // 指定分片上传起始位置。
            $ossClient::OSS_SEEK_TO => $fromPos,
            // 指定文件长度。
            $ossClient::OSS_LENGTH => $toPos - $fromPos + 1,
            // 是否开启MD5校验,true为开启。
            $ossClient::OSS_CHECK_MD5 => $isCheckMd5,
        );
        // 开启MD5校验。
        if ($isCheckMd5) {
            $contentMd5 = OssUtil::getMd5SumForFile($filePath, $fromPos, $toPos);
            $upOptions[$ossClient::OSS_CONTENT_MD5] = $contentMd5;
        }
        try {
            // 上传分片。
            $responseUploadPart[] = $ossClient->uploadPart($bucket, $object, $uploadId, $upOptions);
        } catch(OssException $e) {
            printf(__FUNCTION__ . ": initiateMultipartUpload, uploadPart - part#{$i} FAILED\n");
            printf($e->getMessage() . "\n");
            return;
        }
        printf(__FUNCTION__ . ": initiateMultipartUpload, uploadPart - part#{$i} OK\n");
    }
// $uploadParts是由每个分片的ETag和分片号(PartNumber)组成的数组。
        $uploadParts = array();
        foreach ($responseUploadPart as $i => $eTag) {
            $uploadParts[] = array(
                'PartNumber' => ($i + 1),
                'ETag' => $eTag,
            );
        }
    try {
        // 执行completeMultipartUpload操作时,需要提供所有有效的$uploadParts。OSS收到提交的$uploadParts后,会逐一验证每个分片的有效性。当所有的数据分片验证通过后,OSS将把这些分片组合成一个完整的文件。
        $ossClient->completeMultipartUpload($bucket, $object, $uploadId, $uploadParts);
    }  catch(OssException $e) {
        printf(__FUNCTION__ . ": completeMultipartUpload FAILED\n");
        printf($e->getMessage() . "\n");
        return;
    }
        return json(['code'=>200,'msg'=>'成功','url'=>$url]);





标签: none

添加新评论