{"id":2708,"date":"2016-06-08T17:20:49","date_gmt":"2016-06-08T09:20:49","guid":{"rendered":"http:\/\/blog.hoyo.idv.tw\/?p=2708"},"modified":"2019-03-25T17:36:28","modified_gmt":"2019-03-25T09:36:28","slug":"amazon-s3-simple-storage-service","status":"publish","type":"post","link":"https:\/\/blog.hoyo.idv.tw\/?p=2708","title":{"rendered":"Amazon S3 (Simple Storage Service)"},"content":{"rendered":"<p>https:\/\/blog.csdn.net\/ganggexiongqi\/article\/details\/50483389<\/p>\n<p>--<\/p>\n<h2>\u4f7f\u7528\u5b98\u65b9 SDK<\/h2>\n<p>\u4efb\u4f55\u9700\u8981\u4f7f\u7528\u5225\u4eba\u63d0\u4f9b\u7684 PHP \u7a0b\u5f0f\uff0c\u9996\u5148\u8981\u77e5\u9053\u7684\u5c31\u662f\u300c<span style=\"color: #ff0000;\"><strong>\u8a72 PHP \u662f\u4ec0\u9ebc\u7248\u672c\u5beb\u7684<\/strong><\/span>\u300d<\/p>\n<p>\u63db\u53e5\u8a71\u8aaa\uff0c\u5c0b\u627e SDK \u6642\u4e0d\u662f\u8aaa\u6700\u65b0\u7684\u6700\u9069\u5408\uff0c\u800c\u662f\u6703\u8dd1\u624d\u9069\u5408\u3002<\/p>\n<p>\u4ee5 Hoyo \u7684\u6e2c\u8a66\u74b0\u5883\u8209\u4f8b CentOS 6.7\uff0c\u9019\u662f\u4e00\u500b\u76f8\u7576\u820a\u7684\u7248\u672c\uff0cPHP \u70ba 5.3 \u7248\uff0c\u7db2\u8def\u4e0a\u641c\u5c0b\u5f8c\u53ef\u4ee5\u5f97\u77e5\u9069\u7576\u7684 Amazon PHP SDK \u70ba v2 \u7248\u672c\uff0cv3 \u9700\u8981 PHP 5.5 \u4ee5\u4e0a\u624d\u652f\u63f4\u3002<\/p>\n<p>--<\/p>\n<h2>\u4f7f\u7528\u4e4b\u524d<\/h2>\n<p>\u8acb\u5148\u5c07 Server \u7684\u6642\u9593\u6821\u6e96\uff0c\u96d6\u7136\u4e0d\u662f\u6240\u6709\u529f\u80fd\u90fd\u5c0d\u6642\u9593\u654f\u611f\uff0c\u4e0d\u904e\u505a\u4e86\u5c31\u6c92\u932f<\/p>\n<p>\u5982\u679c\u770b\u5230 PHP \u767c\u751f\u985e\u4f3c\u9019\u500b\u932f\u8aa4<\/p>\n<pre class=\"lang:default decode:true\">PHP Fatal error:  Uncaught Aws\\\\S3\\\\Exception\\\\RequestTimeTooSkewedException: AWS Error Code: RequestTimeTooSkewed<\/pre>\n<p>\u5c31\u4ee3\u8868\u4f60\u4e3b\u6a5f\u7684\u6642\u9593\u4e0d\u6e96\u4e86<\/p>\n<p>--<\/p>\n<p><strong>\u958b\u59cb\u4f7f\u7528<\/strong><\/p>\n<p>\u53c3\u8003<\/p>\n<ul>\n<li><a href=\"http:\/\/docs.aws.amazon.com\/aws-sdk-php\/v2\/guide\/service-s3.html\" target=\"_blank\" rel=\"noopener\">Amazon Simple Storage Service<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/aws\/aws-sdk-php\/releases?after=3.0.5\" target=\"_blank\" rel=\"noopener\">aws\/aws-sdk-php - SDK \u4e0b\u8f09<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/22442660\/upload-file-on-amazon-s3-with-php-sdk\">Upload file on amazon S3 with PHP SDK<\/a><\/li>\n<\/ul>\n<p><strong>\u4e0a\u50b3<\/strong><\/p>\n<pre class=\"nums:true lang:php decode:true\">&lt;?php\r\nrequire __DIR__.'\/Component_Back\/aws-autoloader.php';\r\n\r\nuse Aws\\S3\\S3Client;\r\nuse Aws\\S3\\Exception\\S3Exception;\r\n\r\n$bucket = 'hoyo';\r\n$keyname = 'H_72.png';\r\n\/\/ $filepath should be absolute path to a file on disk\r\n$filepath = 'H_72.png';\r\n\r\n\/\/ Instantiate the client.\r\n$s3 = S3Client::factory(array(\r\n    'key'    =&gt; '\u4f60\u81ea\u5df1\u7684 key',\r\n    'secret' =&gt; '\u4f60\u81ea\u5df1\u7684 secret'\r\n));\r\n\r\ntry {\r\n    \/\/ Upload data.\r\n    $result = $s3-&gt;putObject(array(\r\n        'Bucket' =&gt; $bucket,\r\n        'Key'    =&gt; $keyname,\r\n        'SourceFile'   =&gt; $filepath,\r\n        'ACL'    =&gt; 'public-read'\r\n    ));\r\n\r\n    \/\/ Print the URL to the object.\r\n    \/\/echo '&lt;pre&gt;'; print_r( $result['ObjectURL'] ); echo '&lt;\/pre&gt;';\r\n} catch (S3Exception $e) {\r\n    \/\/echo $e-&gt;getMessage() . \"\\n\";\r\n}\r\n<\/pre>\n<p>--<\/p>\n<h2>\u5c07 Bucket \u7576\u4f5c\u672c\u5730\u5b58\u53d6<\/h2>\n<ul>\n<li><a href=\"http:\/\/docs.aws.amazon.com\/aws-sdk-php\/v2\/guide\/feature-s3-stream-wrapper.html\" target=\"_blank\" rel=\"noopener\">Amazon S3 Stream Wrapper<\/a><\/li>\n<\/ul>\n<pre class=\"lang:default decode:true \">&lt;?php\r\nrequire __DIR__ .'\/..\/Component_Back\/aws-autoloader.php';\r\nuse Aws\\S3\\S3Client;\r\nuse Aws\\S3\\Exception\\S3Exception;\r\nuse Aws\\S3\\StreamWrapper;\r\n\r\n$client = S3Client::factory(array(\r\n    'key'    =&gt; S3Key,\r\n    'secret' =&gt; S3Secret\r\n));\r\n\r\n$client-&gt;registerStreamWrapper();\r\n\r\n$data = file_get_contents('s3:\/\/soundu\/2016\/20161101\/2ff1603017878be35d70acc6c86f18ac');\r\necho $data;\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>--<\/p>\n<h2>\u4f7f\u7528\u00a0HTML Forms \u4e0a\u50b3\u53ca\u5305\u88dd\u6210 PHP curl post<\/h2>\n<p>\u53c3\u8003<\/p>\n<ul>\n<li><a href=\"https:\/\/www.sanwebe.com\/2015\/09\/direct-upload-to-amazon-aws-s3-using-php-html\" target=\"_blank\" rel=\"noopener\">Direct Upload to Amazon AWS S3 Using PHP &amp; HTML<\/a><\/li>\n<\/ul>\n<p>\u548c\u539f\u7a0b\u5f0f\u5dee\u5225\u5728\u65bc Region \u7684\u8a2d\u5b9a\uff0c\u6709\u95dc Region \u7684\u8a2d\u5b9a\u53ef\u4ee5\u53c3\u8003\u00a0<a href=\"http:\/\/docs.aws.amazon.com\/zh_cn\/general\/latest\/gr\/rande.html#s3_region\" target=\"_blank\" rel=\"noopener\">AWS\u533a\u57df\u548c\u7ec8\u7aef\u8282\u70b9<\/a><\/p>\n<p>\u5c07 region \u56fa\u5b9a\u5247\u4e0a\u50b3\u7db2\u5740\u4e5f\u5fc5\u9808\u96a8\u4e4b\u8abf\u6574<\/p>\n<pre class=\"lang:php decode:true\">&lt;?php\r\n$access_key         = \"\u4f60\u7684 Key\"; \/\/Access Key\r\n$secret_key         = \"\u4f60\u7684 Secret\"; \/\/Secret Key\r\n$my_bucket          = \"\u4f60\u7684 Bucket\"; \/\/bucket name\r\n$region             = \"us-west-2\"; \/\/bucket region\r\n$success_redirect   = 'http:\/\/'. $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; \/\/URL to which the client is redirected upon success (currently self) \r\n$allowd_file_size   = \"1048579\"; \/\/1 MB allowed Size\r\n\r\n\/\/dates\r\n$short_date         = gmdate('Ymd'); \/\/short date\r\n$iso_date           = gmdate(\"Ymd\\THis\\Z\"); \/\/iso format date\r\n$expiration_date    = gmdate('Y-m-d\\TG:i:s\\Z', strtotime('+1 hours')); \/\/policy expiration 1 hour from now\r\n\r\n\/\/POST Policy required in order to control what is allowed in the request\r\n\/\/For more info http:\/\/docs.aws.amazon.com\/AmazonS3\/latest\/API\/sigv4-HTTPPOSTConstructPolicy.html\r\n$policy = utf8_encode(json_encode(array(\r\n    'expiration' =&gt; $expiration_date,\r\n    'conditions' =&gt; array(\r\n        array('acl' =&gt; 'public-read'),\r\n        array('bucket' =&gt; $my_bucket),\r\n        array('success_action_redirect' =&gt; $success_redirect),\r\n        array('starts-with', '$key', ''),\r\n        array('content-length-range', '1', $allowd_file_size),\r\n        array('x-amz-credential' =&gt; $access_key.'\/'.$short_date.'\/'.$region.'\/s3\/aws4_request'),\r\n        array('x-amz-algorithm' =&gt; 'AWS4-HMAC-SHA256'),\r\n        array('X-amz-date' =&gt; $iso_date)\r\n    ))));\r\n\r\n\/\/Signature calculation (AWS Signature Version 4)   \r\n\/\/For more info http:\/\/docs.aws.amazon.com\/AmazonS3\/latest\/API\/sig-v4-authenticating-requests.html  \r\n$kDate = hash_hmac('sha256', $short_date, 'AWS4' . $secret_key, true);\r\n$kRegion = hash_hmac('sha256', $region, $kDate, true);\r\n$kService = hash_hmac('sha256', \"s3\", $kRegion, true);\r\n$kSigning = hash_hmac('sha256', \"aws4_request\", $kService, true);\r\n$signature = hash_hmac('sha256', base64_encode($policy), $kSigning);\r\n?&gt;\r\n&lt;!DOCTYPE HTML&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n    &lt;meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\"&gt;\r\n    &lt;title&gt;Aws S3 Direct File Uploader&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;form action=\"http:\/\/&lt;?php echo $my_bucket ?&gt;.s3-us-west-2.amazonaws.com\/\" method=\"post\" enctype=\"multipart\/form-data\"&gt;\r\n    &lt;input type=\"hidden\" name=\"key\" value=\"${filename}\" \/&gt;\r\n    &lt;input type=\"hidden\" name=\"acl\" value=\"public-read\" \/&gt;\r\n    &lt;input type=\"hidden\" name=\"X-Amz-Credential\" value=\"&lt;?php echo $access_key; ?&gt;\/&lt;?php echo $short_date; ?&gt;\/&lt;?php echo $region; ?&gt;\/s3\/aws4_request\" \/&gt;\r\n    &lt;input type=\"hidden\" name=\"X-Amz-Algorithm\" value=\"AWS4-HMAC-SHA256\" \/&gt;\r\n    &lt;input type=\"hidden\" name=\"X-Amz-Date\" value=\"&lt;?php echo $iso_date ; ?&gt;\" \/&gt;\r\n    &lt;input type=\"hidden\" name=\"Policy\" value=\"&lt;?php echo base64_encode($policy); ?&gt;\" \/&gt;\r\n    &lt;input type=\"hidden\" name=\"X-Amz-Signature\" value=\"&lt;?php echo $signature ?&gt;\" \/&gt;\r\n    &lt;input type=\"hidden\" name=\"success_action_redirect\" value=\"&lt;?php echo $success_redirect ?&gt;\" \/&gt;\r\n    &lt;input type=\"file\" name=\"file\" \/&gt;\r\n    &lt;input type=\"submit\" value=\"Upload File\" \/&gt;\r\n&lt;\/form&gt;\r\n&lt;?php\r\n\/\/After success redirection from AWS S3\r\nif(isset($_GET[\"key\"]))\r\n{\r\n    $filename = $_GET[\"key\"];\r\n    $ext = pathinfo($filename, PATHINFO_EXTENSION);\r\n    if(in_array($ext, array(\"jpg\", \"png\", \"gif\", \"jpeg\"))){\r\n        echo '&lt;hr \/&gt;Image File Uploaded : &lt;br \/&gt;&lt;img src=\"\/\/'.$my_bucket.'.s3.amazonaws.com\/'.$_GET[\"key\"].'\" style=\"width:100%;\" \/&gt;';\r\n    }else{\r\n        echo '&lt;hr \/&gt;File Uploaded : &lt;br \/&gt;&lt;a href=\"http:\/\/'.$my_bucket.'.s3.amazonaws.com\/'.$_GET[\"key\"].'\"&gt;'.$filename.'&lt;\/a&gt;';\r\n    }\r\n}\r\n?&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>&nbsp;<\/p>\n<p>--<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"2708\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;3,077&nbsp;total views<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/blog.cs...<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"2708\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;3,077&nbsp;total views<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[287],"tags":[218,53,219],"_links":{"self":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/2708"}],"collection":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2708"}],"version-history":[{"count":13,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/2708\/revisions"}],"predecessor-version":[{"id":5706,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/2708\/revisions\/5706"}],"wp:attachment":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2708"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2708"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2708"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}