{"id":29,"date":"2024-04-18T17:13:43","date_gmt":"2024-04-18T09:13:43","guid":{"rendered":"https:\/\/a.li6.cc\/?p=29"},"modified":"2024-04-18T17:13:43","modified_gmt":"2024-04-18T09:13:43","slug":"php-aws-s3-%e4%b8%8a%e4%bc%a0%e6%96%87%e4%bb%b6%ef%bc%8c%e4%b8%8b%e8%bd%bd%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/a.li6.cc\/index.php\/2024\/04\/18\/php-aws-s3-%e4%b8%8a%e4%bc%a0%e6%96%87%e4%bb%b6%ef%bc%8c%e4%b8%8b%e8%bd%bd%e6%96%87%e4%bb%b6\/","title":{"rendered":"php aws.s3 \u4e0a\u4f20\u6587\u4ef6\uff0c\u4e0b\u8f7d\u6587\u4ef6"},"content":{"rendered":"<h3>\u4e9a\u9a6c\u900a\u7684s3\u5b58\u50a8\u529f\u80fd<\/h3>\n<pre><code>\/\/ \u4e0a\u4f20\u6587\u4ef6\n vendor(&quot;aws.phar&quot;);\n        $credentials = new Credentials(&#039;key&#039; , &#039;\u5bc6\u94a5&#039;);\n        $s3Client = new S3Client([\n            &#039;credentials&#039; =&gt; $credentials,\n            &#039;region&#039; =&gt; &#039;us-west-2&#039;,\/\/\u5730\u533a\n            &#039;version&#039; =&gt; &#039;latest&#039;,\n            &#039;http&#039; =&gt; [&#039;verify&#039; =&gt; false],\n            &quot;use_path_style_endpoint&quot;=&gt;true\n        ]);\n        $fileName = &quot;\/test.jpg&quot;;\/\/\u5b58\u50a8\u7684\u6587\u4ef6\u540d\n        $bucketName = &#039;\u6876\u540d\u5b57&#039;;\n        try {\n            $status = $s3Client-&gt;putObject([\n                &#039;Bucket&#039; =&gt; $bucketName,\n                &#039;Key&#039; =&gt; $fileName,\n                &#039;SourceFile&#039; =&gt; ROOT_PATH . &#039;public\/uploadfile\/chosePt.png&#039;\n            ]);\n            echo &quot;Uploaded $fileName to $bucketName.\\n&quot;;\n        } catch (\\Exception $exception) {\n            echo &quot;Failed to upload $fileName with error: &quot; . $exception-&gt;getMessage();\n            exit(&quot;Please fix error with file upload before continuing.&quot;);\n        }<\/code><\/pre>\n<pre><code>\/\/\u83b7\u53d6\u4e34\u65f6\u4e0b\u8f7d\u6587\u4ef6url\n        $cmd = $s3Client->getCommand('GetObject', [\n            'Bucket' => $bucketName,\n            'Key' => $fileName\n        ]);\n        $request = $s3Client->createPresignedRequest($cmd, '+5 minutes');\n\n\/\/ Get the actual presigned-url\n        $presignedUrl = (string)$request->getUri();\n        print_r($presignedUrl);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e9a\u9a6c\u900a\u7684s3\u5b58\u50a8\u529f\u80fd \/\/ \u4e0a\u4f20\u6587\u4ef6 vendor(&quot;aws.phar &hellip; <a href=\"https:\/\/a.li6.cc\/index.php\/2024\/04\/18\/php-aws-s3-%e4%b8%8a%e4%bc%a0%e6%96%87%e4%bb%b6%ef%bc%8c%e4%b8%8b%e8%bd%bd%e6%96%87%e4%bb%b6\/\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/a.li6.cc\/index.php\/wp-json\/wp\/v2\/posts\/29"}],"collection":[{"href":"https:\/\/a.li6.cc\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/a.li6.cc\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/a.li6.cc\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/a.li6.cc\/index.php\/wp-json\/wp\/v2\/comments?post=29"}],"version-history":[{"count":1,"href":"https:\/\/a.li6.cc\/index.php\/wp-json\/wp\/v2\/posts\/29\/revisions"}],"predecessor-version":[{"id":30,"href":"https:\/\/a.li6.cc\/index.php\/wp-json\/wp\/v2\/posts\/29\/revisions\/30"}],"wp:attachment":[{"href":"https:\/\/a.li6.cc\/index.php\/wp-json\/wp\/v2\/media?parent=29"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/a.li6.cc\/index.php\/wp-json\/wp\/v2\/categories?post=29"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/a.li6.cc\/index.php\/wp-json\/wp\/v2\/tags?post=29"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}