https://blog.csdn.net/ganggexiongqi/article/details/50483389
--
使用官方 SDK
任何需要使用別人提供的 PHP 程式,首先要知道的就是「該 PHP 是什麼版本寫的」
換句話說,尋找 SDK 時不是說最新的最適合,而是會跑才適合。
以 Hoyo 的測試環境舉例 CentOS 6.7,這是一個相當舊的版本,PHP 為 5.3 版,網路上搜尋後可以得知適當的 Amazon PHP SDK 為 v2 版本,v3 需要 PHP 5.5 以上才支援。
--
使用之前
請先將 Server 的時間校準,雖然不是所有功能都對時間敏感,不過做了就沒錯
如果看到 PHP 發生類似這個錯誤
1 |
PHP Fatal error: Uncaught Aws\\S3\\Exception\\RequestTimeTooSkewedException: AWS Error Code: RequestTimeTooSkewed |
就代表你主機的時間不準了
--
開始使用
參考
上傳
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?php require __DIR__.'/Component_Back/aws-autoloader.php'; use Aws\S3\S3Client; use Aws\S3\Exception\S3Exception; $bucket = 'hoyo'; $keyname = 'H_72.png'; // $filepath should be absolute path to a file on disk $filepath = 'H_72.png'; // Instantiate the client. $s3 = S3Client::factory(array( 'key' => '你自己的 key', 'secret' => '你自己的 secret' )); try { // Upload data. $result = $s3->putObject(array( 'Bucket' => $bucket, 'Key' => $keyname, 'SourceFile' => $filepath, 'ACL' => 'public-read' )); // Print the URL to the object. //echo '<pre>'; print_r( $result['ObjectURL'] ); echo '</pre>'; } catch (S3Exception $e) { //echo $e->getMessage() . "\n"; } |
--
將 Bucket 當作本地存取
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php require __DIR__ .'/../Component_Back/aws-autoloader.php'; use Aws\S3\S3Client; use Aws\S3\Exception\S3Exception; use Aws\S3\StreamWrapper; $client = S3Client::factory(array( 'key' => S3Key, 'secret' => S3Secret )); $client->registerStreamWrapper(); $data = file_get_contents('s3://soundu/2016/20161101/2ff1603017878be35d70acc6c86f18ac'); echo $data; |
--
使用 HTML Forms 上傳及包裝成 PHP curl post
參考
和原程式差別在於 Region 的設定,有關 Region 的設定可以參考 AWS区域和终端节点
將 region 固定則上傳網址也必須隨之調整
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
<?php $access_key = "你的 Key"; //Access Key $secret_key = "你的 Secret"; //Secret Key $my_bucket = "你的 Bucket"; //bucket name $region = "us-west-2"; //bucket region $success_redirect = 'http://'. $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; //URL to which the client is redirected upon success (currently self) $allowd_file_size = "1048579"; //1 MB allowed Size //dates $short_date = gmdate('Ymd'); //short date $iso_date = gmdate("Ymd\THis\Z"); //iso format date $expiration_date = gmdate('Y-m-d\TG:i:s\Z', strtotime('+1 hours')); //policy expiration 1 hour from now //POST Policy required in order to control what is allowed in the request //For more info http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-HTTPPOSTConstructPolicy.html $policy = utf8_encode(json_encode(array( 'expiration' => $expiration_date, 'conditions' => array( array('acl' => 'public-read'), array('bucket' => $my_bucket), array('success_action_redirect' => $success_redirect), array('starts-with', '$key', ''), array('content-length-range', '1', $allowd_file_size), array('x-amz-credential' => $access_key.'/'.$short_date.'/'.$region.'/s3/aws4_request'), array('x-amz-algorithm' => 'AWS4-HMAC-SHA256'), array('X-amz-date' => $iso_date) )))); //Signature calculation (AWS Signature Version 4) //For more info http://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-authenticating-requests.html $kDate = hash_hmac('sha256', $short_date, 'AWS4' . $secret_key, true); $kRegion = hash_hmac('sha256', $region, $kDate, true); $kService = hash_hmac('sha256', "s3", $kRegion, true); $kSigning = hash_hmac('sha256', "aws4_request", $kService, true); $signature = hash_hmac('sha256', base64_encode($policy), $kSigning); ?> <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Aws S3 Direct File Uploader</title> </head> <body> <form action="http://<?php echo $my_bucket ?>.s3-us-west-2.amazonaws.com/" method="post" enctype="multipart/form-data"> <input type="hidden" name="key" value="${filename}" /> <input type="hidden" name="acl" value="public-read" /> <input type="hidden" name="X-Amz-Credential" value="<?php echo $access_key; ?>/<?php echo $short_date; ?>/<?php echo $region; ?>/s3/aws4_request" /> <input type="hidden" name="X-Amz-Algorithm" value="AWS4-HMAC-SHA256" /> <input type="hidden" name="X-Amz-Date" value="<?php echo $iso_date ; ?>" /> <input type="hidden" name="Policy" value="<?php echo base64_encode($policy); ?>" /> <input type="hidden" name="X-Amz-Signature" value="<?php echo $signature ?>" /> <input type="hidden" name="success_action_redirect" value="<?php echo $success_redirect ?>" /> <input type="file" name="file" /> <input type="submit" value="Upload File" /> </form> <?php //After success redirection from AWS S3 if(isset($_GET["key"])) { $filename = $_GET["key"]; $ext = pathinfo($filename, PATHINFO_EXTENSION); if(in_array($ext, array("jpg", "png", "gif", "jpeg"))){ echo '<hr />Image File Uploaded : <br /><img src="//'.$my_bucket.'.s3.amazonaws.com/'.$_GET["key"].'" style="width:100%;" />'; }else{ echo '<hr />File Uploaded : <br /><a href="http://'.$my_bucket.'.s3.amazonaws.com/'.$_GET["key"].'">'.$filename.'</a>'; } } ?> </body> </html> |
--
2,604 total views, 2 views today