当前位置:网站首页>Laravel8 enables alicloud file upload

Laravel8 enables alicloud file upload

2022-06-10 23:08:00 s_ Canned ice

Official documents of Alibaba cloud

Simple upload (aliyun.com)icon-default.png?t=M4ADhttps://help.aliyun.com/document_detail/88473.html1. adopt composer To download the required plug-ins .

        The following official documents have what we need compser

composer require aliyuncs/oss-sdk-php

install (aliyun.com)icon-default.png?t=M4ADhttps://help.aliyun.com/document_detail/85580.html2. Write the file upload code in the background controller .

         The following official documents have the official upload code examples we need

 // Alicloud upload 
    public function uploades(Request $request)
    {
        // Get picture value 
        // Receive file upload value 
        $file = $request->file('image');
//         Returns a file string 
        $path = $file->getRealPath();

        $ext = $file->getClientOriginalExtension();

//  Alicloud account AccessKey Have all the API Access rights of , The risk is high . It is highly recommended that you create and use it RAM The user carries out API Visit or daily operations , Please log in RAM Console creation RAM user .
        $accessKeyId = "";
        $accessKeySecret = "";
// yourEndpoint Fill in Bucket The region corresponding to Endpoint. East China 1( Hangzhou ) For example ,Endpoint Fill in for https://oss-cn-hangzhou.aliyuncs.com.
        $endpoint = "https://oss-cn-shanghai.aliyuncs.com";
//  Fill in Bucket name , for example examplebucket.
        $bucket = "";
//  Fill in Object The full path , for example exampledir/exampleobject.txt.Object The full path cannot contain Bucket name .
        $object = rand(1111, 9999) . '.' . $ext;
// <yourLocalFile> By the local file path plus file name including suffixes , for example /users/local/myfile.txt.
//  Fill in the full path of the local file , for example D:\\localpath\\examplefile.txt. If no local path is specified , By default, the file is uploaded from the local path corresponding to the project to which the sample program belongs .
        $filePath = $path;
        try {
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);

            $ossClient->uploadFile($bucket, $object, $filePath);
        } catch (OssException $e) {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return;
        }
//         Return to the upload image path 
        $imgUrl = "https://songphoto.oss-cn-shanghai.aliyuncs.com" . $object;
        return ['url' => $imgUrl];
        print(__FUNCTION__ . "OK" . "\n");

    }

Simple upload (aliyun.com)icon-default.png?t=M4ADhttps://help.aliyun.com/document_detail/88473.html

notes : When we use Alibaba cloud to upload , Need to install certification certificate .

原网站

版权声明
本文为[s_ Canned ice]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206102148510184.html