Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 215 lines (174 sloc) 6.799 kB
62884bb @tpyo
authored
1 #!/usr/local/bin/php
2 <?php
6249cf4 @tpyo Comment updates
authored
3 /**
4 * $Id$
5 *
c982f44 @tpyo
authored
6 * Note: Although this wrapper works, it would be more efficient to use the S3 class instead
6249cf4 @tpyo Comment updates
authored
7 */
62884bb @tpyo
authored
8
0154802 @tpyo Updated examples, renamed enableBucketLogging to setBucketLogging
authored
9 if (!class_exists('S3')) require_once 'S3.php';
62884bb @tpyo
authored
10
11 // AWS access info
12 if (!defined('awsAccessKey')) define('awsAccessKey', 'change-this');
13 if (!defined('awsSecretKey')) define('awsSecretKey', 'change-this');
14
15 // Check for CURL
16 if (!extension_loaded('curl') && !@dl(PHP_SHLIB_SUFFIX == 'so' ? 'curl.so' : 'php_curl.dll'))
17 exit("\nERROR: CURL extension not loaded\n\n");
18
19 // Pointless without your keys!
20 if (awsAccessKey == 'change-this' || awsSecretKey == 'change-this')
21 exit("\nERROR: AWS access information required\n\nPlease edit the following lines in this file:\n\n".
22 "define('awsAccessKey', 'change-me');\ndefine('awsSecretKey', 'change-me');\n\n");
23
24
25 ################################################################################
26
27
28 final class S3Wrapper extends S3 {
29 private $position = 0, $mode = '', $buffer;
30
31 public function url_stat($path, $flags) {
32 self::__getURL($path);
33 return (($info = self::getObjectInfo($this->url['host'], $this->url['path'])) !== false) ?
34 array('size' => $info['size'], 'mtime' => $info['time'], 'ctime' => $info['time']) : false;
35 }
36
37 public function unlink($path) {
38 self::__getURL($path);
39 return self::deleteObject($this->url['host'], $this->url['path']);
40 }
41
42 public function mkdir($path, $mode, $options) {
43 self::__getURL($path);
44 return self::putBucket($this->url['host'], self::__translateMode($mode));
45 }
46
47 public function rmdir($path) {
48 self::__getURL($path);
49 return self::deleteBucket($this->url['host']);
50 }
51
52 public function dir_opendir($path, $options) {
53 self::__getURL($path);
54 if (($contents = self::getBucket($this->url['host'], $this->url['path'])) !== false) {
55 $pathlen = strlen($this->url['path']);
56 if (substr($this->url['path'], -1) == '/') $pathlen++;
57 $this->buffer = array();
58 foreach ($contents as $file) {
59 if ($pathlen > 0) $file['name'] = substr($file['name'], $pathlen);
60 $this->buffer[] = $file;
61 }
62 return true;
63 }
64 return false;
65 }
66
67 public function dir_readdir() {
68 return (isset($this->buffer[$this->position])) ? $this->buffer[$this->position++]['name'] : false;
69 }
70
71 public function dir_rewinddir() {
72 $this->position = 0;
73 }
74
75 public function dir_closedir() {
76 $this->position = 0;
77 unset($this->buffer);
78 }
79
80 public function stream_close() {
81 if ($this->mode == 'w') {
82 self::putObject($this->buffer, $this->url['host'], $this->url['path']);
83 }
84 $this->position = 0;
85 unset($this->buffer);
86 }
87
88 public function stream_stat() {
89 if (is_object($this->buffer) && isset($this->buffer->headers))
90 return array(
91 'size' => $this->buffer->headers['size'],
92 'mtime' => $this->buffer->headers['time'],
93 'ctime' => $this->buffer->headers['time']
94 );
95 elseif (($info = self::getObjectInfo($this->url['host'], $this->url['path'])) !== false)
96 return array('size' => $info['size'], 'mtime' => $info['time'], 'ctime' => $info['time']);
97 return false;
98 }
99
100 public function stream_flush() {
101 $this->position = 0;
102 return true;
103 }
104
105 public function stream_open($path, $mode, $options, &$opened_path) {
106 if (!in_array($mode, array('r', 'rb', 'w', 'wb'))) return false; // Mode not supported
107 $this->mode = substr($mode, 0, 1);
108 self::__getURL($path);
109 $this->position = 0;
110 if ($this->mode == 'r') {
111 if (($this->buffer = self::getObject($this->url['host'], $this->url['path'])) !== false) {
112 if (is_object($this->buffer->body)) $this->buffer->body = (string)$this->buffer->body;
113 } else return false;
114 }
115 return true;
116 }
117
118 public function stream_read($count) {
119 if ($this->mode !== 'r' && $this->buffer !== false) return false;
120 $data = substr(is_object($this->buffer) ? $this->buffer->body : $this->buffer, $this->position, $count);
121 $this->position += strlen($data);
122 return $data;
123 }
124
125 public function stream_write($data) {
126 if ($this->mode !== 'w') return 0;
127 $left = substr($this->buffer, 0, $this->position);
128 $right = substr($this->buffer, $this->position + strlen($data));
129 $this->buffer = $left . $data . $right;
130 $this->position += strlen($data);
131 return strlen($data);
132 }
133
134 public function stream_tell() {
135 return $this->position;
136 }
137
138 public function stream_eof() {
139 return $this->position >= strlen(is_object($this->buffer) ? $this->buffer->body : $this->buffer);
140 }
141
142 public function stream_seek($offset, $whence) {
143 switch ($whence) {
144 case SEEK_SET:
145 if ($offset < strlen($this->buffer->body) && $offset >= 0) {
146 $this->position = $offset;
147 return true;
148 } else return false;
149 break;
150 case SEEK_CUR:
151 if ($offset >= 0) {
152 $this->position += $offset;
153 return true;
154 } else return false;
155 break;
156 case SEEK_END:
157 $bytes = strlen($this->buffer->body);
158 if ($bytes + $offset >= 0) {
159 $this->position = $bytes + $offset;
160 return true;
161 } else return false;
162 break;
163 default: return false;
164 }
165 }
166
167 private function __getURL($path) {
168 $this->url = parse_url($path);
169 if (!isset($this->url['scheme']) || $this->url['scheme'] !== 's3') return $this->url;
170 if (isset($this->url['user'], $this->url['pass'])) self::setAuth($this->url['user'], $this->url['pass']);
171 $this->url['path'] = isset($this->url['path']) ? substr($this->url['path'], 1) : '';
172 }
173
174 private function __translateMode($mode) {
175 $acl = self::ACL_PRIVATE;
176 if (($mode & 0x0020) || ($mode & 0x0004))
177 $acl = self::ACL_PUBLIC_READ;
178 // You probably don't want to enable public write access
179 if (($mode & 0x0010) || ($mode & 0x0008) || ($mode & 0x0002) || ($mode & 0x0001))
180 $acl = self::ACL_PUBLIC_READ; //$acl = self::ACL_PUBLIC_READ_WRITE;
181 return $acl;
182 }
183 } stream_wrapper_register('s3', 'S3Wrapper');
184
185
186 ################################################################################
187
188
189 S3::setAuth(awsAccessKey, awsSecretKey);
190
191
192 $bucketName = uniqid('s3test');
193
194 echo "Creating bucket: {$bucketName}\n";
195 var_dump(mkdir("s3://{$bucketName}"));
196
197 echo "\nWriting file: {$bucketName}/test.txt\n";
198 var_dump(file_put_contents("s3://{$bucketName}/test.txt", "Eureka!"));
199
200 echo "\nReading file: {$bucketName}/test.txt\n";
201 var_dump(file_get_contents("s3://{$bucketName}/test.txt"));
202
203 echo "\nContents for bucket: {$bucketName}\n";
204 foreach (new DirectoryIterator("s3://{$bucketName}") as $b) {
205 echo "\t".$b."\n";
206 }
207
208 echo "\nUnlinking: {$bucketName}/test.txt\n";
209 var_dump(unlink("s3://{$bucketName}/test.txt"));
210
211 echo "\nRemoving bucket: {$bucketName}\n";
212 var_dump(rmdir("s3://{$bucketName}"));
213
214
215 #EOF
Something went wrong with that request. Please try again.