|
1
|
|
|
<?php |
|
2
|
|
|
namespace Qiniu\Storage; |
|
3
|
|
|
|
|
4
|
|
|
use Qiniu\Auth; |
|
5
|
|
|
use Qiniu\Config; |
|
6
|
|
|
use Qiniu\Http\Client; |
|
7
|
|
|
use Qiniu\Http\Error; |
|
8
|
|
|
use Qiniu\Storage\BucketManager; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Desc : A simple file export tool based on BucketManager,Generate download command with php,and download with wget |
|
12
|
|
|
* Author : [email protected] |
|
13
|
|
|
* |
|
14
|
|
|
*/ |
|
15
|
|
|
final class BucketExporter |
|
16
|
|
|
{ |
|
17
|
|
|
private $auth; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct(Auth $auth,$bucket,$logPath=null) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->auth = $auth; |
|
22
|
|
|
$this->bucket = $bucket; |
|
|
|
|
|
|
23
|
|
|
$this->logPath = $logPath ? $logPath : '/tmp/log/qiniu_log/'; |
|
|
|
|
|
|
24
|
|
|
$this->runningTime = date('Y-m-d_H-i-s'); |
|
|
|
|
|
|
25
|
|
|
$this->bucketItemListFilePrefix = $this->logPath.$this->bucket.'_itemLists_'.$this->runningTime; |
|
|
|
|
|
|
26
|
|
|
$this->cmdFilePrefix = $this->logPath.$this->bucket.'_syncCmd_'.$this->runningTime; |
|
|
|
|
|
|
27
|
|
|
file_exists($this->logPath) || mkdir($this->logPath,0777,true); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function exportBucketitem() |
|
31
|
|
|
{ |
|
32
|
|
|
$bucketMgr = new BucketManager($this->auth); |
|
33
|
|
|
$marker = null; |
|
34
|
|
|
$prefix = null; |
|
35
|
|
|
$limit = 10; |
|
36
|
|
|
$delimiter = null; |
|
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
for($i=0;;$i++) |
|
39
|
|
|
{ |
|
40
|
|
|
list($item, $marker, $err) = $bucketMgr->listFiles($this->bucket, $prefix, $marker, $limit);//may be here can replace by marker parameter |
|
|
|
|
|
|
41
|
|
|
$itemLists=''; |
|
42
|
|
|
$t=0; |
|
43
|
|
|
foreach($item as $k=>$v) |
|
44
|
|
|
{ |
|
45
|
|
|
$itemLists.=$v['key']."\n"; |
|
46
|
|
|
$t++; |
|
47
|
|
|
} |
|
48
|
|
|
$itemListFile=$this->bucketItemListFilePrefix.'_'.$i.'.txt'; |
|
|
|
|
|
|
49
|
|
|
file_put_contents($itemListFile,$itemLists,FILE_APPEND); |
|
50
|
|
|
|
|
51
|
|
|
if($t<$limit) |
|
52
|
|
|
{ |
|
53
|
|
|
$count=$i*$limit+$t; |
|
54
|
|
|
$data=array( |
|
55
|
|
|
'msg'=>'Dump over ,Total '.$count.' item !', |
|
56
|
|
|
'count'=>$count, |
|
57
|
|
|
'export_file'=>$itemListFile, |
|
58
|
|
|
); |
|
59
|
|
|
return $data; |
|
60
|
|
|
break; |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function exportFileTree($bucketDomain,$basePath=null) |
|
66
|
|
|
{ |
|
67
|
|
|
$cmdFiles=array(); |
|
68
|
|
|
for($i=0;;$i++) |
|
69
|
|
|
{ |
|
70
|
|
|
$itemListFile=$this->bucketItemListFilePrefix.'_'.$i.'.txt'; |
|
|
|
|
|
|
71
|
|
|
if(!file_exists ($itemListFile)) |
|
72
|
|
|
{ |
|
73
|
|
|
break; |
|
74
|
|
|
} |
|
75
|
|
|
$item = file($itemListFile); |
|
76
|
|
|
$syncCmd=''; |
|
77
|
|
|
foreach($item as $k=>$v) |
|
78
|
|
|
{ |
|
79
|
|
|
if(strlen($v)<1) |
|
80
|
|
|
{ |
|
81
|
|
|
continue;//Sometimes ,Item name is empty;Skip It; |
|
82
|
|
|
} |
|
83
|
|
|
$v = str_replace("\n","",$v); |
|
84
|
|
|
$path_info = pathinfo($v); |
|
85
|
|
|
$base_path = $basePath?$basePath:__DIR__; |
|
86
|
|
|
$local_path = str_replace('\\','/',$base_path.'/'.$path_info['dirname']); |
|
87
|
|
|
file_exists($local_path) || mkdir($local_path,0777,true); |
|
88
|
|
|
|
|
89
|
|
|
if(!file_exists($local_path.'/'.$path_info['basename']))//这里也许可以通过marker遍历位置替代 |
|
90
|
|
|
{ |
|
91
|
|
|
$syncCmd.='wget \''.$bucketDomain.$v.'\' -O '.$local_path.'/'.$path_info['basename'].';'."\n"; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
$cmdFIle=$itemListFile=$this->cmdFilePrefix.'_'.$i.'.txt'; |
|
|
|
|
|
|
96
|
|
|
file_put_contents($cmdFIle,$syncCmd,FILE_APPEND); |
|
97
|
|
|
$cmdFiles[]=$cmdFIle; |
|
98
|
|
|
} |
|
99
|
|
|
return $cmdFiles; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function syncBucketToLocal($bucketDomain,$basePath) |
|
103
|
|
|
{ |
|
104
|
|
|
$this->exportBucketitem(); |
|
105
|
|
|
$cmdFile=$this->exportFileTree($bucketDomain,$basePath); |
|
106
|
|
|
return $cmdFile; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: