把本地文件上传到s3上

1,mysql 表结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
SET NAMES utf8;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for `s3_upload`
-- ----------------------------
DROP TABLE IF EXISTS `s3_upload`;
CREATE TABLE `s3_upload` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`book_path` varchar(255) NOT NULL,
`status` tinyint(4) NOT NULL DEFAULT '0' COMMENT '0---不成功,1---成功',
`created_at` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=476 DEFAULT CHARSET=utf8;

SET FOREIGN_KEY_CHECKS = 1;

2,执行脚本

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182

<?php

$dbhost = '""'; //数据库地址
$dbuser = '***'; // 用户名
$dbpw = '***'; // 密码
$dbname = '***'; // 数据库名
$pconnect = 0; // 连接数

require_once './db/db_mysql.class.php';
$db = new dbstuff;
$db->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect);
$db->setcharset("utf-8");
$dbuser = $dbpw = $dbname = $pconnect = NULL;

function set_book_status($bookpath){
global $db;
$query = $db->query("insert into s3_upload (book_path, status, created_at) values('".$bookpath."',1, ".time().")");
}

function get_book_status($bookpath)
{
global $db;
$result = array();
$sql = "SELECT * FROM s3_upload WHERE book_path = '$bookpath' and status =1 ";
$query = $db->query($sql);

while ( ($row = $db->fetch_array($query )) !== false )
{
$result[] = $row;
}
if(count($result))
return true;
else
return false;
}

require 'vendor/autoload.php';

use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;


$s3 = S3Client::factory(array(
'region' => 'us-east-1', //i.e.: 'us-east-1'
'version' => '2006-03-01', //i.e.: 'latest'
));

function getDirContent($path){
if(!is_dir($path)){
return false;
}
$arr = array();
$data = scandir($path);
foreach ($data as $value){
if($value != '.' && $value != '..'){
$arr[] = $value;
}
}
return $arr;
}
//--------------------------------获取书本内的文件名------start
function searchDir($path,&$files){

if(is_dir($path)){

$opendir = opendir($path);

while ($file = readdir($opendir)){
if($file != '.' && $file != '..'){
searchDir($path.'/'.$file, $files);
}
}
closedir($opendir);
}
if(!is_dir($path)){
$files[] = $path;
}
}
//得到目录名
function getDir($dir){
$files = array();
searchDir($dir, $files);
return $files;
}
/* $filenames = getDir('/srv/www/api.1stflip.com/public_html/trbooks/dqda/36id/');

foreach ($filenames as $value){
echo $value."\r\n";
}*/
//-----------------------------获取书本内文件名----------end











$rootPath = "/srv/www/api.1stflip.com/public_html/trbooks/";
$bookpath = "drre/39v3/";


function getDirContentCicle($path,$flag=false){
if(!is_dir($path)){
return false;
}
$arr = array();
$data = scandir($path);
foreach ($data as $value){
if($value != '.' && $value != '..'){
$temp = $path.$value;
if(!is_dir($temp)){
//echo $temp."\r\n";
global $s3, $rootPath;
$str = str_replace($rootPath,"",$temp);
//echo $str."\r\n";
$s3res = $s3->doesObjectExist("online.1stflip.com",$str);
if(!$s3res){
echo $str."---uploading"."\r\n";
$s3->upload("online.1stflip.com",$str,$temp);
}else{
echo $str."---existed"."\r\n";
}
}
getDirContentCicle($temp.'/',true);
}
}
}

//----------获取用户书包目录---------start

function getDirContent3($path,$resFlag=false){
if(!is_dir($path)){
return false;
}
$arr = array();
$data = scandir($path);
foreach ($data as $value){
if($value != '.' && $value != '..'){
if($resFlag){
$bookpath = $path.$value;
if($bookpath && is_dir($bookpath)){
echo $bookpath."\r\n";
$bookstatus = get_book_status($bookpath);
if(!$bookstatus){
getDirContentCicle($bookpath.'/');
set_book_status($bookpath);

}
}

}
}
}
}


function getDirContent2($path){
if(!is_dir($path)){
return false;
}
$arr = array();
$data = scandir($path);
foreach ($data as $value){
if($value != '.' && $value != '..'){
getDirContent3($path.$value.'/',true);
}
}
}


//----------获取用户书包目录---------end


//getDirContentCicle($rootPath.$bookpath);
getDirContent2($rootPath);

?>

3,备注

aws s3 sdk 用的是2.8 版本

php 版本号是5.3版本