<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                今天 Q 群有朋友說不會 OSS 直傳,那我就來寫個例子吧。歡迎兄弟們探討交流。 # 0. 引入 aws 的 sdk ~~~shell composer require aws/aws-sdk-php ~~~ # 1. 上傳到阿里云OSS ## 1.1 創建S3客戶端 ~~~php $s3client = new \Aws\S3\S3Client([ 'credentials' => [ 'key' => 'KEY', 'secret' => 'SK', ], 'region' => 'oss-cn-chengdu', 'version' => '2006-03-01', 'endpoint' => 'https://caylof.oss-cn-chengdu.aliyuncs.com', 'use_path_style_endpoint' => true, // 'endpoint' => 'https://oss-cn-chengdu.aliyuncs.com', // 'addressing_style' => 'virtual', ]); ~~~ ## 1.2 服務器上傳文件 通常前端先上傳到服務器,然后通過服務器再上傳到OSS,即中傳了一次。 ~~~php function put(\Aws\S3\S3Client $s3client): void { $bucket = 'a'; $key = '123.txt'; $s3client->putObject([ 'Bucket' => $bucket, 'Key' => $key, 'Body' => file_get_contents(__DIR__.'/1.php'), 'ContentType' => 'text/plain', ]); } ~~~ ## 1.3 前端直傳文件到OSS ### 1.3.1 服務器先生成簽名表單 ~~~php function buildForm(\Aws\S3\S3Client $s3client): array { $bucket = 'a/123.txt'; // 這里阿云的兼容似乎有點別扭,用真正的bucket會有問題 $key = 'a/123.txt'; $maxUploadSize = 1024 * 1024 * 10; $contentType = 'text/plain'; $formInputs = ['acl' => 'private']; $options = [ ['acl' => 'private'], ['bucket' => $bucket], ['key' => $key], ['Content-Type' => $contentType], ['content-length-range', 1, $maxUploadSize], ]; $expires = '+10 minutes'; $postObject = new \Aws\S3\PostObjectV4( $s3client, $bucket, $formInputs, $options, $expires ); $formAttributes = $postObject->getFormAttributes(); $formInputs = $postObject->getFormInputs(); $formInputs['key'] = $key; $formInputs['Content-Type'] = $contentType; // print_r($formAttributes); // print_r($formInputs); return [$formAttributes, $formInputs]; } ~~~ 返回的`$formAttributes`和`$formAttributes`就是前端需要的表單屬性和表單輸入,然后用于前端進行構造表單并提交即可。 ## 1.3.2 前端直接上傳文件 我這里不采用JS(通常會是axios)來構造表單,而是用 PHP 的 guzzle client 來模擬前端實現。 ~~~php function uploadMock(array $formAttributes, array $formInputs): void { $multipart = []; foreach ($formInputs as $name => $value) { $multipart[] = [ 'name' => $name, 'contents' => $value, ]; } $multipart[] = [ 'name' => 'file', 'contents' => file_get_contents(__DIR__.'/1.php'), ]; $http = new \GuzzleHttp\Client(); $resp = $http->put($formAttributes['action'], [ 'multipart' => $multipart, 'headers' => [ 'Accept' => 'application/json', ], ]); echo $resp->getStatusCode() . PHP_EOL; print_r($resp->getBody()->getContents()); } ~~~ # 2\. 上傳到騰訊云COS ## 2.1 創建S3客戶端 ~~~php $s3client = new \Aws\S3\S3Client([ 'credentials' => [ 'key' => 'KEY', 'secret' => 'SK', ], 'region' => 'auto', 'version' => 'latest', 'endpoint' => 'https://xxx-yyy.cos.ap-chengdu.myqcloud.com', 'use_path_style_endpoint' => true, ]); ~~~ 服務器上傳文件同上阿里云。 ## 2.2 前端直接上傳文件 ### 2.2.1 服務器先生成簽名表單 ~~~php function buildForm(\Aws\S3\S3Client $s3client): array { $bucket = 'xxx-yyy'; // 這里騰訊云看起來比阿里云做的合理 $key = 'a/123.txt'; $maxUploadSize = 1024 * 1024 * 10; $contentType = 'text/plain'; $formInputs = ['acl' => 'private']; $options = [ ['acl' => 'private'], ['bucket' => $bucket], ['key' => $key], ['Content-Type' => $contentType], ['content-length-range', 1, $maxUploadSize], ]; $expires = '+10 minutes'; $postObject = new \Aws\S3\PostObjectV4( $s3client, $bucket, $formInputs, $options, $expires ); $formAttributes = $postObject->getFormAttributes(); $formInputs = $postObject->getFormInputs(); $formInputs['key'] = $key; $formInputs['Content-Type'] = $contentType; // print_r($formAttributes); // print_r($formInputs); return [$formAttributes, $formInputs]; } ~~~ ### 2.2.2 前端直接上傳文件 ~~~php function uploadMock(array $formAttributes, array $formInputs): void { $multipart = []; foreach ($formInputs as $name => $value) { $multipart[] = [ 'name' => str_replace('X-Amz', 'X-Cos', $name), // 騰訊云對名稱做了替換處理,一個小細節 'contents' => $value, ]; } $multipart[] = [ 'name' => 'file', 'contents' => file_get_contents(__DIR__.'/1.php'), ]; $http = new \GuzzleHttp\Client(); $resp = $http->put($formAttributes['action'], [ 'multipart' => $multipart, 'headers' => [ 'Accept' => 'application/json', ], ]); echo $resp->getStatusCode() . PHP_EOL; print_r($resp->getBody()->getContents()); } ~~~ ~ over ~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看