| 1 |
lars |
1 |
#! /usr/bin/env php
|
|
|
2 |
<?php
|
|
|
3 |
/*
|
|
|
4 |
* Copyright 2010-2012 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
|
5 |
*
|
|
|
6 |
* Licensed under the Apache License, Version 2.0 (the "License").
|
|
|
7 |
* You may not use this file except in compliance with the License.
|
|
|
8 |
* A copy of the License is located at
|
|
|
9 |
*
|
|
|
10 |
* http://aws.amazon.com/apache2.0
|
|
|
11 |
*
|
|
|
12 |
* or in the "license" file accompanying this file. This file is distributed
|
|
|
13 |
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
|
|
|
14 |
* express or implied. See the License for the specific language governing
|
|
|
15 |
* permissions and limitations under the License.
|
|
|
16 |
*/
|
|
|
17 |
|
|
|
18 |
/*
|
|
|
19 |
PREREQUISITES:
|
|
|
20 |
In order to run this sample, I'll assume a few things:
|
|
|
21 |
|
|
|
22 |
* You already have a valid Amazon Web Services developer account, and are
|
|
|
23 |
signed up to use Amazon S3 <http://aws.amazon.com/s3>.
|
|
|
24 |
|
|
|
25 |
* You already understand the fundamentals of object-oriented PHP.
|
|
|
26 |
|
|
|
27 |
* You've verified that your PHP environment passes the SDK Compatibility Test.
|
|
|
28 |
|
|
|
29 |
* You've already added your credentials to your config.inc.php file, as per the
|
|
|
30 |
instructions in the Getting Started Guide.
|
|
|
31 |
|
|
|
32 |
TO RUN:
|
|
|
33 |
* Run this file from the command line with `php cli-s3_progress_bar.php`.
|
|
|
34 |
*/
|
|
|
35 |
|
|
|
36 |
|
|
|
37 |
/*%******************************************************************************************%*/
|
|
|
38 |
// SETUP
|
|
|
39 |
|
|
|
40 |
// Enable full-blown error reporting. http://twitter.com/rasmus/status/7448448829
|
|
|
41 |
error_reporting(-1);
|
|
|
42 |
|
|
|
43 |
// Set plain text headers
|
|
|
44 |
header("Content-type: text/plain; charset=utf-8");
|
|
|
45 |
|
|
|
46 |
// Include the SDK
|
|
|
47 |
require_once '../sdk.class.php';
|
|
|
48 |
|
|
|
49 |
// Include PEAR Console_ProgressBar
|
|
|
50 |
require_once 'lib/ProgressBar.php';
|
|
|
51 |
|
|
|
52 |
|
|
|
53 |
/*%******************************************************************************************%*/
|
|
|
54 |
// DOWNLOAD SAMPLE FILE FROM S3
|
|
|
55 |
|
|
|
56 |
// Instantiate the AmazonS3 class
|
|
|
57 |
$s3 = new AmazonS3();
|
|
|
58 |
|
|
|
59 |
// Instantiate a new progress bar.
|
|
|
60 |
// We won't know the max number of bytes until the download starts, so we'll handle that in our callback.
|
|
|
61 |
$progress_bar = new Console_ProgressBar('* %fraction% KB [%bar%] %percent%', '=>', ' ', 100, 1);
|
|
|
62 |
$progress_bar->UPDATED = false;
|
|
|
63 |
|
|
|
64 |
// Register a callback function to execute when a stream is written locally.
|
|
|
65 |
$s3->register_streaming_write_callback('write_callback');
|
|
|
66 |
|
|
|
67 |
function write_callback($curl_handle, $length)
|
|
|
68 |
{
|
|
|
69 |
// Import from global scope
|
|
|
70 |
$progress_bar = $GLOBALS['progress_bar'];
|
|
|
71 |
|
|
|
72 |
// Have we updated the format with updated information yet?
|
|
|
73 |
if (!$progress_bar->UPDATED)
|
|
|
74 |
{
|
|
|
75 |
// Add the Content-Length of the file as the max number of bytes.
|
|
|
76 |
$progress_bar->reset('* %fraction% KB [%bar%] %percent%', '=>', ' ', 100,
|
|
|
77 |
curl_getinfo($curl_handle, CURLINFO_CONTENT_LENGTH_DOWNLOAD));
|
|
|
78 |
$progress_bar->UPDATED = true;
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
// Update the progress bar with the cumulative number of bytes downloaded.
|
|
|
82 |
$progress_bar->update(curl_getinfo($curl_handle, CURLINFO_SIZE_DOWNLOAD));
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
// Add some spacing above the progress bar.
|
|
|
86 |
echo PHP_EOL;
|
|
|
87 |
|
|
|
88 |
echo 'Downloading http://aws-sdk-for-php.s3.amazonaws.com/demo/big-buck-bunny.mp4' . PHP_EOL;
|
|
|
89 |
echo 'Writing to ' . realpath('./downloads') . '/big-buck-bunny.mp4' . PHP_EOL;
|
|
|
90 |
|
|
|
91 |
// Download a public object.
|
|
|
92 |
$response = $s3->get_object('aws-sdk-for-php', 'demo/big-buck-bunny.mp4', array(
|
|
|
93 |
'fileDownload' => './downloads/big-buck-bunny.mp4'
|
|
|
94 |
));
|
|
|
95 |
|
|
|
96 |
// Add some spacing below the progress bar.
|
|
|
97 |
echo PHP_EOL;
|
|
|
98 |
|
|
|
99 |
|
|
|
100 |
/*%******************************************************************************************%*/
|
|
|
101 |
// UPLOAD SAMPLE FILE TO S3
|
|
|
102 |
|
|
|
103 |
$_100_percent = 0;
|
|
|
104 |
|
|
|
105 |
// Create a bucket to upload to
|
|
|
106 |
$bucket = 's3-progress-bar-' . strtolower($s3->key);
|
|
|
107 |
if (!$s3->if_bucket_exists($bucket))
|
|
|
108 |
{
|
|
|
109 |
$response = $s3->create_bucket($bucket, AmazonS3::REGION_US_E1);
|
|
|
110 |
if (!$response->isOK()) die('Could not create `' . $bucket . '`.');
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
// Instantiate a new progress bar.
|
|
|
114 |
// We won't know the max number of bytes until the download starts, so we'll handle that in our callback.
|
|
|
115 |
$progress_bar = new Console_ProgressBar('* %fraction% KB [%bar%] %percent%', '=>', ' ', 100, 1);
|
|
|
116 |
$progress_bar->UPDATED = false;
|
|
|
117 |
|
|
|
118 |
// Register a callback function to execute when a stream is written locally.
|
|
|
119 |
$s3->register_streaming_read_callback('read_callback');
|
|
|
120 |
|
|
|
121 |
function read_callback($curl_handle, $file_handle, $length)
|
|
|
122 |
{
|
|
|
123 |
// Import from global scope
|
|
|
124 |
$progress_bar = $GLOBALS['progress_bar'];
|
|
|
125 |
|
|
|
126 |
// Have we updated the format with updated information yet?
|
|
|
127 |
if (!$progress_bar->UPDATED)
|
|
|
128 |
{
|
|
|
129 |
// Store the total size in local & global scope
|
|
|
130 |
$_100_percent = $GLOBALS['_100_percent'] = curl_getinfo($curl_handle, CURLINFO_CONTENT_LENGTH_UPLOAD);
|
|
|
131 |
|
|
|
132 |
// Add the Content-Length of the file as the max number of bytes.
|
|
|
133 |
$progress_bar->reset('* %fraction% KB [%bar%] %percent%', '=>', ' ', 100, $_100_percent);
|
|
|
134 |
$progress_bar->UPDATED = true;
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
// Update the progress bar with the cumulative number of bytes uploaded.
|
|
|
138 |
$progress_bar->update(curl_getinfo($curl_handle, CURLINFO_SIZE_UPLOAD));
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
// Add some spacing above the progress bar.
|
|
|
142 |
echo PHP_EOL;
|
|
|
143 |
|
|
|
144 |
echo 'Uploading to http://' . $bucket . '.s3.amazonaws.com/big-buck-bunny.mp4' . PHP_EOL;
|
|
|
145 |
echo 'Reading from ' . realpath('./downloads') . '/big-buck-bunny.mp4' . PHP_EOL;
|
|
|
146 |
|
|
|
147 |
// Upload an object.
|
|
|
148 |
$response = $s3->create_object($bucket, 'big-buck-bunny.mp4', array(
|
|
|
149 |
'fileUpload' => './downloads/big-buck-bunny.mp4'
|
|
|
150 |
));
|
|
|
151 |
|
|
|
152 |
// The "read" callback doesn't fire after the last bits are uploaded (it could end at 99.x%), so
|
|
|
153 |
// manually set the upload to 100%.
|
|
|
154 |
$progress_bar->update($_100_percent);
|
|
|
155 |
|
|
|
156 |
// Add some spacing below the progress bar.
|
|
|
157 |
echo PHP_EOL . PHP_EOL;
|